PAT 1002

PAT 1002

Title description :

给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:
 A1 = 能被5整除的数字中所有偶数的和;
 
 A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;
 
 A3 = 被5除后余2的数字的个数;
 
 A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
 
 A5 = 被5除后余4的数字中最大数字。

Enter a description:

每个输入包含1个测试用例。
每个测试用例先输入一个不超过1000的正整数N。
然后给出N个不超过1000的待分类的正整数。数字间以空格分隔。

Output description:

对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。
若其中某一类数字不存在,则在相应位置输出“N”。

Input example:

13 1 2 3 4 5 6 7 8 9 10 20 16 18

Example output:

30 11 2 9.7 9

Code:

#include <iostream>
#include <iomanip> 
using namespace std;
    
    int main() {
        int c1=0,c2=0,c2Num=0,c3=0,c4Num=0,c5=0;
        float c4=0;
        int n,x,i=1;
        cin>>n;
        while(n--)
        {
            cin>>x;
            switch(x%5)
            {
                case 0:
                    if(x%2==0)
                        c1+=x;
                        break;
                case 1:
                    c2Num++;
                    c2+=(i*x);
                    i=-i;
                    //这样可以使得i在奇数位时为1,偶数位时为-1
                    break;
                case 2:
                    c3++;
                    break;
                case 3:
                    c4+=x;
                    c4Num++;
                    break;
                case 4:
                    if(x>c5)
                        c5=x;
                        break;
            }
        }
        (c1>0)?(cout<<c1<<" "):(cout<<"N ");
        (c2Num!=0)?(cout<<c2<<" "):(cout<<"N ");
        (c3>0)?(cout<<c3<<" "):(cout<<"N ");
        (c4>0)?(cout<<fixed<<setprecision(1)<<c4/c4Num<<" "):(cout<<"N ");
        (c5>0)?(cout<<c5<<" "):(cout<<"N ");
        return 0; } ```

I've seen it before, and I finally checked it out today, and I think it's quite interesting.

io stands for input and output, and manip is the abbreviation of manipulator (manipulator), mainly for some manipulation operators such as cin and cout, such as: setfill, setw, setprecision, this topic uses setprecision

setprecision usage

Use setprecision(n) to control the number of floating-point numbers displayed in the output stream. C++'s default stream output value has 6 significant digits.
If setprecision(n) is combined with setiosflags(ios::fixed), you can control the number of digits to the right of the decimal point.
setiosflags(ios::fixed) is used to represent real numbers in a fixed-point manner. If used together with setiosflags(ios::scientific), it can control the number of decimal places in exponential notation.

in conclusion:

fixed 代表用一般的方式输出浮点数,而不是科学计数法
setprecision(n) 中的变量n可控制输出流显示浮点数的数字个数

Another interesting point is that for a column of numbers, the addition operation is performed on the numbers with odd digits, and the subtraction operation is performed on the numbers with even digits. We all know how to sum, but how to stagger the sum? How can I control the sign of the number without changing the size of the number?
The answer is: multiplying a number by "-1" will change the sign of the number, and multiplying it by "1" will not change the number. So we multiply "1" for odd bits and "-1" for even bits.

if(x%5 == 1) 
{
 int i = 1; // i变量是用来改变数字符号的,由于第一个数为正数,所以i的初始值为1*
c2 = c2 + (i*x); 
i = -i; // 这样可以使i在奇数位的时候为1,偶数位的时候为-1
 }

Guess you like

Origin blog.csdn.net/Duba_zhou/article/details/126396405