首页 > 代码库 > 字母对应数字

字母对应数字

  1. /*Description
  2. Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino‘s by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens‘‘ number 3-10-10-10.
  3. The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:
  4. A, B, and C map to 2
  5. D, E, and F map to 3
  6. G, H, and I map to 4
  7. J, K, and L map to 5
  8. M, N, and O map to 6
  9. P, R, and S map to 7
  10. T, U, and V map to 8
  11. W, X, and Y map to 9
  12. There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.
  13. Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)
  14. Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.
  15. Input
  16. The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.
  17. Output
  18. Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:
  19. No duplicates.
  20. */
  21. #include <iostream>
  22. #include <math.h>
  23. #include <string>
  24. #include <map>
  25. using namespace std;
  26. #include <fstream>
  27. //ifstream fin("C:\\Users\\yw\\Desktop\\E\\E.2.dat");
  28. //ofstream fout("result1.txt");
  29. //#define cout fout
  30. //#define cin fin
  31. int main()
  32. {
  33. int n;
  34. cin>>n;
  35. //getchar();
  36. map<string,int>result; //存结果对应的map的
  37. map< string,int>::iterator iter; //建立反向迭代器
  38. map<char,int>num;
  39. // char num[91];
  40. num[‘A‘ ]=num[‘B‘]=num[‘C‘]=‘2‘; //字母对应的数字
  41. num[‘D‘ ]=num[‘E‘]=num[‘F‘]=‘3‘;
  42. num[‘G‘ ]=num[‘H‘]=num[‘I‘]=‘4‘;
  43. num[‘J‘ ]=num[‘K‘]=num[‘L‘]=‘5‘;
  44. num[‘M‘ ]=num[‘N‘]=num[‘O‘]=‘6‘;
  45. num[‘P‘ ]=num[‘R‘]=num[‘S‘]=‘7‘;
  46. num[‘T‘ ]=num[‘U‘]=num[‘V‘]=‘8‘;
  47. num[‘W‘ ]=num[‘X‘]=num[‘Y‘]=‘9‘;
  48. while(n--)
  49. {
  50. char word[100]; //注意字符可能很多
  51. cin>>word;
  52. char *p=word;
  53. string temp="";
  54. int count=0;
  55. while( (*p)!=‘\0‘) //转换数字
  56. {
  57. if( (*p>=‘A‘&&*p<‘Q‘)||(*p>‘Q‘&&*p<‘Z‘) )
  58. {
  59. temp+=num[*p];
  60. count++; p++;
  61. if(count==3)
  62. temp+=‘-‘;
  63. }
  64. else if( *p>=‘0‘&&*p<=‘9‘ )
  65. {
  66. temp+=(*p);
  67. count++; p++;
  68. if(count==3)
  69. temp+=‘-‘;
  70. }
  71. else p++;
  72. }
  73. iter=result.find(temp); //查找时候在map里面有
  74. if(iter==result.end() )
  75. result[temp]=1;
  76. else
  77. result[temp]=iter->second+1; //有就数字加一
  78. }
  79. int ok=1;
  80. for(iter = result.begin(); iter != result.end(); iter++) //利用方向迭代器输出数据
  81. {
  82. if(iter->second>1)
  83. {cout<<iter->first<<‘ ‘<<iter->second<<endl; ok=0;}
  84. }
  85. if(ok) cout<<"No duplicates."<<endl;
  86. return 0;
  87. }



来自为知笔记(Wiz)


附件列表

     

    字母对应数字