C题解 1021 个位数统计 (15 分)(1021 digit statistics of problem solution c (15 points))

原题

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

代码

#include <iostream>
using namespace std;

int result[10]={0};//存储0-9出现的次数
int main()
{
    char c;
    while (c != '\n')
    {
        scanf("%c", &c);//单个字符读取
        if (c >= '0' && c <= '9')
        {          
            result[c -'0']++;
        }
        
    }

    for (int i = 0; i < 10; i++)
    {
        if (result[i]>0)
        {
            cout <<i<<":"<<result[i]<<endl;
        }
        
    }
    
    return 0;
}
————————

Original title

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

code

#include <iostream>
using namespace std;

int result[10]={0};//存储0-9出现的次数
int main()
{
    char c;
    while (c != '\n')
    {
        scanf("%c", &c);//单个字符读取
        if (c >= '0' && c <= '9')
        {          
            result[c -'0']++;
        }
        
    }

    for (int i = 0; i < 10; i++)
    {
        if (result[i]>0)
        {
            cout <<i<<":"<<result[i]<<endl;
        }
        
    }
    
    return 0;
}