1012. Classification of numbers

Given a series of positive integers, sort the numbers as required and output the following 5 numbers:
A1 = the sum of all even numbers in the numbers divisible by 5;
A2 = the numbers that will be divided by 5 and have a remainder of 1 as given Sequentially perform interleaved summation, that is, calculate n1-n2+n3-n4...;
A3 = the number of digits with remainder 2 after division
by 5; A4 = the average number of digits with remainder 3 after division by 5, accurate to 1 decimal point digit;
A5 = the largest digit of the 4 remaining digits after dividing by 5.
Input format:
Each input contains 1 test case. Each test case first gives a positive integer N not exceeding 1000, and then gives N positive integers not exceeding 1000 to be classified. The numbers are separated by spaces.
Output format:
For the given N positive integers, calculate A1~A5 according to the requirements of the topic and output them in sequence in one line. Numbers are separated by spaces, but there must be no extra spaces at the end of the line. If one of the types of numbers does not exist, output "N" in the corresponding position.
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 Sample 2:
N 11 2 N 9

Note: The topic is very simple, that is, the use of multiple ifs. When faced with long questions, don't be afraid, and don't be too big. You must first sort out the context of the question, find the backbone, and then subdivide it into multiple small questions. Solve problems one problem at a time, don't get upset.

#include<stdio.h>
int main()
{
    int sum1=0, sum2=0, count1=0;
    double sum3=0, count2=0;
    int a[10000], b[10000];
    int i, j, k, N;//习惯从长到短了

    scanf("%d", &N);
    for (i=0; i<N; i++)
        scanf("%d", &a[i]);

    for (i=0,j=1,k=0; i<N; i++){//一定要记得题目要求输入的是N个正整数
        if (a[i]%5==0){
            if (a[i]%2==0)
                sum1 += a[i];//偶数和
        }
        else if (a[i]%5==1){
            if (j%2==1) sum2 += a[i];//交错和
            else    sum2 -= a[i];
            j ++;
        }
        else if
            (a[i]%5==2)count1 ++;//统计个数
        else if (a[i]%5==3){
            sum3 += a[i]*1.0;//乘以1.0防止变成整形计算
            count2 ++;//统计个数
            }
        else a[k++] = a[i];
    }

    if (sum1==0) printf("N ");
    else printf("%d ", sum1);

    if (j==1) printf("N ");
    else printf("%d ", sum2);

    if (count1==0) printf("N ");
    else printf("%d ", count1);

    if (count2==0) printf("N ");
    else printf("%.1f ", sum3/count2);

    int max = a[0];
    for (i=0; i<k; i++){
        if (a[i]>max) max = a[i];
    }
    if (k==0) printf("N");
    else printf("%d", max);//一直没能想出来怎么简化最下面这五条if else,好想一步判断,想到了就回来改掉
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325564158&siteId=291194637