UVa-1225 Digit Counting(数数字)

对于一个大于1且小于10000的整数N,我们定义一个唯一与之相关联的序列。例如,若N为13,则该序列为12345678910111213。现要求对于一个输入值N,记录这个序列中每个数字出现了多少次。

输入格式:先是一个整数以表明要输入的整数个数,之后每行给出一个大于1且小于10000的整数。

输出格式:每行输出每个数字出现的个数。如果输入为13,那么输出为1 6 2 2 1 1 1 1 1 1。

具体见https://cn.vjudge.net/problem/uva-1225

思路:没有多加思考,直接上。取序列,再遍历。这次用了一个vector存入答案,最后统一输出。

我的C++代码:

 1  #include <iostream>
 2  #include <string>
 3  #include <vector>
 4  #include <array>
 5  using namespace std;
 6  int main()
 7  {
 8      int T, n;
 9      cin >> T;
10      vector<string> re;
11      for (int i = 0;i < T;++i)
12      {
13          array<int, 10> dig{ 0 };//这里使用int dig[10]={ 0 }即可,我仅仅是试试刚学的标准库容器...
14          cin >> n;
15          string s, s2;
16          for (int j = 1;j <= n;++j)
17          {
18              s += to_string(j);
19          }
20          for (auto x : s)
21          {
22              dig[x - ‘0‘]++;
23          }
24          bool t = true;
25          for (auto x : dig)
26          {
27              if (t)
28              {
29                  t=!t;
30                  s2 += to_string(x);
31              }
32              else s2 += " " + to_string(x);
33          }
34          re.push_back(s2);
35      }
36      for (auto x : re)cout << x << endl;
37      return 0;
38  }

原文地址:https://www.cnblogs.com/starpast/p/8398552.html

时间: 2025-01-05 17:15:48

UVa-1225 Digit Counting(数数字)的相关文章

UVa 1225 Digit Counting --- 水题

UVa 1225 题目大意:把前n(n<=10000)个整数顺次写在一起,12345678910111213...,数一数0-9各出现多少字 解题思路:用一个cnt数组记录0-9这10个数字出现的次数,先将cnt初始化为0,接着让i从1枚举到n, 对每个i,处理以活的i的每一个位置上的数,并在相应的cnt下标上+1 最后输出cnt数组即可 /* UVa 1225 Digit Counting --- 水题 */ #include <cstdio> #include <cstring

UVa 1225 / UVALive 3996 Digit Counting 数数字(字符统计)

Digit Counting Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting

UVA 1225 Digit Counting(统计数位出现的次数)

Digit Counting Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu SubmitStatus Description Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting

紫书第三章练习题:UVA 1225 Digit Counting by 15邱盼威

来源:http://m.blog.csdn.net/article/details?id=70861055 Trung is bored with his mathematicshomeworks. He takes a piece of chalk and starts writing a sequence ofconsecutive integers starting with 1 to N (1 < N < 10000). After that, hecounts the number

UVa 1225 - Digit Counting

题目:输出1~N所有数字中,0~9出现的总次数. 分析:简单题.打表计算,查询输出即可. 说明:最近事情好多啊╮(╯▽╰)╭. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int f[10000][10]; in

UVa 1225 - Digit Counting - ACM/ICPC Danang 2007 解题报告

1.题目大意 把前n$n\le 10000$个整数顺次写在一起:12345678910111213……计算0~9各出现了多少次. 2.思路 第一想法是打表,然而觉得稍微有点暴力.不过暂时没有想到更好的办法了,写完看了一下其它人的思路好像也差不多是打表的思路. 3.应注意的问题 (1)首先是格式问题,我第一次提交的时候PE了,因为没有意识到空格也会有影响.最开始我的最后一段代码是: for(i=0;i<10;i++) printf("%d ",s[n][i]); printf(&q

UVa 1583 Digit Generator(数)

For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 

(UVA)1225 --Digit Counting(数数字)

题目链接:http://vjudge.net/problem/UVA-1225 #include <iostream> #include <cstring> #include <cstdio> using namespace std; int f[10000][10]; int main() { memset(f, 0, sizeof(f)); for (int i = 1 ; i < 10000 ; ++ i) { for (int j = 0 ; j <

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 <tex2html_verbatim_mark>(1 < N < 10000) <tex2html_verbatim_mark>. After that, he counts t

UVa1587.Digit Counting

题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=247&page=show_problem&problem=3666 13764622 1225 Digit Counting Accepted C++11 0.035 2014-06-18 07:44:02 1225 - Digit Counting Time limit: 3.000 seconds Tru