C++ Pat B 1012. Digital classification (20/20)

Source: Internet
Author: User

/*1012. Digital Classification (20) given a series of positive integers, classify the numbers as required and output the following 5 numbers: A1 = All the even sums of the numbers that can be divisible by 5, and A2 = The numbers that are divided by 5 after the remainder 1 are staggered and summed in the order that they are calculated n1-n2+n3-n4 ... A3 = The number of digits after the remainder 2 by 5, and A4 = The average of the number of 3 after 5, which is accurate to 1 digits after the decimal point; A5 = The largest number in the number after 5 is removed from the remainder 4. Input format: Each input contains 1 test cases. Each test case first gives a positive integer of not more than 1000 n, followed by a N of not more than 1000 of the positive integers to be classified. The numbers are separated by a space. Output format: For a given n positive integer, the A1~A5 is computed according to the topic requirement and sequentially output in a row. The numbers are separated by a space, but there is no extra space at the end of the line. If one of the numbers does not exist, the "N" is output at the corresponding location. Input Sample 1:13 1 2 3 4 5 6 7 8 9 10 20 16 18 Output Sample 1:30 11 2 9.7 9 Input Sample 2:8 1 2 4 5 6 7 9 16 Output Example 2:n 2 N 9*//*    Road: 1. Input data 1.1 According to the modulus value, judging the use case array element belongs to what kind of data 1.2 according to classification standard, preprocessing classification data 2. Output in accordance with the prescribed format. Note: The boundary condition is the detail, for example: to A2 count: To determine whether there are A2 elements, to avoid summing the result of 0 o'clock, causing ambiguity.    */#include <iostream>using namespace Std;int main () {int size; int tmp, mod, a2_count = 0,a2_prev = 1, A4_count = 0;//tmp: Temporarily stores raw data elements; A2_prev: The sign mark for the previous A2 element, the default is +,1:+,-1:-;a3_count: the meta-A3 The number of elements count int A1 = 0, A2 = 0, A3 = 0, A5 = -1;//a5: Stores the largest number in the number of 5 after the remainder 4, double A4 = 0;//a4: The sum of the stored element, and the last averaging scanf ("%d", &amp    ; size); while (size--) {scanf ("%d", &tmp);        MoD = tmp% 5; if (mod = = 0) {if (tmp% 2 = = 0) {A1 + = Tmp;//a1: All even-numbered and}}else (mods) that can be divisible by 5 = = 1) {//a2: will be 5 after the remainder 1 of the number in the order of the staggered summation, that is, calculate n1-n2+n3-n4 ...            ; A2 + = A2_prev * TMP;            A2_prev *=-1;        a2_count++;//Note: For the A2 count reason: To determine whether there are A2 elements, to avoid summing the result of 0 o'clock, causing ambiguity.        } else if (mod = = 2) {//a3: The number of digits 5 after the remainder 2; a3++; } else if (mod = = 3) {//a4: The average of the number of 5 after the remainder 3 is accurate to 1 digits after the decimal point; A4 + = tmp;//Cumulative sum a4_count++;//count} else {//            A5: The largest number in the number of the remaining 4 after 5 is removed. A5 = tmp > A5?        TMP:A5;    }} if (A1! = 0) {printf ("%d", A1);    } else {printf ("%s", "N");    } if (A2_count! = 0) {printf ("%d", A2);    } else {printf ("%s", "N");    } if (A3! = 0) {printf ("%d", A3);    } else {printf ("%s", "N");    } if (A4! = 0) {printf ("%.1f", A4/a4_count);    } else {printf ("%s", "N");    } if (A5! =-1) {printf ("%d", A5); }else {printf ("%s", "N"); }//printf ("%d%d%d%.1f%d", A1, A2, A3, A4/a4_count, A5);//test return 0;}

  

C++ Pat B 1012. Digital classification (20/20)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.