如何在JAVA中添加约束和条件?

时间:2015-11-16 14:32:28

标签: java cmd

所以我不久前删了我的帖子,到目前为止我要告诉你我的工作。基本上,我是一个新手,我希望你能帮助我解决这个问题,因为我很难想象下一步该做什么。

public class Display{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        //int testcase = input.nextInt();
        int n = 0, even = 0;

        //while(input.hasNext()){
        n = input.nextInt();
        while(n != 1){
            if(n%2 == 0){
                //even++ then n/=2
                even++;
            }
            else{
                //n = 3n+1
            }
            //sequence ctr++
        }
        System.out.println(even);
        //output
    }
}

所以我的教授要求我们做3n + 1问题/ Collat​​z猜想,这里有一些条件。

对于输入N,循环长度N是生成的数字的数量,包括1.在上面的例子中,循环长度22是16.给定数字N,你要确定绝对值N的循环长度与从N开始的序列产生的偶数的数量之间的差异。

约束:

1 <= T <= 100000
1 <= N <= 1000000

输入格式 第一行输入包含整数T,表示测试用例的数量。 T线跟随。每条T线包含一个数字N. 输出格式 对于N的每个值,显示N的循环长度与从N开始的序列生成的偶数的数量之间的绝对差值。 样本输入

4
10
34
22
18237

示例输出

2
4
5
55

解释 对于测试用例N = 10;产生的序列是10 - 5 - 16 - 8 - 4 - 2 - 1,周期长度为7.序列上有5个偶数,分别是10,16,8,4和2.答案是7 - 5 = 2。

1 个答案:

答案 0 :(得分:2)

那么你的代码中缺少什么?看看吧。您需要在每次迭代时更改n。根据您的要求,对于偶数和奇数,需要采用不同的方式。这是你在整个循环中缺少的。另一方面,您会跟踪确实发生的偶数,但您忘记的是跟踪循环中调用的操作总数。

看看你的代码看起来像是一些评论作为explenation。

public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    int n = 0; 
    int even = 0; 
    // You need a variable to count each iteration. One iteration is one mathematical operation on the number. So after each iteration a new number is generated
    // We start with 1 since we have a number that we input.
    int count = 1;  
    System.out.println("Please input a number");
    n = input.nextInt(); 
    while(n != 1){ 
        if(n%2 == 0){ 
            // The first thing missing. You didn´t change n the condition is ok
            // By calling n/2 you change n as your requirement says. Devide n by two if it is even
            n/=2; // We assign and divide n by two in one step by using /=
            ++even; 
        }
        else{
            // A small syntax mistake. you code said n = 3n+1
            // But 3n isn´t known to the java compiler and you would need to tell java what mathematical operation you would like to have here. 
            // That´s where 3*n is valid.
            n = 3*n+1; 
        }
        // The next thing is here. you said you would need to have the total
        // amount of numbers after each iteration. so you just need to 
        // increase it buy one after each iteration. One iteration would be one mathematical operation on your number. 
        ++count; 
        System.out.println("n is now " + n); 
    }
    System.out.println("we had " + even + " even numbers");
    System.out.println("we had " + count + " total numbers");
    // Now that you keep track of both, the total amount of numbers and the even numbers you would need to simply subtract them from each other to get your result.
    System.out.println("Resulting in total - even as:" + (count-even)); // Just subtract and print out your expected Output.
}