求指正

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

YSzita @ 2023-08-01 22:00:34

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,t=0,t1=0,s=0,s1=0;
    double d,d1;
    cin>>a>>b;
    for(int i=1;i<=a;i++){
        if(i%b==0){
            t+=i;
            s++;
        }else{
            t1+=i;
            s1++;
        }
    }
    d=t/s;
    d1=t1/s1;
    cout<<cout<<setprecision(1)<<std::fixed<<d<<" ";
    cout<<cout<<setprecision(1)<<std::fixed<<d1;
    return 0;
}

by Rieman_sum @ 2023-08-01 22:03:04

@YSzita 经典错误,d=t/sd=t*1.0/s下一个同样


by Rieman_sum @ 2023-08-01 22:04:43

double=int/int是非常经典的错误,要多乘一个1.0


by wzb13958817049 @ 2023-08-01 22:09:18

@YSzita 不懂就问cout<<cout是什么意思

应该为

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,t=0,t1=0,s=0,s1=0;
    double d,d1;
    cin>>a>>b;
    for(int i=1;i<=a;i++){
        if(i%b==0){
            t+=i;
            s++;
        }else{
            t1+=i;
            s1++;
        }
    }
    d=t*1.0/s;
    d1=t1*1.0/s1;
    cout<<fixed<<setprecision(1)<<d<<" ";
    cout<<fixed<<setprecision(1)<<d1;
    return 0;
}

AC记录


by YSzita @ 2023-08-01 22:10:12

@Guo1 复制错了


|