我正在尝试解决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;
}
}
}
答案 0 :(得分:3)
根据说明:
输入的第一行包含正整数C(0
您的程序不采用此输入格式。您的程序需要在3个单独的行上使用K,low和high的单个测试用例。它应该采用多个测试用例,每个测试用例在一行上有K,低和高。
答案 1 :(得分:0)
好的,我做到了。现在它的工作方式与描述完全相同。它仍然说 - 错误的答案?
import java.util.Scanner;
class Exercise
{
Scanner sc = new Scanner(System.in);
int divisor;
int count = 0;
int numbers = 0;
int low;
int high;
int test = 0;
public void getInput()
{
test = sc.nextInt();
for (int i = 1; i <= test; i++)
{
get();
}
}
public void get()
{
sc.nextLine();
divisor = sc.nextInt();
low = sc.nextInt();
high = sc.nextInt();
calculate();
numbers = 0;
}
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);
}
}
public static void main(String[] args)
{
Exercise obj = new Exercise();
try
{
obj.getInput();
}
catch(Exception e)
{
return;
}
}
}