首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JAVA中添加约束和条件?

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

Stack Overflow用户
提问于 2015-11-16 22:32:28
回答 1查看 1.5K关注 0票数 0

所以我前段时间删除了我的帖子,现在我将向你展示我目前的工作。基本上,我是一个新手,我希望你能帮我解决这个问题,因为我正在苦苦思索下一步该做什么。

代码语言:javascript
复制
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问题/Collatz猜想,这里有一些条件。

对于输入N,N的周期长度是生成的直到并包括1的数字的数量。在上面的示例中,22的周期长度是16。给定一个数字N,您需要确定N的周期长度与从N开始的序列生成的偶数的数量之间的绝对差。

约束条件:

代码语言:javascript
复制
1 <= T <= 100000
1 <= N <= 1000000

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

代码语言:javascript
复制
4
10
34
22
18237

样本输出

代码语言:javascript
复制
2
4
5
55

测试用例说明N= 10,生成的序列为10 -5- 16 -8-4- 2 -1,循环长度为7,序列上有5个偶数,分别为10,16,8,4,2,答案为7-5 =2。

EN

回答 1

Stack Overflow用户

发布于 2015-11-16 22:37:58

那么,您在代码中遗漏了什么呢?看看这个。您需要在每次迭代中更改n。根据您的要求,这需要对偶数和奇数做不同的处理。这就是你在整个循环中所遗漏的。另一方面,您跟踪发生的偶数的数量,但您忘记的是跟踪在循环中调用的操作的总量。

下面是你的代码是什么样子的,还有一些注释作为解释。

代码语言:javascript
复制
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.
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33737814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档