运用if和for,例子的答案正确,但只能得40分,求大佬帮忙看看错在哪里。谢谢!

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

jinhai @ 2020-10-08 16:39:31

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,m[1000],i,j;
    double c,d,e,f,num=0,sum=0;
    cin>>a>>b;
    for(i=1;i<=a;i++)
    {
        m[i]=i;
    }
    c=a/b;
    i--;
    f=i-c;
    for(j=1;j<=a;j++)
    {
        if(m[j]%b==0)
        {
            num+=m[j];
        }
    }
    d=num/c*1.0;
    for(int t=1;t<=a;t++)
    {
        sum+=m[t];
    }
    e=(sum-num)/f*1.0;
    cout<<fixed<<setprecision(1)<<d<<" "<<e;
}

by 清清老大 @ 2020-10-08 16:42:47

数组开小了


by hanyuchen2019 @ 2020-10-08 16:43:20

n \le 10000

by jinhai @ 2020-10-08 17:05:11

谢谢!


|