PAT 1021 个位数统计 (15 分)

给定一个 k 位整数 N=dk−110k−1+⋯+d​1101+d0(0≤d​i​​ ≤9, i=0,⋯,k−1, d​k−1
​​ >0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 N=100311,则有 2 个 0,3 个 1,和 1 个 3。

输入格式:

每个输入包含 1 个测试用例,即一个不超过 1000 位的正整数 N。

输出格式:

对 N 中每一种不同的个位数字,以 D:M 的格式在一行中输出该位数字 D 及其在 N 中出现的次数 M。要求按 D 的升序输出。

输入样例:

100311

输出样例:

0:2
1:3
3:1

#include <stdio.h>
#include <string.h>

int main()
{
	char shuru[1001];
	int count[10];
	int len, i;
	
	scanf("%s", shuru);
	len = strlen(shuru);
	
	for (i = 0; i < 10; i++)
	{
		count[i] = 0;
	}
	
	for (i = 0; i < len; i++)
	{
		if (shuru[i] == '0')
		{
			count[0] += 1;
		}
		if (shuru[i] == '1')
		{
			count[1] += 1;
		}
		if (shuru[i] == '2')
		{
			count[2] += 1;
		}
		if (shuru[i] == '3')
		{
			count[3] += 1;
		}
		if (shuru[i] == '4')
		{
			count[4] += 1;
		}
		if (shuru[i] == '5')
		{
			count[5] += 1;
		}
		if (shuru[i] == '6')
		{
			count[6] += 1;
		}
		if (shuru[i] == '7')
		{
			count[7] += 1;
		}
		if (shuru[i] == '8')
		{
			count[8] += 1;
		}
		if (shuru[i] == '9')
		{
			count[9] += 1;
		}
	}
	
	for (i = 0; i< 10; i++)
	{
		if (count[i] != 0)
		{
			printf("%d:%d\n", i, count[i]);
		}
	}
	
	return 0;
}

C++

#include <iostream>
#include <map>
using namespace std;
int main()
{
	string s;
	cin >> s;
	map<char, int> m;
	for(int i = 0; i < s.length(); i++)
	{
		m[s[i]]++;
	}
	for(auto it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值