求助,这段代码有问题吗,输出样例都是对的,但是0分

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

MysteryRiddle @ 2022-08-23 11:00:50

#include<iostream>
#include<iomanip>
using namespace std;

int main(){
    int n,k;
    double sum1=0,sum2=0,n1,n2;
    cin>>n>>k;
    for (int i=1;i<=n;i++){
        if(i%k==0){
            sum1+=i;
            n1++;
        }else{
            sum2+=i;
            n2++;
        }
    }
    cout<<fixed<<setprecision(1)<<sum1/n1<<" "<<sum2/n2;

    return 0;

}

by WA_sir @ 2022-08-23 11:04:56

@MysteryRiddle

n1和n2初始要赋值为0


by small_black_son @ 2022-08-23 11:05:53

吸氧AC


by zhm20120707 @ 2022-08-23 11:40:04

其实我也做了很多遍, 这道题你就错在了没把n1、n2赋值为0,我也附上AC代码吧.


- #include<bits/stdc++.h>//头文件就不说了
- using namespace std;
- int main(){
    double a,x=0,y=0,d=0,e=0;
    int b;
    cin>>a>>b;
    for(int i=1;i<=a;i++)
    {
      if(i%b==0)
      {
        x+=i;
        d++;
        }
      else
      {
        y+=i;
        e++;
       }
    }
    cout<<fixed<<setprecision(1)<<x/d<<' '<<y/e;
  return 0;
  }

|