求助!!!

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

CatLord @ 2022-07-08 18:00:20


#include<bits/stdc++.h>
using namespace std;
int main()     
{
   int n,k;
   double b=0,c=0,d,e,f,g;
   cin>>n>>k;
   for(int i=1;i<=n;i++)
   {
    if(i%k==0)
    {
      b=b+i;
      d++;
       }
       else
       {
        c=c+i;
        e++;
       }
   }
   f=b/d;
   g=c/e;
   cout<<fixed<<setprecision(1)<<f<<" "<<g;
    return 0; 
}

                               本人代码样例和测试点都符合,但却是WA,求助

by wizard(偷开O2 @ 2022-07-08 18:10:57

-nan


by Blikewsr @ 2022-07-08 18:20:20

@CatLord

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k, tot_one, tot_two, num_one, num_two;
double ans_one, ans_two;
signed main () {
    cin >> n >> k;
    for (int i = 1; i <= n; ++ i) {
        if (!(i % k)) {
            tot_one += i;
            ++ num_one;
        }
        else {
            tot_two += i;
            ++ num_two;
        }
    }
    ans_one = 1.0 * (tot_one * 1.0 / num_one);
    ans_two = 1.0 * (tot_two * 1.0 / num_two);
    printf ("%.1lf %.1lf\n", ans_one, ans_two);
    return 0;
}   

by Blikewsr @ 2022-07-08 18:27:07

@CatLord

你的 double d, e, f, g 没有初始化, 根据你的代码, 我修改了一下, 不喜勿碰, 谢谢!

#include <bits/stdc++.h>
using namespace std;
int n, k;
double b, c, d, e, f, g;
int main () {
    cin >> n >> k;
    for (int i = 1; i <= n; ++ i) {
        if(i % k == 0) {
            b += i;
            ++ d;
        }
        else {
            c += i;
            ++ e;
        }
    }
    f = b / d;
    g = c / e;
    cout << fixed << setprecision (1) << f << ' ' << g << '\n';
    return 0; 
}

|