首页 > 学院 > 开发设计 > 正文

PAT BASIC 1012

2019-11-11 03:17:28
字体:
来源:转载
供稿:网友

思路:

这个题大概是想考switch的使用吧,没什么难点,如果想知道cout怎么保留一位小数,去看一下我的博客

然后这个题的样例2我觉得有问题,明明有5,不能说是没有。既然样例这么理解那就按照他来。

代码:

#include<iostream>#include<iomanip>using namespace std;int main(){ int state[5] = { 0 }; int a1 = 0, a2 = 0, a3 = 0, a5 = 0; double a4 = 0; int flag2 = 1, num4 = 0; int n, i, j, num; cin >> n; for (i = 0; i < n; i++) { cin >> num; switch (num%5) { case 0: if (num % 2 == 0) { state[0] = 1; a1 += num; } break; case 1: state[1] = 1; a2 = a2 + flag2*num; flag2 = -1 * flag2; break; case 2: state[2] = 1; a3++; break; case 3: state[3] = 1; a4 = a4 + num; num4++; break; case 4: state[4] = 1; if (num > a5) a5 = num; break; default: break; } } cout.setf(ios::fixed); a4 = a4 / num4; if (state[0]) cout << a1 << " "; else cout << "N "; if (state[1]) cout << a2 << " "; else cout << "N "; if (state[2]) cout << a3 << " "; else cout << "N "; if (state[3]) cout << fixed << setPRecision(1) << a4 << " "; else cout << "N "; if (state[4]) cout << a5; else cout << "N"; //while (1) //{ //} return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表