SOS 测试没错 但40分

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

zlzlzlzlzl_ @ 2024-12-09 18:43:37

#include <stdio.h>
int main(){
    int count1=0,count2=0;
    float sum1=0.0,sum2=0.0;
    int chushu,n;
    scanf("%d",&n);
    scanf("%d",&chushu);
    for(int i=1;i<=n;i++){
        if(i%chushu==0){
            sum2+=i;
            count2++;
        }
        else{
            sum1+=i;
            count1++;
        }
    }
    printf("%.1f %.1f",sum2/count2,sum1/count1);
    return 0;
}

求助


by xiezt123456 @ 2024-12-09 19:04:38

@zlzlzlzlzl_改一下精度,求互关

#include <stdio.h>
int main(){
    int count1=0,count2=0;
    double sum1=0.0,sum2=0.0;//改一下精度就好了
    int chushu,n;
    scanf("%d",&n);
    scanf("%d",&chushu);
    for(int i=1;i<=n;i++){
        if(i%chushu==0){
            sum2+=i;
            count2++;
        }
        else{
            sum1+=i;
            count1++;
        }
    }
    printf("%.1f %.1f",sum2/count2,sum1/count1);
    return 0;
}

by zlzlzlzlzl_ @ 2024-12-09 22:51:41

@xiezt123456 感谢 关注你了 哥


by Jupiter_123 @ 2024-12-15 17:18:24

@xiezt123456 我也是楼主这样的问题,但明明float也都可以输出正确的,为什么一定要double型呢,在这道题的背景下应该对精度要求没那么高吧?


by xiezt123456 @ 2024-12-15 19:30:11

@Jupiter_123保留的六位没那么高精度,但保留小数可能有误差,也会丢失精度的


by Jupiter_123 @ 2024-12-16 13:04:20

@xiezt123456啊啊好的,谢谢大佬!


by ma_rui @ 2024-12-27 22:24:30

好简单啊

#include<bits/stdc++.h>
using namespace std;
int n,k,ansa,suma,ansb,sumb;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        if(i%k==0){
            ansa+=i;
            suma++;
        }else{
            ansb+=i;
            sumb++;
        }
    }
    cout<<fixed<<setprecision(1)<<1.0*ansa/suma<<" "<<fixed<<setprecision(1)<<1.0*ansb/sumb;
    return 0;
}

求互关


|