0分,求助!

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

xiao_tuan_zi @ 2024-01-22 09:31:51

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k;
    int sum;
    scanf("%d%d",&n,&k);
    for(int i=k;i<=n;i+=k)
        sum+=i;
    printf("%.1f ",double(sum)/(n/k));
    sum=(1+n)*n/2-sum;
    printf("%.1f",double(sum)/(n-n/k));
    return 0;
}

by lizexuanaibiancheng @ 2024-01-22 09:34:34

你的sum变量没有初始值,你改一下试试。


by CeceliaZXW @ 2024-01-22 09:37:07

sum没有初始值,可以把sum赋值为0,否则是计算机给你随意赋值


by lizexuanaibiancheng @ 2024-01-22 09:38:09

对的,改一下就能过了,这是我的提交信息:我的提交信息

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k;
    int sum=0;
    scanf("%d%d",&n,&k);
    for(int i=k;i<=n;i+=k)
        sum+=i;
    printf("%.1f ",double(sum)/(n/k));
    sum=(1+n)*n/2-sum;
    printf("%.1f",double(sum)/(n-n/k));
    return 0;
}

by dzl0620 @ 2024-01-23 10:48:53

sum变量


|