当前位置:   article > 正文

上机笔试题3及程序源码_font-family: initial !important;

font-family: initial !important;
扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A,2各4张,小王1张,大王1张。牌面从小到大用如下字符和字符串表示(其中,小写joker表示小王,大写JOKER表示大王):) 
3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER 
输入两手牌,两手牌之间用“-”连接,每手牌的每张牌以空格分隔,“-”两边没有空格,如:4 4 4 4-joker JOKER
请比较两手牌大小,输出较大的牌,如果不存在比较关系则输出ERROR

基本规则:
(1)输入每手牌可能是个子,对子,顺子(连续5张),三个,炸弹(四个)和对王中的一种,不存在其他情况,由输入保证两手牌都是合法的,顺子已经从小到大排列;
(2)除了炸弹和对王可以和所有牌比较之外,其他类型的牌只能跟相同类型的存在比较关系(如,对子跟对子比较,三个跟三个比较),不考虑拆牌情况(如:将对子拆分成个子)
(3)大小规则跟大家平时了解的常见规则相同,个子,对子,三个比较牌面大小;顺子比较最小牌大小;炸弹大于前面所有的牌,炸弹之间比较牌面大小;对王是最大的牌;
(4)输入的两手牌不会出现相等的情况。

答案提示:
(1)除了炸弹和对王之外,其他必须同类型比较。
(2)输入已经保证合法性,不用检查输入是否是合法的牌。

(3)输入的顺子已经经过从小到大排序,因此不用再排序了.

本题需要解决同类型手牌的大小比较和不同类型手牌判断4张相同牌组成的炸弹和2张joker组成的王炸。对比优秀代码可以发现,本文在string类中find、substr、npos等运用上欠缺,此外,在判断手牌牌面大小是采用switch语句相比字符串find方法略显繁琐,这是需要改进和学习的地方,具体的程序源码如下所示:

  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. int encode(char a)
  7. {
  8. int c=-1;
  9. switch(a)
  10. {
  11. case '1':
  12. c=10;
  13. break;
  14. case 'J':
  15. c=11;
  16. break;
  17. case 'Q':
  18. c=12;
  19. break;
  20. case 'K':
  21. c=13;
  22. break;
  23. case 'A':
  24. c=14;
  25. break;
  26. case '2':
  27. c=15;
  28. break;
  29. case 'j':
  30. c=16;
  31. break;
  32. default:
  33. c=a-'0';
  34. break;
  35. }
  36. return c;
  37. }
  38. bool compare(char a,char b)
  39. {
  40. int c=encode(a);
  41. int d=encode(b);
  42. return c>d;
  43. }
  44. int cardsize(string str)
  45. {
  46. int pos=0;
  47. int i=0;
  48. int count=1;
  49. while((pos=str.find(' ',i))!=string::npos)
  50. {
  51. i=pos+1;
  52. count++;
  53. }
  54. return count;
  55. }
  56. int main()
  57. {
  58. string input,first,second,result;
  59. getline(cin,input);
  60. int position=input.find('-');
  61. first=input.substr(0,position);
  62. second=input.substr(position+1);
  63. int firstsize=cardsize(first);
  64. int secondsize=cardsize(second);
  65. if(firstsize==secondsize)//若两者属于相同的长度
  66. {
  67. if(first=="JOKER"||second=="JOKER")
  68. {
  69. cout<<"JOKER"<<endl;
  70. return 0;
  71. }
  72. else if(first=="JOKER joker"||second=="JOKER joker")
  73. {
  74. cout<<"JOKER joker"<<endl;
  75. return 0;
  76. }
  77. else
  78. NULL;
  79. char a=first.at(0);
  80. char b=second.at(0);
  81. if(compare(a,b))//若a大于b
  82. {
  83. result=first;
  84. }
  85. else
  86. {
  87. result=second;
  88. }
  89. }
  90. else//若两者长度不同,取炸弹和王炸作为最大值
  91. {
  92. if(first=="JOKER joker"||first=="joker JOKER")
  93. {
  94. result=first;
  95. }
  96. else if(second=="JOKER joker"||second=="joker JOKER")
  97. {
  98. result=second;
  99. }
  100. else if(cardsize(first)==4)
  101. {
  102. result=first;
  103. }
  104. else if(cardsize(second)==4)
  105. {
  106. result=second;
  107. }
  108. else
  109. result="ERROR";
  110. }
  111. cout<<result<<endl;
  112. return 0;
  113. }
附加优秀程序源码:

<div class="line number1 index0 alt2" style="line-height: 18px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(102, 102, 102); white-space: pre !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><code class="cpp preprocessor" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: gray !important; background: none !important;">#include <string></code></div><div class="line number2 index1 alt1" style="line-height: 18px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(102, 102, 102); white-space: pre !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><code class="cpp preprocessor" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: gray !important; background: none !important;">#include <vector></code></div><div class="line number3 index2 alt2" style="line-height: 18px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(102, 102, 102); white-space: pre !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><code class="cpp preprocessor" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: gray !important; background: none !important;">#include <iostream></code></div><div class="line number4 index3 alt1" style="line-height: 18px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(102, 102, 102); white-space: pre !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><code class="cpp keyword bold" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-weight: 700 !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: rgb(0, 102, 153) !important; background: none !important;">using</code> <code class="cpp keyword bold" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-weight: 700 !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: rgb(0, 102, 153) !important; background: none !important;">namespace</code> <code class="cpp plain" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: rgb(0, 0, 0) !important; background: none !important;">std;</code></div><div class="line number5 index4 alt2" style="line-height: 18px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(102, 102, 102); white-space: pre !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><code class="cpp preprocessor" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: gray !important; background: none !important;">#define PrintS1 {cout << s1 << endl;return 0;}</code></div><div class="line number6 index5 alt1" style="line-height: 18px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(102, 102, 102); white-space: pre !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><code class="cpp preprocessor" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: gray !important; background: none !important;">#define PrintS2 {cout << s2 << endl;return 0;}</code></div><div class="line number7 index6 alt2" style="line-height: 18px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(102, 102, 102); white-space: pre !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><code class="cpp color1 bold" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-weight: 700 !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: gray !important; background: none !important;">int</code> <code class="cpp plain" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: rgb(0, 0, 0) !important; background: none !important;">count(string & str){</code></div><div class="line number8 index7 alt1" style="line-height: 18px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(102, 102, 102); white-space: pre !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"><code class="cpp spaces" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; background: none !important;">    </code><code class="cpp color1 bold" style="white-space: nowrap; word-wrap: break-word; margin: 0px !important; padding: 0px !important; font-weight: 700 !important; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: gray !important; background: none !important;">int</code> <code clas
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/434369
推荐阅读
  

闽ICP备14008679号