求指正

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

SDFZ_SHH @ 2023-10-14 19:30:39

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, k, q, w, e, r;
    double a, b;
    cin >> n >> k;
    for (int i = 1; i >= n; i++)
    {
        if (i % k == 0)
        {
            q += i;
            e++;
        }
        else
        {
            w += i;
            r++;
        }
    }
    a = q / e;
    b = w / r;
    printf("%.1lf %.1lf",a,b);
    return 0;
}

by chenlongyu123 @ 2023-10-15 10:36:17



#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,ans=0,ansk=0,sum=0,sumk=0;
    double aver,averk;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        if(i%k==0){
            sumk=sumk+i;
            ansk++;
        }
        if(i%k!=0){
            sum=sum+i;
            ans++;
        }
    }
    averk=sumk*1.0/ansk;
    aver=sum*1.0/ans;
    cout<<fixed<<setprecision(1)<<averk<<" "<<aver;
    return 0;
} ```

by chenlongyu123 @ 2023-10-15 10:37:10

我是这样写的```


by IAKIOIpwp @ 2023-10-18 20:59:00

#include <bits/stdc++.h>
using namespace std;
int n, k, q, w, e, r;//2
double a, b;//2
int main()
{

    cin >> n >> k;
    for (int i = 1; i <= n; i++)//3
    {
        if (i % k == 0)
        {
            q += i;
            e++;
        }
        else
        {
            w += i;
            r++;
        }
    }
    a =(double) q / e;//1
    b = (double)w / r;//1
    printf("%.1lf %.1lf",a,b);
    return 0;
}

AC

首先,int 型相除结果是整数(见1)

其次,变量在int main中不一定会是0,需移到int main外,设为全局变量(见2)

最后,改一个小错误--------把i>=n改为i<=n(见3)

这就AC了!!!


|