40分

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

hushicheng888 @ 2024-08-23 13:32:31

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,f,c=0,d=0;
    float a=0,b=0;
    cin>>n>>f;
    for(int i=1;i<=n;i++)
    {
        if(i%f==0)
        {
            a=a+i;
            c++;
        }
        else
        {
            b=b+i;
            d++;
        }

    }
    a=a/c;
    b=b/d;
    printf("%.1f %.1f",a,b);
    return 0;
}

by pmkmzfuzsotqotmzs @ 2024-08-23 13:46:03

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    double sum1=0,sum2=0;
    int l1=0,l2=0;
    for(int i=1;i<=a;i++)
    {
        if(i%b==0)
        {
            sum1+=i;
            l1++;
        }
        else
        {
            sum2+=i;
            l2++;
        }
    }
    cout<<fixed<<setprecision(1)<<sum1/l1<<" ";
    cout<<fixed<<setprecision(1)<<sum2/l2<<endl;
    return 0;
}

by _zlh4_Fishrow_ @ 2024-08-23 13:58:30

@hushicheng888 注意看题目样例,答案可以是小数,所以建议把变量类型改为double


by hushicheng888 @ 2024-08-23 14:46:30

@pmkmzfuzsotqotmzs 谢谢


|