1012 数字分类

本文提供了一个C++程序的实例,该程序通过一系列条件判断来处理输入数据,并最终输出特定的结果。程序中涉及了数组操作、条件分支、循环等基本编程结构,以及浮点数格式化输出的方法。

题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805311146147840

题解:

 

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 
 5 int main(){
 6     int n;
 7     cin>>n;
 8     bool flag1=false,flag2=false,flag3=false,flag4=false,flag5=false;
 9     int *p=new int[n];
10     for(int i=0;i<n;i++)
11         cin>>p[i];
12     int A1=0,A2=0,A3=0,A5=0;
13     float A4=0.0;
14     int *tempA5=new int[n],kA4=0,kA5=0,cnt=0;
15     for(int i=0;i<n;i++){
16         int select=p[i]%5+1;
17         switch(select){
18             case 1:
19                 if(p[i]%2==0){
20                     A1+=p[i];
21                     flag1=true;
22                 }
23                 break;
24             case 2:
25                 if(cnt%2!=0) p[i]=-p[i];
26                 A2+=p[i];
27                 cnt++;
28                 flag2=true;
29                 break;
30             case 3:
31                 A3++;
32                 flag3=true;
33                 break;
34             case 4:
35                 A4+=p[i];
36                 kA4++;
37                 flag4=true;
38                 break;
39             default:
40                 tempA5[kA5++]=p[i];
41                 flag5=true;
42                 break;
43         }
44     }
45     A4=(float)A4/kA4;//容易错,用强制类型转换
46     A5=tempA5[0];
47     for(int i=0;i<kA5;i++){
48         if(tempA5[i]>A5) A5=tempA5[i];
49     }
50     if(!flag1) cout<<'N'<<" ";
51     else cout<<A1<<" ";
52     if(!flag2) cout<<'N'<<" ";
53     else cout<<A2<<" ";
54     if(!flag3) cout<<'N'<<" ";
55     else cout<<A3<<" ";
56     if(!flag4) cout<<'N'<<" ";
57     else cout<<setiosflags(ios::fixed)<<setprecision(1)<<A4<<" ";//不加setiosflags(ios::fixed)输出格式错误
58     if(!flag5) cout<<'N';
59     else cout<<A5;
60     return 0;
61 }

 

转载于:https://www.cnblogs.com/Gzu_zb/p/9513363.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值