一个测试点都没过,看不出来哪里不对qaq

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

bluetored @ 2022-09-12 16:46:13

#include <stdio.h>
int main ()
{
    int n,k;
    scanf ("%d %d",&n,&k);
    int number [n-1];
    for (int i =0;i<n;i++)
    {
        int a=1;
        number[i]=a;
        a++;
    }
    int sum1=0,sum2=0,b=0,c=0;
    for (int i=0;i<n;i++)
    {
        if (number[i]%k==0)  {sum1 = sum1+number[i];
        b++;}
        else {sum2=sum2+number[i];c++;}
    }
    printf ("%.1f %.1f",1.0*sum1/b,1.0*sum2/c);
    return 0;
}

by zz_z2Spider @ 2022-09-12 16:50:16


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int n,k;
    double sum=0,a,b,sum2=0,tot=0,tot1=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            sum+=i;
            tot++;
        }
        else
        {
            sum2+=i;
            tot1++;
        }
    }
    a=sum/tot;
    b=sum2/tot1;
    printf("%.1lf",a);
    cout<<" ";
    printf("%.1lf",b);
    return 0;
}

by bluetored @ 2022-09-12 17:08:45

找到原因了各位,我把int a的声明放到for里面了,导致a的值一直是1,往往总是被小错误弄得焦头烂额qaq


|