为社么我这个代码只得40分?

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

coderzhx @ 2024-06-10 18:53:48

#include <iostream>
using namespace std;

int n,k;
int sum1,sum2;
int cnt1,cnt2;

int main() {
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            sum1+=i;
            cnt1++;
        }
        else
        {
            sum2+=i;
            cnt2++;
        }
    }
    printf("%.1lf",double(sum1/cnt1));
    cout<<" ";
    printf("%.1lf",double(sum2/cnt2));

    return 0;
}

by Free_Duck @ 2024-06-10 19:08:59

#include <iostream>
using namespace std;

int n,k;
int sum1,sum2;
int cnt1,cnt2;

int main() {
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            sum1+=i;
            cnt1++;
        }
        else
        {
            sum2+=i;
            cnt2++;
        }
    }
    printf("%.1lf",double(sum1*1.0/cnt1));//得乘1.0,不然就直接取整了
    cout<<" ";
    printf("%.1lf",double(sum2*1.0/cnt2));

    return 0;
}

by Dream_0_0 @ 2024-06-10 19:28:22

要 *1.0,不然直接取整,对的40分就是测试点输出为整数的,不然你就全错


by LDD0814250 @ 2024-07-18 11:20:19

因为sum和sum2和cnt1和cnt是int, AC得是double型的


by LDD0814250 @ 2024-07-18 11:22:31

@LDD0814250 double sum1,sum2; double cnt1,cnt2; printf("%0.1f",sum1/cnt1); printf("%0.1f",sum2/cnt2);


by limuchudsb @ 2024-07-21 09:23:55

用double


by limuchudsb @ 2024-07-21 09:25:40

c```cpp



#include<bits/stdc++.h>
using namespace std;
int main()
{
    double sum=0,ant=0,a=0,b=0;//用double 
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            sum=sum+i;
            ant++;
        }
        else
        {
            a=a+i;
            b++;
        }
    }
    cout<<fixed<<setprecision(1)<<sum/ant<<' '<<a/b;
    return 0;
}

|