首页 > 其他分享 >数数字(Digit Counting)

数数字(Digit Counting)

时间:2022-11-28 18:36:59浏览次数:39  
标签:Digit 数字 int digit ++ digitCount input Counting appears


Digit Counting

Time limit: 3.000 seconds

Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 toN (1 < N < 10000). After that, he counts the number of times each digit (0 to 9) appears in the sequence. For example, withN = 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

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


数数字

       把前n(n<10000)个整数顺次写在一起:123456789101112……数一数 0 ~ 9 各出现多少次(输出10个整数,分别是0,1,……,9出现的次数。

【分析】

       (分析过程附加在程序注释中)

用java语言编写程序,代码如下:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
//先是把所有的结果都计算出来
int[][] digitCount = getDigitCount();

Scanner input = new Scanner(System.in);
int T = input.nextInt();
for(int i = 0; i < T; i++) {
int n = input.nextInt();
for(int j = 0; j < 9; j++)
System.out.print(digitCount[n][j] + " ");
System.out.println(digitCount[n][9]);
}
}

//求前n个整数顺次写在一起后 0 ~ 9各出现多少次。答案在二维数组的第n行中。
public static int[][] getDigitCount() {
int[][] digitCount = new int[10001][10];

//前0个整数顺次写在一起后 0 ~ 9各出现0次
for(int i = 0; i < 10; i++)
digitCount[0][i] = 0;

for(int i = 1; i <= 10000; i++) {
//知道前 n-1 个整数的结果,只要再加上 n 这个数内0~9各出现的次数即为前n个整数的结果
for(int j = 0; j < 10; j++)
digitCount[i][j] = digitCount[i - 1][j];

getNumCounting(digitCount, i, i);
}
return digitCount;
}

//计算每个数内0~9各出现的次数
private static void getNumCounting(int[][] arr, int i, int n) {
int digit = 0;
while(n > 0) {
digit = n % 10;
n /= 10;
arr[i][digit]++;
}
}
}



标签:Digit,数字,int,digit,++,digitCount,input,Counting,appears
From: https://blog.51cto.com/u_15894233/5893359

相关文章

  • 全连接神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架,......
  • java 验证邮箱格式正确性、验证字符串是否为数字
    java验证邮箱格式正确性 importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassRegisterCheck{   /**    *验证输入的邮箱格式是......
  • 实验五:全连接神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架,......
  • 谈谈我的「数字文具盒」
    最近耗费了一周的时间迁移服务器,升级打造V3版本的「数字文具盒」。从2018年第一次尝试搭建博客,期间尝试各种生产力工具到现如今的第三次迁移服务器,终于打造了一个完美......
  • 实验五:全连接神经网络手写数字识别实验v
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架,......
  • 智慧型可视化综合管理平台,全方位打造数字孪生城市
    随着人工智能、大数据、物联网、云计算、区块链等信息技术的快速发展,智慧化管理正在成为城市运营的全新方式。北京深圳等一些一线城市已经率先开始城市运营数字化升级的工......
  • 使用微信的你千万不要透露这串数字!赶紧告诉家里人
    "IT有得聊”是机械工业出版社旗下IT专业资讯和服务平台,致力于帮助读者在广义的IT领域里,掌握更专业、实用的知识与技能,快速提升职场竞争力。 据不完全统计,中国网民规模达7.1......
  • 实验五:全神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架......
  • 数字签名方案
    数字签名方案数字签名安全模型(Sig-forge实验流程):攻击者通过查询一些消息的签名,不能构造出没有查询过的消息的签名,即\(Pr[Sig-forge_{A,\Pi}(n)=1]\leqnegl(n)\)RSA......
  • 实验五:全连接神经网络手写数字识别实验
    实验五:全连接神经网络手写数字识别实验 【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程......