【刷题】uva10954 Add All【3】

一、题目

1.题目描述

题目:Fahim likes to solve mathematical problems. But sometimes it becomes challenging for him to solve all the mathematical problems. So sometimes he gets angry to solve mathematical puzzles. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 to N (1 < N < 10000). After that, he counts the number of times each digit (0 to 9) appears in the sequence. For example, with N = 13, the sequence is.
12345678910111213
The sequence is interesting, right! In this sequence, zero(0) appears once, one(1) appears 6 times, two(2) appears 2 times, three(3) appears 3 times, and each digit from four(4) to nine(9) appears once. After playing for a while, Fahim gets bored again. He now wants to write a program to do this for him. Your task is to help him with writing this program.
输入:Look at the input file carefully. It consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets. For each test case, there is one single line containing the number N.
输出:Now for each individual test case, write sequentially in one line the number of digit 0, 1, . . . 9 separated by a space.
示例 :
在这里插入图片描述

二、解题报告

1.思路分析

  • 通过不断取模的方式直到取到模为0为止,得到数字的每一位数t5%10
  • 创建数组0-9的全零数组,通过判断数组索引t2==t5%10来累计计数

2.代码详解

C++👇

#include<iostream>
int main(){
	int k=0;
	scanf("%d",&k);
	for (int t1=0;t1<k;t1++){
		int N=0;
		int result[10]={0};
		scanf("%d",&N);
		for (int t2=0;t2<10;t2++){
			for(int t3=1;t3<N+1;t3++){
				int t5=0;
				t5=t3;
				while(t5!=0){
					if(t2==t5%10){
						result[t2]++;
					}
					t5=t5/10;
				}
			
			}
		}
		for(int t4=0;t4<10;t4++){
			printf("%d",result[t4]);
			if(t4!=9){
				printf(" ");
			} else{
				printf("\n");
			}
		}
	
	}
} 

3.注意事项

1.数组脏数据
在这里插入图片描述
这样👇
在这里插入图片描述

2.输出的格式一定是后面直接回车,不能多空格
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

司六米希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值