简体   繁体   English

找到所有快乐的数字,直到

[英]find all happy numbers until

My assignment is to print all the happy numbers from 10 until the program prints 3 consecutive numbers. 我的任务是从10开始打印所有快乐数字,直到程序打印3个连续数字。 Those 3 numbers should be 1880 1881 and 1882. i wrote while(num2<1883) just for the loop to stop. 那三个数字应该是1880 1881和1882。我写了while(num2 <1883)只是为了使循环停止。 i should compare the 3 last "sums" and stop the loop when they are consecutive. 我应该比较最后3个“和”,并在它们连续时停止循环。

do {
        num = num2;
        while (num > 0 || sum > 9) {
            if (num == 0) {
                num = sum;
                sum = 0;
            }
            sum += Math.pow(num % 10, 2);
            num /= 10;
        }
        if (sum == 1) {
            counter++;
            System.out.println(counter + ") " + num2 + " is a happy number :-)");
        }
        num2++;
        sum = 0;
    } while (num2 < 1883);//<---?????

You should have a List that contains consecutive found numbers. 您应该具有一个包含连续找到的数字的列表。 And loop until the length of this list is 3. Inside the loop check if your candidate is an happy number. 循环直到该列表的长度为3。在循环中检查您的候选人是否是一个快乐的数字。 If it is add it to the list. 如果是,请将其添加到列表中。 If it isn't clear the list since you haven't reached a consecutive happy number. 如果由于您尚未达到连续的快乐数字而无法清除列表。 After exiting the loop the List contains the three consecutive happy numbers found. 退出循环后,列表包含找到的三个连续的快乐数字。

List<Integer> foundNumbers = new ArrayList<Integer>();
int candidate = 10;
while(foundNumbers.length < 3) {
    if(isHappyNumber(candidate)) {
        foundNumbers.add(candidate);
        System.out.println(candidate + " is a happy number");
    } else {
        foundNumbers.clear();
    }
    candidate ++;
}
//Print the list members

The general format is: 通用格式为:

int lastHappyNumber = 0;
int currentHappyNumber = 0;
int thirdLastSum = 0;
int secondLastSum = 0;
int firstLastSum = 0;

do {
    currentHappyNumber = getNextHappyNumber();     // <--- fill in with your logic
    thirdLastSum = secondLastSum;
    secondLastSum = firstLastSum;
    firstLastSum = lastHappyNumber + currentHappyNumber;
    lastHappyNumber = currentHappyNumber;
} 
while ((thirdLastSum + 1) == secondLastSum && (secondLastSum + 1) == firstLastSum);

Since this is an assignment, I will leave computing the happy numbers to you, but the idea is that every iteration, you shift down the last 3 sums and then compare them. 由于这是一项任务,因此我将计算快乐数字交给您,但是想法是每次迭代时,您都将最后三个总和向下移,然后进行比较。 Once you find a valid solution (where the last three sums were consecutive integers), the value of lastHappyNumber holds the happy number that solves the problem. 找到有效的解决方案(最后三个和为连续的整数)后, lastHappyNumber的值将保存解决问题的快乐数。 lastHappyNumber = currentHappyNumber ensures that during the next iteration, the last happy number will be stored and allow for a sum computation to be performed. lastHappyNumber = currentHappyNumber确保在下一次迭代期间,将存储最后一个快乐数,并允许进行总和计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM