UVA1225 数数字 Digit Counting——输出格式!输出格式!输出格式!

柔情只为你懂 2022-11-10 14:29 318阅读 0赞

这道题我检查了很多遍,但是并没有发现结果的任何错误。但是一直给我WA,最后发现是因为输出格式每一行多出了一个空格,为什么它不给我PE!!!???


UVA1225 数数字 Digit Counting

UVa题目PDF

题目描述

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

  1. 2
  2. 3
  3. 13

Sample Output

  1. 0 1 1 1 0 0 0 0 0 0
  2. 1 6 2 2 1 1 1 1 1 1

这道题还有一个要点,就是数组不要开太大,也不要消耗过多内存,做一个[10]的数组就够了,因为题目的限制是0B

在这里插入图片描述

正解:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. int a[10];
  6. int main()
  7. {
  8. int t;
  9. cin >> t;
  10. int n;
  11. while (t--)
  12. {
  13. cin >> n;
  14. memset(a, 0, sizeof(a));
  15. string str = "";
  16. for (int x = 1; x <= n; x++)
  17. {
  18. str += to_string(x);
  19. }
  20. for (int x = 0; x < str.size(); x++)
  21. {
  22. a[(str[x] - '0')] ++;
  23. }
  24. for (int x = 0; x < 9; x++)
  25. cout << a[x] << " ";
  26. cout << a[9];
  27. cout << endl;
  28. }
  29. return 0;
  30. }

发表评论

表情:
评论列表 (有 0 条评论,318人围观)

还没有评论,来说两句吧...

相关阅读

    相关 输入输出格式要求

    输入和输出命令是我们在写代码中用的最多的命令,下面我们来看几种输入与输出格式 输入 方法一:读入一个字符 在读入一个字符的时候,有人会选择以下方法 ![在这里

    相关 printf输出格式总结

      printf函数称为格式输出函数,其关键字最末一个字母f即为“格式”(format)之意。其功能是按用户指定的格式,把指定的数据显示到显示器屏幕上。 1 pri