有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java寻找奇数除数

我正在试图解决一个关于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;
        }
    }   
}

共 (2) 个答案

  1. # 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;
         }
    
     }  
    

    }

  2. # 2 楼答案

    根据说明:

    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.

    您的程序不采用此输入格式。您的程序需要一个单独的测试用例,在3行上分别显示K、low和high。它应该采取多个测试用例,每个测试用例在一行上都有K、low和high