Dear123 @ 2023-11-17 21:25:46
测试用例没问题,不知道是哪一步出问题了,求助大佬
#include <iostream>
using namespace std;
int main(){
int n,k,a=0,b=0;
float sum1,sum2;
cin>>n>>k;
for(int i=1;i<=n;i++){
if(i%k==0) {
sum1+=i;
a++;
}
else {
sum2+=i;
b++;
}
}
printf("%.1f %.1f",sum1/a,sum2/b);
}
by penguin_is_cool @ 2023-11-17 21:37:27
AC代码:
#include <iostream>
using namespace std;
int main(){
int n,k;
double sum=0,sum1=0,c,d;
cin>>n>>k;
for (int i=1;i<=n;i++){
if (i%k==0){
sum+=i;
c++;
}
else{
sum1+=i;
d++;
}
}
printf("%.1f %.1f",sum/c,sum1/d);
}
@Dear123
by penguin_is_cool @ 2023-11-17 21:38:28
@Dear123 你的sum1和sum2要初始化,全为零
by Dear123 @ 2023-11-19 00:18:45
@penguin_is_cool 好的,谢谢!