0分求助

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

lake_tree @ 2021-09-15 20:13:41

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c1;
    cin>>a>>b;
    float c=0,d=0;
    for(int i=1;i<=a;i++){
        if(i%b==0){
            c+=i;
            c1++;
        }else if(i%b!=0){
            d+=i;
        } 
    }
    cout<<fixed<<setprecision(1)<<(float)c/c1<<" ";
    cout<<fixed<<setprecision(1)<<(float)d/(a-c1);
    return 0;
}

by B天选之子B @ 2021-09-15 20:18:24

你试试用float 定义c1@lake_tree


by lake_tree @ 2021-09-15 20:21:58

@B天选之子B 成了40分……


by B天选之子B @ 2021-09-15 20:26:21

@lake_tree 现在你做出来了吗?


by lake_tree @ 2021-09-15 20:33:52

@B天选之子B 改成double就过了


by B天选之子B @ 2021-09-15 20:34:12

a也要用float @lake_tree


by B天选之子B @ 2021-09-15 20:35:12

@lake_tree

我float 也过了QAQ


by B天选之子B @ 2021-09-15 20:35:40

#include<bits/stdc++.h>
using namespace std;
int n,k;
float ans,js;
int read() {
    int x=0,f=1;
    char ch=getchar();
    for(; ch<'0'||ch>'9'; ch=getchar())if(ch=='-')f=-1;
    for(; ch>='0'&&ch<='9'; ch=getchar())x=(x<<3)+(x<<1)+(ch^48);
    return x;
}
int main() {
    n=read(),k=read();
    for(register int i=k; i<=n; i+=k) ans+=i,js++;
    printf("%.1lf %.1lf",ans/js,(double)((1+n)*n/2-ans)/(n-js));
    return 0;
}

|