zoukankan      html  css  js  c++  java
  • 紫书第三章练习题:UVA 1225 Digit Counting by 15 anruoxin

    来源: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 of times each digit (0 to 9) appears in the sequence. Forexample, with N = 13, the sequence is: 12345678910111213

    In this sequence, 0appears once, 1 appears 6 times, 2 appears 2 times, 3 appears 3 times, and eachdigit from 4 to 9 appears once. After playing for a while, Trung gets boredagain. He now wants to write a program to do this for him. Your task is to helphim with writing this program. Input The input file consists of several datasets. The first line of the input file contains the number of data sets whichis a positive integer and is not bigger than 20. The following lines describethe data sets. For each test case, there is one single line containing thenumber N. Output For each test case, write sequentially in one line the numberof digit 0, 1, . . . 9 separated by a space.

    Sample 

    Input 

    13 

    Sample 

    Output 

    0 1 1 1 0 0 0 0 0 0

    1 6 2 2 1 1 1 1 1 1

    链接:https://cn.vjudge.net/contest/160964#problem/A

    题意:输入一个N, 统计1~N中,0~9出现的次数;

    解题:

    因为N最大只有10000,组数<=20,可以直接循环统计0~9 的个数。

    代码如下:

     1 #include<stdio.h>
     2 int main()
     3 {
     4     int t,n;
     5     scanf("%d",&t);
     6     while(t--)
     7     {
     8         int a[10]={0},i;
     9         scanf("%d",&n);
    10         for(i=1;i<=n;i++)
    11         {
    12             int x=i;
    13             while(x)
    14             {
    15                 a[x%10]++;
    16                 x/=10;
    17             }
    18         }
    19         for(i=0;i<9;i++)
    20         printf("%d ",a[i]);
    21         printf("%d
    ",a[9]);
    22     }
    23 }
  • 相关阅读:
    启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
    处理textarea的空格和换行
    微信内置页实现自动拨号到页面的功能
    兼容ie的渐变css
    H5压缩图片上传(FileReader +canvas)
    css样式写一个小三角
    js禁止选中图片和文字
    js解决浏览器打印自动分页的问题
    IE8透明度问题
    在css加载完毕后自动判断页面是否加入css或js文件
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6777305.html
Copyright © 2011-2022 走看看