zoukankan      html  css  js  c++  java
  • 课后题 3-3 水题

    Digit Counting
    Time Limit: 3000MS        Memory Limit: Unknown        64bit IO Format: %lld & %llu
    Submit
     
    Status
    
    Description
    Download as 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 
    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

       给你一个数字   从1 开始到这个数字  意见 一共出现了 多少个  1 -  9   统计一下       n<10000  

     1 #include <iostream>  
     2 #include <cstdlib>  
     3 #include <cstring>  
     4 #include <cstdio>  
     5 using namespace std;  
     6 char str[104];  
     7 int main()  
     8 { 
     9     int n; 
    10     while (scanf("%d",&n)!=EOF) 
    11     {
    12         while (n--)
    13         { 
    14             scanf("%s",str); 
    15             int len = strlen(str);  
    16             for (int k,i = 1 ; i <= len ; ++ i)       //字符的长度           i是   循环 的长度
    17             {
    18                 if (len%i == 0)                           //  如果是循环的话  那么一定是 可以整除的.
    19                 { 
    20                     for (k = i ; k < len ; ++ k)    //从第二个字符开始比较 
    21                         if (str[k] != str[k%i])   //  不相等的话跳出去                   //如果确定了字符串长度 那么 可以一直贴合到 k=len 
    22                             break;
    23                 } 
    24                 if (k == len)                //知道最后 都不知道  循环长度 那么 字符长度就是  循环长度
    25                 { 
    26                     printf("%d
    ",i); 
    27                     break; 
    28                 }  
    29             }  
    30             if (n)
    31                 printf("
    ");  
    32         }    
    33     }  
    34     return 0;
    35 }
  • 相关阅读:
    Leetcode 290 Word Pattern
    Leetcode 205 Isomorphic Strings
    Leetcode 345 Reverse Vowels in a String
    Leetcode 151 Reverse Words in a String
    Leetcode 344 Reverse String
    Leetcode 383 Ransom Note
    leetcode 387 First Unique Character in a String
    反码补码和位运算
    SpringBoot进阶
    布隆过滤器
  • 原文地址:https://www.cnblogs.com/A-FM/p/5168019.html
Copyright © 2011-2022 走看看