ZOJ2971 Give Me the Number 【模拟】

àì夳堔傛蜴生んèń 2021-12-10 16:45 306阅读 0赞

这道题目使用Map。 然后一次性遍历下来即可。 QAQ

注意初始化的时候小心点不要错..

Source Code:

  1. //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <stack>
  8. #include <string>
  9. #include <map>
  10. #include <set>
  11. #include <list>
  12. #include <queue>
  13. #include <vector>
  14. #include <algorithm>
  15. #define Max(a,b) (((a) > (b)) ? (a) : (b))
  16. #define Min(a,b) (((a) < (b)) ? (a) : (b))
  17. #define Abs(x) (((x) > 0) ? (x) : (-(x)))
  18. #define MOD 1000000007
  19. #define pi acos(-1.0)
  20. using namespace std;
  21. typedef long long ll ;
  22. typedef unsigned long long ull ;
  23. typedef unsigned int uint ;
  24. typedef unsigned char uchar ;
  25. template<class T> inline void checkmin(T &a,T b){
  26. if(a>b) a=b;}
  27. template<class T> inline void checkmax(T &a,T b){
  28. if(a<b) a=b;}
  29. const double eps = 1e-7 ;
  30. const int N = 210 ;
  31. const int M = 1100011*2 ;
  32. const ll P = 10000000097ll ;
  33. const int MAXN = 100000 ;
  34. const int INF = 0x3f3f3f3f ;
  35. const int MAX = 10500 ;
  36. map <string, int> g;
  37. void init(){
  38. g["zero"] = 0, g["one"] = 1, g["two"] = 2, g["three"] = 3, g["four"] = 4;
  39. g["five"] = 5, g["six"] = 6, g["seven"] = 7, g["eight"] = 8, g["nine"] = 9;
  40. g["ten"] = 10, g["eleven"] = 11, g["twelve"] = 12, g["thirteen"] = 13, g["fourteen"] = 14;
  41. g["fifteen"] = 15, g["sixteen"] = 16, g["seventeen"] = 17, g["eighteen"] = 18, g["nineteen"] = 19;
  42. g["twenty"] = 20, g["thirty"] = 30, g["forty"] = 40, g["fifty"] = 50, g["sixty"] = 60;
  43. g["seventy"] = 70, g["eighty"] = 80, g["ninety"] = 90;
  44. }
  45. int main(){
  46. std::ios::sync_with_stdio(false);
  47. int i, j, t, k, u, v, x, y, numCase = 0;
  48. init();
  49. cin >> t;
  50. cin.get();//
  51. while(t--){
  52. string ss;
  53. getline(cin, ss, '\n');//
  54. ss += ' ';
  55. int len = ss.length();
  56. int num = 0, cur = 0, ans = 0;
  57. for(i = 0; i < len; ++i){
  58. if(ss[i] == ' '){
  59. string str = ss.substr(num, i - num);
  60. num = i + 1;
  61. if(str == "and"){
  62. continue;
  63. } else if(str == "million"){
  64. cur *= 1000000;
  65. ans += cur;
  66. cur = 0;
  67. } else if(str == "thousand"){
  68. cur *= 1000;
  69. ans += cur;
  70. cur = 0;
  71. } else if(str == "hundred"){
  72. cur *= 100;
  73. } else{
  74. cur += g[str];
  75. }
  76. }
  77. }
  78. ans += cur;
  79. cout << ans << endl;
  80. }
  81. return 0;
  82. }

转载于:https://www.cnblogs.com/wushuaiyi/p/3608716.html

发表评论

表情:
评论列表 (有 0 条评论,306人围观)

还没有评论,来说两句吧...

相关阅读