华为OD机试真题 - 用连续自然数之和来
列方程 double L= (num * 2 + i - i * i) / 4.0;
如果 L 是整数就存在
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
System.out.println(getReslut(num));
}
public static int getReslut(int num) {
int count = 1;
for (int i = 1; i <= num / i; i++) {
double temp = (num * 2 + i - i * i) / 4.0;
if (temp == (int)temp)
count++;
}
return count;
}
如果 L 是整数就存在
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
System.out.println(getReslut(num));
}
public static int getReslut(int num) {
int count = 1;
for (int i = 1; i <= num / i; i++) {
double temp = (num * 2 + i - i * i) / 4.0;
if (temp == (int)temp)
count++;
}
return count;
}
全部评论
相关推荐