PAT乙级1021

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805300404535296

题解

很简单,用string保存这个数字,用一个数组保存各数字出现的次数,然后遍历字符串统计各数字个数,最后按照格式输出各位数字的个数。

// PAT BasicLevel 1021
// https://pintia.cn/problem-sets/994805260223102976/problems/994805300404535296

#include <iostream>
#include <string>
using namespace std;

int main()
{
    // 用户输入数字
    string str;
    cin >> str;

    // 统计各数字个数
    int digitCount[]={0,0,0,0,0,0,0,0,0,0};
    for(int i=0;i<str.length();i++){
        digitCount[str[i]-'0']++;
    }

    // 输出各数字个数
    for(int i=0;i<10;i++){
        if(digitCount[i]>0){
            cout << i << ':' << digitCount[i] << endl;
        }
    }

    //system("pause");
    return 0;
}

作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!


原文地址:https://www.cnblogs.com/chouxianyu/p/11309258.html