PAT_Basic 1012
2016-03-06 22:39
375 查看
Unfortunally I mis-understand the meaning of the question and failed at first
First input is the number of the test list numbers, not as one of them
The following is the unfinished codes( I just quit), the whole idea is about the (switch..case) using method:
/*
给定一系列正整数,请按要求对数字进行分类,并输出以下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”。
输入样例1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出样例1:
30 11 2 9.7 9
输入样例2:
8 1 2 4 5 6 7 9 16
输出样例2:
N 11 2 N 9
*/
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
void main(){
int tmp_in;
int A1, A2, A3, A4, A5;
A1 = A2 = A3 = A4 = A5 = 0;
int label2 = -1;
int max = -1;
int num_A4 = 0;
int tmp;
while (cin >> tmp_in){
int re = tmp_in % 5;
switch (re)
{
case 0:
if (tmp_in % 2 == 0) A1 += tmp_in;
break;
case 1:
label2 = 0 - label2;
A2 = A2 + tmp_in*label2;
break;
case 2:
A3++;
break;
case 3:
A4 += tmp_in;
num_A4++;
break;
case 4:
if (tmp_in > max) max = tmp_in;
break;
default:
break;
}
}
A5 = max;
float cnt4 = (float)A4 / (float)num_A4;
if (A1 == 0) cout << "N ";
else cout << A1;
if (A2 == 0) cout << "N ";
else cout << A2;
if (A3 == 0) cout << "N ";
else cout << A3;
if (cnt4 != 0){
char* c4 = {};
sprintf(c4, "%0.1f", cnt4);
printf("%0.1f ", cnt4);
}
else
cout << "N ";
if (max > 0)
cout << A5;
else
cout << "N ";
system("pause");
}
First input is the number of the test list numbers, not as one of them
The following is the unfinished codes( I just quit), the whole idea is about the (switch..case) using method:
/*
给定一系列正整数,请按要求对数字进行分类,并输出以下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”。
输入样例1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出样例1:
30 11 2 9.7 9
输入样例2:
8 1 2 4 5 6 7 9 16
输出样例2:
N 11 2 N 9
*/
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
void main(){
int tmp_in;
int A1, A2, A3, A4, A5;
A1 = A2 = A3 = A4 = A5 = 0;
int label2 = -1;
int max = -1;
int num_A4 = 0;
int tmp;
while (cin >> tmp_in){
int re = tmp_in % 5;
switch (re)
{
case 0:
if (tmp_in % 2 == 0) A1 += tmp_in;
break;
case 1:
label2 = 0 - label2;
A2 = A2 + tmp_in*label2;
break;
case 2:
A3++;
break;
case 3:
A4 += tmp_in;
num_A4++;
break;
case 4:
if (tmp_in > max) max = tmp_in;
break;
default:
break;
}
}
A5 = max;
float cnt4 = (float)A4 / (float)num_A4;
if (A1 == 0) cout << "N ";
else cout << A1;
if (A2 == 0) cout << "N ";
else cout << A2;
if (A3 == 0) cout << "N ";
else cout << A3;
if (cnt4 != 0){
char* c4 = {};
sprintf(c4, "%0.1f", cnt4);
printf("%0.1f ", cnt4);
}
else
cout << "N ";
if (max > 0)
cout << A5;
else
cout << "N ";
system("pause");
}
相关文章推荐
- 1.让自己习惯C++
- 重写(override)那点事
- jQuery学习笔记
- iOS音频播放(二):AudioSession
- C语言实现使用静态数组实现循环队列
- BroadcastReceiver
- 对软件工程的疑惑
- 数据结构之链表归并排序
- iOS音频播放(一):概述
- JavaScript Core -- 函数表达式面试题
- ASP.NET 网站如何集成QQ登录功能
- 娱乐大咖项目- 总结
- 基于MATLAB实现的云模型计算隶属度
- 内核分析 第二周
- 构建一个RESTful Web Service(Building a RESTful Web Service)
- 汇编语言对显存直接输出字符串
- Drop and Drag API学习与整理
- 《Linux内核分析》 操作系统是如何工作的
- 学习状况总结
- 0.九九乘法表[递归]