SPOJ“ ACPC10A-接下来是什么-答案错误

问题描述 投票:0回答:1

我尝试了以下代码,该代码与我尝试过的自定义输入配合使用,但是,它未被接受并显示-WRONG ANSWER

SPOJ-https://www.spoj.com/problems/ACPC10A/

import java.util.*;
import java.lang.*;

class Main
{
    public static void findProgression(int a, int b, int c){
        int diff1 = Math.abs(b-a);
        int diff2 = Math.abs(c-b);
        int nextNumber;
        if(diff1 == diff2){
            nextNumber = c + diff1;
            System.out.println("AP " + nextNumber);
        }else{
            nextNumber = c*b/a;
            System.out.println("GP " + nextNumber);
        }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        while(a != 0 || b != 0 || c != 0){
            findProgression(a,b,c);
            a = scan.nextInt();
            b = scan.nextInt();
            c = scan.nextInt();
        }       
    }
}
java algorithm math output
1个回答
0
投票

几乎没有什么要注意的:

1)应该按原样比较差异,而不是绝对值。

2)在计算GP的公共比率时要小心。使用类型转换。

3)解决方案的破坏条件。

4)整数溢出。

看看下面的实现:

import java.util.*;
import java.lang.*;

class Solution
{
    public static void findProgression(long a, long b, long c){
        long diff1 = b-a;
        long diff2 = c-b;
        long nextNumber;
        if(diff1 == diff2){
            nextNumber = c + diff1;
            System.out.println("AP " + nextNumber);
        }else{
            nextNumber = (long)((double)c*((double)c/(double)b));
            System.out.println("GP " + nextNumber);
        }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner scan = new Scanner(System.in);

        while(true){

            int a = scan.nextInt();
            int b = scan.nextInt();
            int c = scan.nextInt();

            if(a==0 && b==0 && c==0)break;

            findProgression(a, b, c);

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.