歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java程序練習-Mobile Number

Java程序練習-Mobile Number

日期:2017/3/1 11:16:44   编辑:Linux編程
描述
One day, an alpc found a modified telephone booklet in our laboratory recording almost all alpcs' mobile phone numbers. Now he asks you to verify the numbers.

We call the number of different digitals at the same position of two phone numbers: 'difference'.

Number 1: 1 3 6 0 0 0 0 0 0 0 0
Number 2: 1 4 6 0 0 0 0 0 0 0 1
Difference: 0+1+0+0+0+0+0+0+0+0+1 = 2

Search the booklet and find out the numbers between the difference A and B to the given correct phone numbers.

輸入
The first line of the input is two integers: n and m. n is the numbers counted on the booklet, while m is the given correct phone numbers (0 < n ≤ 1000, 0 < m ≤ 200).

Then follow n lines. On each line is a phone number in the booklet which are all distinct. Then m lines, on each line there is a correct given phone number and difference A and B (0 ≤ A, B ≤ 11). Note: all the input phone numbers will be valid phone number: 11 digitals per number.

輸出
For each given correct phone number, print "Number *:"(* should be replaced by the order this number shows up).

Then print all the within-difference number in the booklet. Ordered as the same as in the booklet.

Then follows a line printed "Total: *", * should be replaced by the total number of the within-difference numbers.

Then print a blank line after each given correct phone number.

樣例輸入
4 2
13600000000
13500000001
13700000001
15500000000
13600000000 1 1
13700000001 1 2
樣例輸出
Number 1:
Total: 0

Number 2:
13600000000
13500000001
Total: 2

參考代碼

  1. import java.util.*;
  2. public class Main {
  3. public final static int N = 11;
  4. public static void main(String[] args) throws Exception {
  5. Scanner cin = new Scanner(System.in);
  6. int size = cin.nextInt(),cases = cin.nextInt();
  7. cin.nextLine();
  8. String data[] = new String[size];
  9. int i,j;
  10. for(i = 0;i < size;++ i){
  11. data[i] = cin.nextLine();
  12. }
  13. for(j = 1;j <= cases;++ j){
  14. int total = 0;
  15. String ts = cin.next();
  16. char ts2c[] = ts.toCharArray();
  17. int ta = cin.nextInt();
  18. int tb = cin.nextInt();
  19. int temp;
  20. if(ta > tb){
  21. temp = ta;
  22. ta = tb;
  23. tb = temp;
  24. }
  25. cin.nextLine();
  26. System.out.println("Number "+j+":");
  27. for(i = 0;i < size;++ i){
  28. int diff = 0;
  29. char data2c[] = data[i].toCharArray();
  30. for(int cp = 0;cp < N;++ cp){
  31. if(ts2c[cp]!= data2c[cp]){
  32. diff ++;
  33. }
  34. }
  35. if(diff >= ta && diff <= tb){
  36. total ++;
  37. System.out.println(data[i]);
  38. }
  39. }
  40. System.out.println("Total: "+total);
  41. System.out.println();
  42. }
  43. }
  44. }
Copyright © Linux教程網 All Rights Reserved