【java】萌新求助,为啥样例只能输出50.0而不是50.1

P5719 【深基4.例3】分类平均

Tachibana_Yuki @ 2022-01-06 16:50:51

代码如下 </hr>

import java.util.*;
class Main {
    private static Scanner input = new Scanner(System.in);
    public static void main(String []args) {
        int n = input.nextInt();
        int k = input.nextInt();
        int sum1 = 0,sum2 = 0,cnt = 0;      
        for(int i = 1;i <= n;i += 1) {
            if(i % k == 0) {
                System.out.println(i);
                sum1 += i;
                cnt += 1;
            } else {
                sum2 += i;
            }
        }
        double res1 = sum1 / cnt;
        double res2 = sum2 / (n - cnt);
        // System.out.printf("%d %d\n",cnt,(n - cnt));
        System.out.printf("%.1f %.1f\n",res1,res2);
        input.close();
    }
}

by Tom俩 @ 2022-01-06 16:53:21

@Tachibana_Yuki 好好读题


by _Haoomff_ @ 2022-01-06 17:36:35

@Tachibana_Yuki A 类数可以被 k 整除(也就是说是 k 的倍数),而 B 类数不能,所以还要定一个cnt2,用来存B类数的个数


|