求助!样例过不了!

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

Bilipaigen @ 2021-05-02 17:00:32

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

by _caiji_ @ 2021-05-02 17:11:17

@Bilipaigen

for (int i = n;i>1;i--)
//             ^~~

漏了 1


by ALWAYS_Lyz_Zqbx @ 2021-05-02 17:11:34

这位同学,这边建议您参考一下了的呢

#include<bits/stdc++.h>

using namespace std;
int n,k,cnt1,cnt2;
double sum1,sum2;
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 %.1lf",sum1/cnt1,sum2/cnt2);
    return 0;
}

by Bilipaigen @ 2021-05-03 11:30:29

@caijianhong 谢谢,改回来了


|