20分,pascal(c++)蒟蒻求助

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

AIMEE11 @ 2022-08-01 12:41:29

#include<iostream>
using namespace std;
int main()
{
    int n,k;
    double sa=0,a=0,sb=0,b=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            sa++;
            a+=i;
        }
        else
        {
            sb++;
            b+=i;
        }
    }
    cout<<(double)a/sa<<" "<<(double)b/sb;
}

by wangjunjie2020 @ 2022-08-01 12:51:34

精确到小数点后一位


by wangjunjie2020 @ 2022-08-01 12:54:15

@AIMEE11


by hanzhang666 @ 2022-08-01 12:54:31

精确到小数点后一位。

@AIMEE11


by _technoblade_ @ 2022-08-04 21:58:58

#include<bits/stdc++.h>
using namespace std;
int main()
{
    double n,cnt,cnt2,s,x,ans1,ans2;
    int i,k;
    cin>>n>>k;
    s=x=cnt=cnt2=ans1=ans2=0;
    for(i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            cnt++;
            s+=i;
        }
        else
        {
            cnt2++;
            x+=i;
        }
    }
    ans1=s*1.0/cnt;
    ans2=x*1.0/cnt2;
    printf("%.1f",ans1);
    cout<<" ";
    printf("%.1f",ans2);
    return 0;
}

by AIMEE11 @ 2022-08-07 10:13:10

@technoblade 谢谢,我现在已经AC了


|