**PAT(Basic Level) 1012 數字分類
阿新 • • 發佈:2020-07-24
我的錯誤程式碼:
1 #include<iostream> 2 #include <iomanip> 3 4 using namespace std; 5 6 int main() 7 { 8 int N; 9 cin>>N; 10 11 int n; 12 int j=1; 13 int sum0=0,yu0=0,sum1=0,yu1=0,yu2=0,yu3=0,sum3=0,max4=-1,yu4=0; 14 for(int i=0;i<N;i++){ 15 cin>>n;16 if(n>=5&&n%10==0){ 17 yu0++; 18 sum0=sum0+n; 19 } 20 else if(n%5==1){ 21 yu1++; 22 sum1=sum1+j*n; 23 j=(-1)*j; 24 } 25 else if(n%5==2){ 26 yu2++; 27 } 28 else if(n%5==3){ 29 yu3++; 30 sum3=sum3+n; 31 } 32 else{ 33 yu4++; 34 if(n>max4) max4=n; 35 } 36 } 37 38 39 if(yu0==0) cout<<"N"<<" "; 40 else cout<<sum0<<" "; 41 42 if(yu1==0) cout<<"N"<<" "; 43 else cout<<sum1<<" "; 44 45 if(yu2==0) cout<<"N"<<" "; 46 else cout<<yu2<<" "; 47 48 double q=(double)sum3; 49 double w=(double)yu3; 50 if(yu3==0) cout<<"N"<<" "; 51 else { 52 cout<<setprecision(1) <<std::fixed<<q/w<<" "; 53 } 54 55 if(yu4==0) cout<<"N"; 56 else cout<<max4; 57 58 return 0; 59 }
改了半天程式碼都沒通過。。不知道哪裡出了問題。
看了看別人的程式碼:
1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 5 int main() 6 { 7 bool flag1=false; 8 bool flag2=false; 9 bool flag3=false; 10 bool flag4=false; 11 bool flag5=false; 12 int N; 13 int factor=1; 14 int t=0; 15 int A=0; 16 int mod=0; 17 int A1=0,A2=0,A3=0,A5=0; 18 double A4=0; 19 int k=0; 20 cin>>N; 21 while(N--) 22 { 23 cin>>t; 24 A=t/5; 25 mod=t%5; 26 switch(mod) 27 { 28 case 0: 29 if(A%2==0) {A1+=t;flag1=true;} break; 30 case 1: 31 A2 += factor*t; factor=-factor;flag2=true;break; 32 case 2: 33 A3++;flag3=true;break; 34 case 3: 35 A4+=t;k++;flag4=true;break; 36 case 4: 37 if(A5<t) A5=t;flag5=true;break; 38 } 39 40 } 41 if (flag1) 42 cout<< A1<<" "; 43 else 44 cout<<"N"<<" "; 45 if (flag2) 46 cout<< A2<<" "; 47 else 48 cout<<"N"<<" "; 49 if (flag3) 50 cout<< A3<<" "; 51 else 52 cout<<"N"<<" "; 53 if (flag4) 54 cout<<fixed<<setprecision(1)<<A4/k<<" "; 55 else 56 cout<<"N"<<" "; 57 if (flag5) 58 cout<<A5; 59 else 60 cout<<"N"; 61 cout<<endl; 62 //system("PAUSE"); 63 return 0; 64 }
//來自:https://www.cnblogs.com/ainima/p/6331249.html
我的程式碼好醜。。。