当前位置:网站首页>[pat (basic level) practice] - [simple simulation] 1064 friends

[pat (basic level) practice] - [simple simulation] 1064 friends

2022-07-01 04:30:00 IronmanJay

One 【 questions 】

  • Class B

Two 【 Title number 】

  • 1064 Number of friends (20 branch )

3、 ... and 【 Title Description 】

  • If the sum of two integers is the same , It's called “ Number of friends ”, And the public one is theirs “ Friend ID number ”. for example 123 and 51 It's the number of friends , because 1+2+3 = 5+1 = 6, and 6 It's their friend ID number . Given some integers , Ask you to count how many different ID numbers they have .

Four 【 Title Example 】

  • Input format :
    Enter the first line to give a positive integer N. The next line shows N A positive integer , Numbers are separated by spaces . Make sure that all numbers are less than 1 0 4 10^4 104 .

  • Output format :
    First, the first line outputs the number of different friend ID numbers in the given number ; The next line outputs these ID numbers in increasing order , The numbers are separated by a space , And there must be no extra space at the end of the line .

  • sample input :
    8
    123 899 51 998 27 33 36 12

  • sample output :
    4
    3 6 9 26

5、 ... and 【 Their thinking 】

  • We can notice that , The range of figures given in the title is [ 0 ∼ 9999 ] [0\sim9999] [09999], So the range of the sum of the digits is [ 0 ∼ 36 ] [0\sim36] [036], So create a new one with a length of 37 The number of , Initialize to 0, Then scan each number in turn , And ask you to sum up , After finding out, change the corresponding position to 1, Note that only the original position is 0 To modify the , Because the title requires only one output . And record the number in it , Then we just need to output according to the requirements of the topic , Number of outputs first , Then traverse the array created before , If a position is 1, Just output the subscript of this position . In addition, you need to pay attention to the problem of spaces

6、 ... and 【 The final score 】

  • 20 branch

7、 ... and 【 Code implementation 】

#include<stdio.h>
int main()
{
    
    int n,count,temp,sum = 0;
    scanf("%d",&n);
    int nums[37] = {
    0};
    for(int i = 0;i<n;i++)
    {
    
        scanf("%d",&temp);
        while(temp != 0)
        {
    
            sum += temp % 10;
            temp /= 10;
        }
        if(nums[sum] == 0)
        {
    
            nums[sum]++;
            count++;
        }
        sum = 0;
    }
    printf("%d\n",count);
    for(int i = 0;i<37;i++)
    {
    
        if(nums[i] != 0)
        {
    
            printf("%d",i);
            count--;
            if(count != 0)
            {
    
                printf(" ");
            }
        }
    }
    return 0;
}

8、 ... and 【 Submit results 】

 Insert picture description here

原网站

版权声明
本文为[IronmanJay]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160255223625.html