猿问

无法理解这种在线判断的解决方案是如何出错的

我正在尝试解决似乎是一个相对简单的问题,输入孩子的数量和糖果的总数,如果糖果可以在孩子之间平均分配,则打印“是”,否则打印“否” .

从图中可以看出,前两个测试用例通过了,而最后一个没有通过。我在这里挠头,有什么想法吗?

import java.util.*;

public class AnotherCandies {


    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);


        int cases = sc.nextInt();


        for(int i = 0; i < cases; i++)

        {

            long numKids = sc.nextLong();

            long total = 0;


            for(int j = 0; j < numKids; j++)

            {

                long n = sc.nextLong();

                total += n;

            }


            if(total % numKids == 0)

                System.out.println("YES");

            else

                System.out.println("NO");

        }


    }


}


侃侃无极
浏览 170回答 2
2回答

慕哥9229398

你有长超载总和的问题。您可以%在每一步使用,然后再做一次以获得最终总和:public static void main(String[] args) {&nbsp; &nbsp; try (Scanner scan = new Scanner(System.in)) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0, T = scan.nextInt(); i < T; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int N = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < N; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = (int)(scan.nextLong() % N + sum) % N;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(N == 0 ? "YES" : "NO");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕运维8079593

我认为你需要检查案件total=0。也许在这种情况下你应该打印否?if(total == 0)&nbsp; &nbsp; &nbsp;System.out.println("NO");else if(total % numKids == 0)&nbsp; &nbsp; &nbsp;System.out.println("YES");else&nbsp; &nbsp; &nbsp;System.out.println("NO");
随时随地看视频慕课网APP

相关分类

Java
我要回答