数数字(数组与函数的运用)

本文介绍了如何用C++解决计算从1到N的连续整数序列中每个数字出现次数的问题。通过两个解法示例,包括使用函数和不使用函数,帮助初学者理解数组和函数的应用。并提供了样例输入和输出。
摘要由CSDN通过智能技术生成

前言

本人是一名计算机新人,在去年下半年初识C语言,然而到现在进度一直很慢,对许多东西知其然而不知其所以然,希望通过写博客的方式来督促自己并将学习到的知识扎实掌握。

一、原型题

不喜英文题目的可自取翻译后的结果~

1225 Digit Counting

Trung is bored with his mathematics homeworks. 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 In this sequence, 0 appears once, 1 appears 6 times, 2 appears 2 times, 3 appears 3 times, and each digit from 4 to 9 appears once. After playing for a while, Trung 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. Input The input file 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. Output For each test case, write sequentially in one line the number of digit 0, 1, . . . 9 separated by a space.

Sample Input

2

3

13

Sample Output

0 1 1 1 0 0 0 0 0 0

1 6 2 2 1 1 1 1 1 1

二、翻译后

Trung对他的数学作业感到厌烦。他拿起一支粉笔,开始写一个从1到N的连续整数序列(1 < N < 10000)。然后,他计算每个数字(0到9)在序列中出现的次数。例如,当N = 13时,序列为:12345678910111213。0出现一次,1出现6次,2出现2次,3出现3次,从4到9的每一位都出现一次。玩了一会儿之后,Trung又觉得无聊了。现在,他想编写一个程序来实现这一点。你的任务是帮助他写这个程序。输入文件由几个数据集组成。输入文件的第一行包含不大于20的正整数数据集的数量。下面几行描述了数据集。对于每个测试用例,都有一行包含数字n。输出每个测试用例,按顺序在一行中写入数字0,1,…9用空格隔开。

样例输入
2

3

13

样例输出

0 1 1 1 0 0 0 0 0 0

1 6 2 2 1 1 1 1 1 1

三、解法

1.解法(有函数)

代码如下(示例):

#include <iostream>

void solve(int num) {
    int count[10] = { 0 };
    for (int i = 1; i <= num; ++i) {
        int n = i;
        while (n) {
            ++count[n % 10];
            n /= 10;
        }
    }
    for (int i = 0; i < 10; ++i) std::cout << count[i] << ' ';
}

int main() {
    int cases;
    std::cin >> cases;
    while (cases) {
        --cases;
        int num;
        std::cin >> num;
        solve(num);
        std::cout << std::endl;
    }
}

2.解法(无函数)

代码如下(示例):

#include<iostream>

int main()
{
	int n;
	std::cin >> n;
	while (n--)
	{
		int a[10] = { };
		int x; 
		std::cin >> x;
		for (int i{ 1 }; i <= x; ++i)
		{
			int j{ i };
			while (j > 0)
			{
				a[j % 10]++;
				j /= 10;
			}
		}
		for (int i{ 0 }; i <= 8; i++)
			std::cout << a[i] << " ";
		std::cout << a[9] << std::endl;
	}
	return 0;
}

四、实现截图

第一行输入我们要输入多少组数,我们输入了5,即:1、13、111、1024、666这六个数


总结

这个题不难,就数组与函数的运用,即使初识C语言也能够完全搞懂这道题以及这道题的C++用法。

希望通过这道题给和我相同的初学者们一点我的想法。

当然,这道题还有许许多多的实现代码,那些代码比我所写好的大有人在。在这里也不探讨效率问题了。

首次写博客,希望各位大佬多多担待。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值