PAT 数字分类
来源:互联网 发布:java编程教学视频 编辑:程序博客网 时间:2024/06/06 13:14
题目描述
给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:A1 = 能被5整除的数字中所有偶数的和;A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;A3 = 被5除后余2的数字的个数;A4 = 被5除后余3的数字的平均数,精确到小数点后1位;A5 = 被5除后余4的数字中最大数字。
输入描述:
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。
输出描述:
对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。若其中某一类数字不存在,则在相应位置输出“N”。
输入例子:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出例子:
30 11 2 9.7 9
#include <stdio.h>#include <algorithm>using namespace std;int main(){int n,x;int a1 = 0,a2 = 0,a3 = 0,a5 = 0;double a4 = 0.0;scanf("%d",&n);int flag = 1;int sum = 0;int t1 = 0,t2 = 0,t3 = 0,t4 = 0,t5 = 0;for (int i = 0; i < n; ++i){scanf("%d",&x);if(!(x%5) && !(x%2)){a1 += x;t1 = 1;}else if(x % 5 == 1){t2 = 1;if(flag){a2 += x;flag = 0;}else{a2 -= x;flag = 1;}}else if(x % 5 == 2){t3 = 1;a3++;}else if(x % 5 == 3){t4 = 1;a4 += x;sum++;}else if(x % 5 == 4){t5 = 1;if(x > a5)a5 = x;}}a4 = a4 / (sum * 1.0);if(t1)printf("%d ",a1);elseprintf("N ");if(t2)printf("%d ",a2);elseprintf("N ");if(t3)printf("%d ",a3);elseprintf("N ");if(t4)printf("%.1f ",a4);elseprintf("N ");if(t5)printf("%d\n",a5);elseprintf("N\n");}
0 0
- PAT 1012. 数字分类
- PAT 1012 数字分类
- 1012 数字分类 PAT
- PAT-1012 数字分类
- 数字分类,PAT,1012
- PAT---B1012 数字分类
- PAT-数字分类
- PAT 1012 数字分类
- PAT 数字分类
- PAT 1012. 数字分类
- PAT 1012.数字分类
- PAT B1012.数字分类
- PAT 1012数字分类
- PAT 1012. 数字分类
- PAT basic 1012 :数字分类
- PAT 1012. 数字分类 (20)
- PAT乙级 1012. 数字分类
- pat(B) 1012. 数字分类
- PHP代码错误,nginx直接返回500错误解决方案
- ArcGIS Workflow Manager集成框架之使用体会及其安装配置
- 初识JS的魅力(1)
- 简述linux启动过程
- leetcode_169. Majority Element
- PAT 数字分类
- LeetCode 11. Container With Most Water
- Ibatis贴加引用
- Appium 获取h5页面元素需要加调试代码
- Windows 2008允许多用户远程登录设置
- js-基本数据类型-你不知道的趣味题
- android 开发中 最近在工作中用到的 adb 命令
- gradle学习指南
- ionic -- 使用cordova的插件实现分享到QQ、微博、微信