PAT B1021 个位数统计 (15)

AC代码

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int max_n = 1010;
char ans[max_n];
int count[10][2] = {0};

int main() {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    char a;
    int times = 0;
//gets()在PAT编译里面错误,故换了种方法读入数据
    while(cin >> a) {
        ans[times++] = a;
    }
    int length = strlen(ans);
    for(int i = 0; i < length; i++) {
        int a = ans[i] - '0';
        count[a][1] = 1;
        count[a][0]++;
        //printf("count[%d][1]: %d, count[ans[%d]][0]:%d\n", a, count[a][1], a, count[a][0]);
    }
    for(int i = 0; i < 10; i++) {
        if(count[i][1] == 1) {
            printf("%d:%d\n", i, count[i][0]);
        }
    }
    return 0;
}
posted @ 2019-08-05 23:05  哨音  阅读(105)  评论(0编辑  收藏  举报