c++上编译成功,洛谷上为什么编译失败

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

__Real_Madrid__ @ 2024-08-14 20:10:01

为什么洛谷上编译不过QaQ

#include <bits/stdc++.h>
using namespace std;
int main(){
    long long n,k,c=0,d=0;
    double ans1,ans2,a=0,b=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
    if(i%k==0){
        a+=i;
        c+=1;
    }
    if(i%k!=0){
        b+=i;
        d+=1;
    }}
    cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
    ans1=a/c;
    ans2=b/d;
    printf("%.1lf",ans1);
    cout<<" ";
    printf("%.1lf",ans2);
    return 0;
}

by FurippuWRY @ 2024-08-14 20:11:18

你自己粘贴的问题。


by haley001 @ 2024-08-14 20:12:51

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long n, k, c = 0, d = 0;
    double ans1, ans2, a = 0, b = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        if (i % k == 0) {
            a += i;
            c += 1;
        }
        if (i % k != 0) {
            b += i;
            d += 1;
        }
    }
    //cout << a << " " << b << " " << c << " " << d << '\n';
    ans1 = a / c;
    ans2 = b / d;
    printf("%.1lf %.1lf", ans1,ans2);
    return 0;
}

这样就可以了


by Orz_Sponge_Bob @ 2024-08-14 20:12:54

c++上成功只是过了样例,我以前也这样。


by 幻想繁星 @ 2024-08-14 20:17:43

@Orz_Sponge_Bob 幽默


by 幻想繁星 @ 2024-08-14 20:18:20

@__Real_Madrid__ 幽默


by __Real_Madrid__ @ 2024-08-14 20:20:09

谢谢!!已关


by Acerkaio @ 2024-08-14 20:21:00

@幻想繁星 幽默


by 幻想繁星 @ 2024-08-14 20:21:58

@Acerkaio 幽默


by zhizhenhuyuzhe @ 2024-08-14 20:22:09

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

by __Real_Madrid__ @ 2024-08-14 20:22:36

@haley002 还真过了


|