怎么错了?

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

Konnyaku_q @ 2023-04-20 17:19:04

rt

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
long long n,k,ans,sum,d,SUM,ANS,D; 
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            sum+=i;
            ans++;
        }
        else
        {
           SUM+=i;
           ANS++;   
        }

    }
    d=sum/ans;
    D=SUM/ANS;
    printf("%0.1f",d);
    cout<<" "; 
    printf("%0.1f",D);

}

by cn_ryh @ 2023-04-20 17:27:34

写个 d=(double)sum/ans

然后 Dddouble类型的


by cn_ryh @ 2023-04-20 17:27:57

@wzbxx 补


by Konnyaku_q @ 2023-04-20 17:32:25

补了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
long long n,k,ans,sum,SUM,ANS;
double d,D;  
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            sum+=i;
            ans++;
        }
        else
        {
           SUM+=i;
           ANS++;   
        }

    }
    d=sum/ans;
    D=SUM/ANS;
    printf("%0.1f",d);
    cout<<" "; 
    printf("%0.1f",D);

}

by Konnyaku_q @ 2023-04-20 17:33:42

@cn_ryh but少了0.1


by cn_ryh @ 2023-04-20 17:34:28

@wzbxx 下面写 d = (double)sum/ans,另一个也是


|