码迷,mamicode.com
首页 > 其他好文 > 详细

UVa-1225 Digit Counting(数数字)

时间:2018-02-01 14:42:52      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:ios   col   标准   数字   序列   using   定义   ...   cout   

对于一个大于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  }

 

UVa-1225 Digit Counting(数数字)

标签:ios   col   标准   数字   序列   using   定义   ...   cout   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!