【发布时间】:2018-10-22 17:20:12
【问题描述】:
我目前正在尝试创建一种算法,该算法应该能够将数字与特定数字总和对齐。例如在 1 到 500 之间的数字和为 13 的数字。有人可以给我一些关于如何进行的建议吗?
我目前的进度:
public class Digitsum{
static int Digitsum2(int number) { //method to find out the digit sum from numbers between 1 and 500
int sum = 0;
if(number == 0){
return sum;
} else {
sum += (number%10);
Digitsum2(number/10);
}
return sum;
}
static int Digitsum13(int x) { //method to line up the numbers with a digit sum of 13
while (x <= 500) {
x++;
Digitsum2(x);
}
return x;
}
public static void main(String[ ] args) {
Digitsum13(13);
}
}
【问题讨论】:
-
能否请您添加更多示例(不是在代码中,只是输入输出)
-
“排队”是什么意思?
-
输入:Digitsum13(20);输出:1 到 500 之间的每个数字,数字总和为 20
-
排队 = System.out.println(1-500 之间的数字,数字总和为 13)