当前位置:网站首页>PAT 乙等 1021 个位数统计
PAT 乙等 1021 个位数统计
2022-06-23 04:11:00 【章鱼bro】
1021. 个位数统计 (15)
给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-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
思路:位数太大,只能使用字符串,首先初始化一个长度为10的数组用作计数数组,下表对应数字,对输入的字符串的每一位-‘0’,以此运算结果作为数组下标加一运算,最后进行输出
一、起始变量
1.input【1000】输入字符串
2.count【10】计数数组
二、运算
1.输入字符串
2.对输入字符串的每一位做减‘0’运算,
3.把运算结果作为计数数组下标执行加一运算
4.循环输出
三、代码
#include "stdio.h"
#include "string.h"
int main()
{
char input[1000];
memset(input,0,1000);
scanf("%s",input);
int i;
int count[10] = {0,0,0,0,0,0,0,0,0,0};//对应0-9的出现次数
for(i = 0; i < strlen(input); i++)
{
count[input[i] - '0']++;
}
for(i = 0; i < 10; i++)
{
if(count[i] != 0)
{
printf("%d:%d\n",i,count[i]);
}
}
return 0;
}边栏推荐
- Visdom draws multiple dynamic loss curves
- 华为软硬件生态圈成型,从根子上改变美国对软硬件体系的领导地位
- Win11应用商店一直转圈解决办法
- How much disk IO will actually occur for a byte of the read file?
- B-string value (string DP) of the 16th Northeast College Students' Programming Competition (warm-up)
- Use of visdom
- 高等数学(第七版)同济大学 习题1-9 个人解答
- MySQL面试真题(二十二)——表连接后的条件筛选及分组筛选
- Heimdall Database Proxy横向扩展提高20倍
- 技能自检 | 想当测试Leader,这6项技能你会吗?
猜你喜欢

Export PDF with watermark

Shifu, the open source development platform of the Internet of things, is open for internal testing! Release of the first version of technical documents

The road of architects starts from "storage selection"

STC 32-bit 8051 MCU development example tutorial I development environment construction

Face recognition determination threshold

Software design and Development Notes 2: serial port debugging tool based on QT design

QT QWidget nesting relative position acquisition (QT drawing nesting)

Software project management 8.4 Software project quality plan

Composite API

STC 32位8051单片机开发实例教程 一 开发环境搭建
随机推荐
数字藏品赋能实体产业释放了哪些利好?
Software project management 8.4 Software project quality plan
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-7 personal solutions
51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭
数字藏品如何赋能经济实体?
The digital collection market has just begun
The road of architects starts from "storage selection"
STC 32比特8051單片機開發實例教程 一 開發環境搭建
CF [1700d] D. River locks (DP, bisection, Mathematics)
What benefits have digital collections enabled the real industry to release?
Wechat applet: wechat can also send flash photos to create wechat applet source code download and customize flash time
GDB data reading in GDAL (III) of GIS
Oracle异常
June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
Genetic engineering of AI art? Use # artbreeder to change any shape of the image
Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发
LeetCode-1757. Recyclable and low-fat products_ SQL
Digital collections - new investment opportunities
What does the English letter PC mean? What does the Internet PC mean