1064. 朋友数(20)

#include 
int calc(int c){    //分离各个数字并计算出朋友证号
    int friendnum = 0;
    while(c){
        friendnum += c % 10;
        c = c / 10;
    }
    return friendnum;
}
int main(){
    int N, c[40] = {0};
    int a[10005];
    int t;
    scanf("%d", &N);
    for(int i = 0; i < N; i++){
        scanf("%d", &a[i]);
        t = calc(a[i]);
        c[t] = t;           //用一个c数组保存朋友证号
    }
    int count = 0;          //计算出朋友证号的个数
    for(int i = 0; i < 40; i++){
        if(c[i] != 0)
            count++;
    }
    printf("%d\n", count);
    int one = 0;
    for(int i = 0; i < 40; i++){//输出朋友证号
        if(c[i] != 0){
            if(one == 1) putchar(' ');
            one = 1;
            printf("%d", c[i]);
        }
    }
    return 0;
}

你可能感兴趣的:(PAT乙级初步题解)