首页 > 代码库 > PAT 甲级 1120 Friend Numbers

PAT 甲级 1120 Friend Numbers

 1 #include <iostream>
 2 #include <cstring>
 3 int a[10000];
 4 int max1=0, n=0;
 5 int first = 0;
 6 using namespace std;
 7 
 8 void bijiao()
 9 {
10     int i;
11     for (i=1;i<=max1; i++)
12     {
13         if (a[i]>=1)
14         {
15             n++;
16         }
17     }
18 }
19 void shuchu()
20 {
21     int i;
22     for (i=1;i<=max1;i++)
23     {
24         if (a[i]>=1)
25         {
26             if (first)
27             {
28                 cout <<" ";
29             }
30             cout <<i;
31             first = 1;
32         }
33     }
34 }
35 void qiujie(int x)
36 {
37     int m=0;
38     while(x>0)
39     {
40         m += x%10;
41         x /= 10;
42     }
43     max1 = m>max1 ? m:max1;
44     a[m]++;
45 }
46 int main()
47 {
48     int N;
49     cin >>N;
50     int i;
51     int x;
52     for (i=0; i<N; i++)
53     {
54         cin >>x;
55         qiujie(x);
56     }
57     bijiao();
58     cout <<n <<endl;
59     shuchu();
60 
61     return 0;
62 }

 

PAT 甲级 1120 Friend Numbers