求助,为什么RE!

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

lizhehao @ 2024-01-25 22:09:42

源代码如下,为什么RE?

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k,j1=0,j2=0;
    long long sum1=0,sum2=0;
    cin>>n,k;
    for(int i=1;i<=n;i++){
        if(i%k==0){
            sum1+=i;
            j1++;
        }
        if(i%k==1){
            sum2+=i;
            j2++;
        }
    }
    long double s1=sum1/j1*1.0;
    long double s2=sum2/j2*1.0;
    cout<<setprecision(1)<<fixed<<s1<<" ";
    cout<<setprecision(1)<<fixed<<s2;
    return 0;
} 

by __yun__ @ 2024-01-25 22:14:46

第6行应该为cin>>n>>k;


by __yun__ @ 2024-01-25 22:16:54

第12行if的条件应该是i%k!=0


by __yun__ @ 2024-01-25 22:17:39

第17行应该是s1=sum1*1.0/j1,第18行同理


by __yun__ @ 2024-01-25 22:17:54

@lizhehao


by __yun__ @ 2024-01-25 22:19:08

改好的代码如下

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k,j1=0,j2=0;
    long long sum1=0,sum2=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        if(i%k==0){
            sum1+=i;
            j1++;
        }
        if(i%k!=0){
            sum2+=i;
            j2++;
        }
    }
    long double s1=sum1*1.0/j1;
    long double s2=sum2*1.0/j2;
    cout<<setprecision(1)<<fixed<<s1<<" ";
    cout<<setprecision(1)<<fixed<<s2;
    return 0;
} 

by lizhehao @ 2024-01-27 13:44:54

感谢大犇!!!


|