1. 程式人生 > >1064. 朋友數(20)

1064. 朋友數(20)

#include <cstdio>
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; }