1064 朋友数 (20 分)
题目链接
算法分析
每读入一个数,计算它的朋友证号,并在cnt数组的相应位置加一,如果首次出现,则tot加一,tot即为朋友证号的数目,然后再跑一次循环,从小到大输出朋友证号。
代码实现
#include<bits/stdc++.h>
using namespace std;
#define N 50
int cnt[N], tot;
int cal(int x){
int sum = 0;
while(x){
sum += x % 10;
x /= 10;
}
return sum;
}
int main(){
int n, x;
scanf("%d", &n);
for(int i = 1; i <= n; ++ i){
scanf("%d", &x);
if(++ cnt[cal(x)] == 1) tot ++;
}
printf("%d\n", tot);
bool flag = 0;//格式化输出
for(int i = 1; i <= 36; ++ i)
if(cnt[i] >= 1){
if(flag) printf(" ");
printf("%d", i);
flag = 1;
}
return 0;
}