Java 求因子的奇数
我正在努力解决一个正在进行的问题。我想我已经解决了,因为它给了我正确的结果,但当我在网页上提交代码时,它说的是错误的答案。我不明白我的错误在哪里。你能帮帮我吗Java 求因子的奇数,java,algorithm,Java,Algorithm,我正在努力解决一个正在进行的问题。我想我已经解决了,因为它给了我正确的结果,但当我在网页上提交代码时,它说的是错误的答案。我不明白我的错误在哪里。你能帮帮我吗 class Exercise { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int divisor; int count = 0; int numbers = 0; int low;
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;
}
}
}
课堂练习
{
BufferedReader输入=新的BufferedReader(新的InputStreamReader(System.in));
整数除数;
整数计数=0;
整数=0;
int低;
int高;
//获取用户的输入
public void getInput()引发IOException
{
System.out.println(“请输入k、低和高”);
字符串line1=input.readLine();
字符串line2=input.readLine();
字符串line3=input.readLine();
除数=整数.parseInt(第1行);
低=整数.parseInt(第2行);
高=整数.parseInt(第3行);
}
//查找每个数的奇数因子数
公共空间计算()
{
如果((除数%2!=0)){
对于(int k=低;k来自说明:
输入的第一行包含一个正整数C(0 包含三个整数的行:K,low和high(1
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;
}
}
import java.util.Scanner;
课堂练习
{
扫描仪sc=新的扫描仪(System.in);
整数除数;
整数计数=0;
整数=0;
int低;
int高;
int检验=0;
public void getInput()
{
测试=sc.nextInt();
对于(int i=1;i)你的代码要求的输入格式与他们给你的不同。不知道这是否是唯一的问题。数学提示:只有平方数的除数是奇数。你可能需要考虑到这一点,以使你的程序足够快,足以处理大型测试用例。嗯。好的,我应该如何更正我的代码以满足要求e?任何人都可以发布代码片段,因为我显然不知道如何更正它。@user3054367:我已经向您展示了问题。解决方案取决于您。否则,您将如何学习解决这些问题?@user3054367:提示:循环输入行,解析每个测试用例,并针对每个测试用例运行计算代码。甚至这个answer没有解释您需要修复的内容。他的程序已经在3行上检查了K、low和high,就像它说您必须这样做一样。它没有说您必须在任何地方循环输入。@SSpoke:不,OP当前是这样做的,而不是他应该怎么做的。K、low和high不在3行上进行。