zoukankan      html  css  js  c++  java
  • [PAT乙级] Practise 1012 数字分类

    PAT (Basic Level) Practice (中文)1012

    1012 数字分类

    给定一系列正整数,请按要求对数字进行分类,并输出以下 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
    

    思路:

    由题意可知:

    • put[0] - 被5余0-偶数
    • put[1] - 除5余1 按顺序交错求和 n1-n2+n3-n4
    • put[2] - 除5余2 个数
    • put[3] - 除5余3 Avg 0.1
    • put[4] - 除5余4 Max
    • else N

    注意:

    在求 put[1] 的和时,会有求和结果为也0的情况,注意判断。

    代码:

    #include <iostream>
    #include <map>
    using namespace std;
    
    int main() {
    	double put[5] = { 0 };
    	int na2 = 1, fa2 = 0;
    	int va3 = 0, now, N;
    	cin >> N;
    	for (int i = 0; i < N;i++) {
    		cin >> now;
    		if (now % 5 == 0 && now % 2 == 0) {
    			put[0] += now;
    		}
    		if (now % 5 == 1) {
    			put[1] = put[1] + now * na2;
    			na2 *= -1;
    			fa2++;
    		}
    		if (now % 5 == 2) {
    			put[2]++;
    		}
    		if (now % 5 == 3) {
    			put[3] += now;
    			va3++;
    		}
    		if (now % 5 == 4 && now > put[4]) {
    			put[4] = now;
    		}
    		if (cin.get() == '
    ')
    			break;
    	}
    	if (put[3] != 0) put[3] /= va3;
    	if (put[0] == 0) cout << "N" << " ";
    	else if (put[0] > 0) cout << put[0] << " ";
    
    	if (fa2 == 0 ) cout << "N" << " ";
    	else if (fa2 > 0) cout << put[1] << " ";
    
    	if (put[2] == 0) cout << "N" << " ";
    	else if (put[2] > 0) cout << put[2] << " ";
    
    	if (put[3] == 0.0) cout << "N" << " ";
    	else if (put[3] > 0.0)
    		printf("%.1f ", put[3]);
    
    	if (put[4] == 0) cout << "N";
    	else if (put[4] > 0) cout << put[4];
    	return 0;
    }
    
  • 相关阅读:
    Html 回顾
    Parallel 并行编程
    Task---常用的多线程(基于多线程线程)
    Threadpool 可以对线程加以管理的封装
    AsyncThreads---异步多线程
    Abstract 封装,继承,多态
    IO&&Serize 利用线程Thread.Sleep实现"自动输出"
    Ling && Lambda
    Delegate&&Event
    Delegate &&Lambda
  • 原文地址:https://www.cnblogs.com/eisuto/p/12490911.html
Copyright © 2011-2022 走看看