求救

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

Wwei0909 @ 2023-02-13 16:03:11


import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        int sum1=0,sum2=0,count1=0,count2=0,i;
        float aver1=0,aver2=0;
        for(i=1;i<=n;i++){
            if(i%k==0){
                sum1+=i;
                count1++;
            }else if(i%k!=0){
                sum2+=i;
                count2++;
            }
        }if(count1==0&&count2!=0){
            aver1=sum1;
            aver2=sum2/count2;
        }if(count2==0&&count1!=0){
            aver2=0;
            aver1=sum1/count1;
        }if(count1!=0&&count2!=0){
        aver1=sum1/count1;
        aver2=sum2/count2;}
        System.out.printf("%.1f %.1f",aver1,aver2);
    }
}

by XIAOHAOYU1 @ 2023-03-17 16:24:25

int n,k;
double ap=0,bp=0,as=0,bs=0;
cin >> n >> k;
for(int i = 1; i <= n; i++)
{
    if(i % k == 0) 
    {
        ap += i;
        as++;
    }
    else
    {
        bp += i;
        bs++;
    }
}
cout << fixed << setprecision(1) << ap / as << " " << bp / bs;

by XIAOHAOYU1 @ 2023-03-17 16:24:57

试试


|