为什么全是WA!!!

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

abababwosb @ 2024-08-30 10:12:30

#include <bits/stdc++.h>
using namespace std;

int main() {
    double ev1, ev2, cnt1 = 0, cnt2 = 0;
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        if (i % k == 0) {
            cnt1++;
            ev1 += i;
        } else {
            cnt2++;
            ev2 += i;
        }
    }
    ev1 /= cnt1;
    ev2 /= cnt2;
    printf("%.1f", ev1);
    cout<<" ";
    printf("%.1f", ev2);
    return 0;
}

by zhaicy @ 2024-08-30 10:17:42

#include <bits/stdc++.h>
using namespace std;

int main() {
    double ev1, ev2, cnt1 = 0, cnt2 = 0;
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        if (i % k == 0) {
            cnt1++;
            ev1 += i;
        } else {
            cnt2++;
            ev2 += i;
        }
    }
    ev1 /= cnt1;
    ev2 /= cnt2;
    printf("%.1lf", ev1);
    cout<<" ";
    printf("%.1lf", ev2);
    return 0;
}

你用的是double用lf


by JMY111213 @ 2024-08-30 10:18:13


#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 lzm0107 @ 2024-08-30 10:36:13

@zhaicy double怎么就lf了


by lzm0107 @ 2024-08-30 10:39:37

@zhaicy 这个明显局部变量没初始化,和 %f%lf 有半点关系吗?不懂可以不用回答的


by zhaicy @ 2024-08-30 10:40:04

@lzm0107 float用printf写是f,double用printf不是lf吗


by lzm0107 @ 2024-08-30 10:40:59

@zhaicy

https://zh.cppreference.com/w/cpp/io/c/fprintf


by zhaicy @ 2024-08-30 10:42:30

@lzm0107

难道不是这样写吗

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k;
    double a,b,suma1=0,sumb1=0,suma2=0,sumb2=0; 
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            suma1++;
            suma2+=i;
        }
        else
        {
            sumb1++;
            sumb2+=i;
        }
    }
    a=suma2/suma1;
    b=sumb2/sumb1;
    printf("%.1lf %.1lf",a,b);
    return 0;
}

by lzm0107 @ 2024-08-30 10:51:06

@zhaicy 这样当然可以啊,但是lz又不是错在这个


by llhhss @ 2024-09-05 21:10:37

@abababwosb

#include<iostream>
#include<cstdio>
using namespace std;
int n,k,cnt1=0,cnt2=0;
double ans1,ans2,sum1,sum2;
int main(){
    cin>>n;
    cin>>k;
    for(int i=1;i<=n;i++){
        if(i%k==0){
            sum1+=i;
            cnt1++;
        }else{
            sum2+=i;
            cnt2++;
        }
    }
    ans1=sum1/cnt1;
    ans2=sum2/cnt2;
    printf("%.1lf %.1lf",ans1,ans2);
    return 0;
}

|