PAT 1012 Digital Category B (C language), pass the test point solutions 8

Given a positive integer number, press the classification requirements for digital, and outputs the following numbers 5:
A. 1 = 5 be divisible numbers and even numbers;
A 2 = 5 will be in addition to after more than 1 interleaved digital sum in the order given, i.e. 2 -n + 1 calculated n-n--n. 4 ⋯. 3;
a is = 5. 3 except after the number of digits 2, it;
a = 4 is other than 5 Mean numbers after the 3, 1 decimal place;
a 5 = 5 is other than the maximum numbers 4 digital.

Input formats:

Each input comprises a test. Each test case is given a first positive integer of not more than 1000 N, and then gives the N does not exceed 1000 to be classified positive integer. Between numbers separated by a space.

Output formats:

For a given positive integer N, is calculated according to the subject in claim 1 ~ A 5 and A sequentially outputting in a row. Between numbers separated by spaces, but the end of the line may not have the extra space.

If a certain type wherein the number does not exist, the output in the corresponding position N.

Sample Input 1:

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

Output Sample 1:

30 11 2 9.7 9

Sample Input 2:

8 1 2 4 5 6 7 9 16

Output Sample 2:

N 11 2 N 9

This question biggest pit, eighth test points could not pass .
The reason: a2 possible equal to zero.
Solution: Add condition, it determines there is no number is divided by 51 (i.e., 5% have determined == 1) statement is not executed in the IF (A [J]) .

#include <stdio.h>
int main()
{
    int a[1001],n,b=1;
    int a1=0,a2=0,a3=0,a5=0;
    float a4=0,c=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int j=0;j<n;j++)
    {
        if(a[j]%10==0)
            a1=a1+a[j];
        if(a[j]%5==1)
        {    
            if(b%2==1)
            {
                a2=a2+a[j];
                b++;
            }
            else
            {
                a2=a2-a[j];
                b++;
            }       
        }
        if(a[j]%5==2)
            a3++;
        if(a[j]%5==3)
        {    
            a4=a4+a[j];
            c++;
        }    
        if(a[j]%5==4 && a[j]>a5)
            a5=a[j];
    }
    if(a1==0)
        printf("N ");
    else
        printf("%d ",a1);
    if(a2==0 && b==1)  //a2有可能等于0,此时要判断有没有除以5余1的数字
        printf("N ");
    else
        printf("%d ",a2);
    if(a3==0)
        printf("N ");
    else
        printf("%d ",a3);
    if(a4==0)
        printf("N ");
    else
        printf("%.1f ",a4/c);
    if(a5==0)
        printf("N");
    else
        printf("%d",a5);
    return 0;
}

/*本题最大坑点,第8个测试点过不了。*/
/*原因:a2可能等于0*/
/*解决方法:加个条件,判断有没有除以5余1的数字(即判断有没有执行过if(a[j]%5==1)里的语句)。*/
发布了15 篇原创文章 · 获赞 0 · 访问量 288

Guess you like

Origin blog.csdn.net/weixin_44562957/article/details/104056442