歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java程序練習-計數的夢

Java程序練習-計數的夢

日期:2017/3/1 11:16:38   编辑:Linux編程

計數的夢
時間限制: 10000ms內存限制: 1024kB
描述
Bessie 處於半夢半醒的狀態。過了一會兒,她意識到她好像在數羊,不能入睡。Bessie的大腦反應靈敏,仿佛真實地看到了她數過的一個又一個數。她開始注意每一個數碼:每一個數碼在計數的過程中出現過多少次?
給出兩個整數 M 和 N (1 <= M <= N <= 2,000,000,000 以及 N-M <= 500,000),求每一個數碼出現了多少次。
例如考慮序列 129..137: 129, 130, 131, 132, 133, 134, 135, 136, 137。統計後發現:
1x0 1x5
10x1 1x6
2x2 1x7
9x3 0x8
1x4 1x9
輸入
共一行,兩個用空格分開的整數 M 和 N
輸出
共一行,十個用空格分開的整數,分別表示數碼(0..9)在序列中出現的次數。
樣例輸入
129 137
樣例輸出
1 10 2 9 1 1 1 1 0 1

參考代碼
  1. /*
  2. * Dream Counting 2011-10-1 1:28PM Eric Zhou
  3. */
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. public class Main {
  8. public static int cnt[] = new int[10];
  9. public static void main(String[] args) throws IOException {
  10. BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
  11. String sn[] = cin.readLine().split(" ");
  12. int a = Integer.parseInt(sn[0]);
  13. int b = Integer.parseInt(sn[1]);
  14. int i = 0;
  15. for(i = a;i <= b;++ i){
  16. setcnt(i);
  17. }
  18. for(i = 0;i < 10;++ i)
  19. System.out.print(cnt[i]+" ");
  20. System.out.println();
  21. }
  22. private static void setcnt(int n) {
  23. if(n == 0)
  24. cnt[0] ++;
  25. while(n > 0){
  26. int p = n % 10;
  27. cnt[p] ++;
  28. n /= 10;
  29. }
  30. }
  31. }
Copyright © Linux教程網 All Rights Reserved