gpt4 book ai didi

java - 查找奇数除数

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:45 26 4
gpt4 key购买 nike

我正在尝试解决 this page 上的问题.我想我已经解决了它,因为它给了我正确的结果,但是当我在网页中提交代码时,它给出了错误的答案。我不明白我的错误在哪里。你能帮帮我吗?

class Exercise 
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int divisor;
int count = 0;
int numbers = 0;
int low;
int high;

// Get the input of the user
public void getInput() throws IOException
{
System.out.println("Please enter k, low and high");
String line1 = input.readLine();
String line2 = input.readLine();
String line3 = input.readLine();

divisor = Integer.parseInt(line1);
low = Integer.parseInt(line2);
high = Integer.parseInt(line3);
}

// Finding number of odd divisors for each number
public void calculate()
{
if ((divisor % 2 != 0)) {
for (int k = low; k <= high; k++) {
count = 0;
for (int j = 1; j <= k; j++) {
if (k % j == 0) {
count++;
}
}
if (count == divisor) {
numbers++;
}
}
System.out.println(numbers);
}
else
System.out.println("Sorry. The divisor should be an odd number. Please try again.");
}

public static void main(String[] args)
{
Exercise obj = new Exercise();
try {
obj.getInput();
obj.calculate();
}
catch(Exception e) {
return;
}
}
}

最佳答案

来自说明:

The first line of the input contains a positive integer C (0 < C < 100,000), the number of test cases to follow. Each case consists of a line containing three integers: K, low, and high (1 < K<10000, 0 < low ≤ high < 10^10). K will always be an odd integer.

您的程序不采用这种输入格式。您的程序需要一个单独的测试用例,其中 K、低和高分别位于 3 行。它应该采用多个测试用例,每个用例在一行中包含 K、低和高。

关于java - 查找奇数除数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com