zoukankan      html  css  js  c++  java
  • UVa1225

    Digit Counting

    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

    Submit Status Practice UVA 1225 uDebug

    Description

     

    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

    题意:

           输入一个正整数N,你需要不间断地列出从一到N的所有正整数,统计列举结果中数字0~9分别出现的次数。

    输入:

           情况数T,之后T行每行一个正整数N(N < 10000)

    输出:

           依次输出数字0~9的出现次数。

    分析:

           简单模拟,for循环从1到N,每次判断当前的数是几位数并分离每个数位的数字即可。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 using namespace std;
     5 const int MAX_N = 1e5 - 1;
     6 int N;
     7 int a[13];
     8 int main(){
     9     int T; cin >> T;
    10     while(T--){
    11         scanf("%d",&N);
    12         memset(a,0,sizeof a);
    13         //printf("%d %d %d %d
    ",a,b,c,d);
    14         for(int i = 1; i <= N ; i++){
    15             if(i < 10){
    16                 a[i]++;
    17             }
    18             else if(i >= 10 && i < 100){
    19                 int a_ = i / 10;
    20                 int b = (i - a_ * 10);
    21                 a[a_]++; a[b]++;
    22             }
    23             else if(i >= 100 && i < 1000){
    24                 int a_ = i / 100;
    25                 int b = (i - a_ * 100) / 10;
    26                 int c = (i - a_ * 100 - b * 10);
    27                 a[a_]++; a[b]++; a[c]++;
    28             }
    29             else if(i >= 1000 && i < 10000){
    30                 int a_ = i / 1000;
    31                 int b = (i - a_ * 1000) / 100;
    32                 int c = (i - a_ * 1000 - b * 100) / 10;
    33                 int d = (i - a_ * 1000 - b * 100 - c * 10);
    34                 a[a_]++; a[b]++; a[c]++; a[d]++;
    35             }
    36         }
    37         for(int i = 0 ; i <= 9 ; i++)
    38             printf("%d%c",a[i],i == 9 ? '
    ' : ' ');
    39     }
    40     return 0;
    41 }
    View Code
  • 相关阅读:
    Exchange 2013学习笔记七:邮箱导入导出
    Exchange 2013学习笔记六:邮件归档
    Exchange 2013学习笔记五:资源邮箱
    Exchange 2013学习笔记四:新建用户邮箱
    Exchange 2013学习笔记三:创建邮箱数据库
    Exchange 2013学习笔记二:邮箱访问
    Exchange 2013学习笔记一:Exchange Server 2013安装
    域学习笔记十五:在活动目录中清除彻底失败的域控制器信息
    域学习笔记十四:迁移操作主控
    域学习笔记十三:将域控制器迁移到新买的服务器
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5769229.html
Copyright © 2011-2022 走看看