Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4. public class ccc06j3 {
  5.  
  6. public static void main(String[] args) {
  7. HashMap<Character, String> map = new HashMap<Character, String>();
  8.  
  9. // Primitive Reference
  10. // byte Byte
  11. // short Short
  12. // int Integer
  13. // long Long
  14. // double Double
  15. // float Float
  16. // char Character
  17. // boolean Boolean
  18.  
  19. // key Character
  20. // value String
  21.  
  22. // 'a' "2"
  23. // 'b' "22"
  24. // 'c' "222"
  25.  
  26. map.put('a',"2");
  27. map.put('b',"22");
  28. map.put('c',"222");
  29. map.put('d',"3");
  30. map.put('e',"33");
  31. map.put('f',"333");
  32. map.put('g',"4");
  33. map.put('h',"44");
  34. map.put('i',"444");
  35. map.put('j',"5");
  36. map.put('k',"55");
  37. map.put('l',"555");
  38. map.put('m',"6");
  39. map.put('n',"66");
  40. map.put('o',"666");
  41. map.put('p',"7");
  42. map.put('q',"77");
  43. map.put('r',"777");
  44. map.put('s',"7777");
  45. map.put('t',"8");
  46. map.put('u',"88");
  47. map.put('v',"888");
  48. map.put('w',"9");
  49. map.put('x',"99");
  50. map.put('y',"999");
  51. map.put('z',"9999");
  52.  
  53. while (true) {
  54. //input String S
  55. Scanner sc = new Scanner(System.in);
  56. String S = sc.next();
  57. if (S.equals("halt")) {
  58. break;
  59. }
  60. //read char one by one
  61. char pre = ' ';
  62. int click = 0;
  63. for (int i=0; i<S.length(); i++) {
  64. char C = S.charAt(i); //current char
  65. String code = map.get(C); //mapping numbers
  66. click += code.length();
  67. //has pause
  68. if (code.charAt(0)==pre) {
  69. click+=2; //add 2 second pause
  70. }
  71. pre = code.charAt(0);
  72. }
  73. System.out.println(click);
  74. }
  75.  
  76. //"abca"
  77. //create temporary variable char pre=' ';
  78. //first char is a map to "2" "2" first char compare with pre
  79. //if they are equals, then add 2 second pause
  80. //pre = "2".charAt(0);
  81. //second char is b map to "22" "22" first char compare with pre
  82. //they are equal then add 2 second pause
  83. //pre = "22".charAt(0)
  84. //third char is c map to "3" "3" first char compare with pre
  85. //they are not equals, don't add anything for pause
  86.  
  87. //"666"
  88.  
  89.  
  90.  
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement