著名的3n+1问题如果一个数是偶数,除以2,是奇数,乘3加1,直到变成1为止计算需要的总步数。写了一个java code,很简单,但是网上提交给Sphere,version 1可以接受,version 2就是接受不了,一直说是wrong answer,不知道哪里错了,求助高手解答问题所在。sphere里这个问题的链接在这里
version 1:
http://www.spoj.pl/problems/PROBTNPO/ version 2:
http://www.spoj.pl/problems/PROBTRES/code在这里
// -------
// imports
// -------import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;import java.util.Scanner;// -------------
// SphereCollatz
// -------------public class Main {    // store all the cycle lengths for each number
    private static int [] cache= new int[1000000];
    
    // ----
    // read
    // ----    /**
     * reads two ints into a[0] and a[1]
     * @param r a  java.util.Scanner
     * @param a an array of int
     * @return true if that succeeds, false otherwise
     */
    public static boolean read (Scanner r, int[] a) {
        if (!r.hasNextInt())
            return false;
        a[0] = r.nextInt();
        a[1] = r.nextInt();
        assert a[0] > 0;
        assert a[1] > 0;
        return true;}    // ----
    // eval
    // ----    /**
     * @param i the beginning of the range, inclusive
     * @param j the end       of the range, inclusive
     * @return the max cycle length in the range [i, j]
     */
    public static int eval (int i, int j) {
        assert i > 0;
        assert j > 0;
        assert i < 1000000;
        assert j < 1000000;
        // <your code>
        // code starts
        int max=0;
        for (long k= (long)i; k<= (long)j; k++){
            int val = calc(k);
            if (val>max)
                max=val;
        }
        // code ends
        //int v = 1;
        //assert v > 0;
        //return v;
        assert max > 0;
        return max;
        }    // ----
    // calc
    // ----    /**
     * calculates the cycle length of k
     * @param k the number whose cycle length needs to be calculated
     * @param l the cycle length
     */    public static int calc (long k) {
        if (k == 1)  return 1;        if (k%2 == 1)
                k=3*k+1;
        else
                k = k/2;        if (k < 1000000){
            if(cache[(int)k]!=0)
               return 1+cache[(int)k];
            cache[(int)k] = calc(k);}        return 1+calc(k);
    }
    
    // -----
    // print
    // -----    /**
     * prints the values of i, j, and v
     * @param w a java.io.Writer
     * @param i the beginning of the range, inclusive
     * @param j the end       of the range, inclusive
     * @param v the max cycle length
     */
    public static void print (Writer w, int i, int j, int v) throws IOException {
        assert i > 0;
        assert j > 0;
        assert v > 0;
        w.write(i + " " + j + " " + v + "\n");
        w.flush();}    // -----
    // solve
    // -----    /**
     * @param r a java.util.Scanner
     * @param w a java.io.Writer
     */
    public static void solve (Scanner r, Writer w) throws IOException {
        final int[] a = {0, 0};
        while (read(r, a)) {
            final int v;
            if (a[0] < a[1]){
               if (a[0] < a[1]/2)
                  v = eval(a[1]/2, a[1]);
               else
                  v = eval(a[0], a[1]);
            }
            else{
               if (a[1] < a[0]/2)
                  v = eval(a[0]/2, a[0]);
               else
                  v = eval(a[1], a[0]);
            }
            print(w, a[0], a[1], v);}}    public static void main (String[] args) throws IOException {
        final Scanner r = new Scanner(System.in);
        final Writer w = new PrintWriter(System.out);
        solve(r,w);
  }
}