赞
踩
(3)输入的顺子已经经过从小到大排序,因此不用再排序了.
本题需要解决同类型手牌的大小比较和不同类型手牌判断4张相同牌组成的炸弹和2张joker组成的王炸。对比优秀代码可以发现,本文在string类中find、substr、npos等运用上欠缺,此外,在判断手牌牌面大小是采用switch语句相比字符串find方法略显繁琐,这是需要改进和学习的地方,具体的程序源码如下所示:
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <vector>
- using namespace std;
- int encode(char a)
- {
- int c=-1;
- switch(a)
- {
- case '1':
- c=10;
- break;
- case 'J':
- c=11;
- break;
- case 'Q':
- c=12;
- break;
- case 'K':
- c=13;
- break;
- case 'A':
- c=14;
- break;
- case '2':
- c=15;
- break;
- case 'j':
- c=16;
- break;
- default:
- c=a-'0';
- break;
- }
- return c;
- }
- bool compare(char a,char b)
- {
- int c=encode(a);
- int d=encode(b);
- return c>d;
-
- }
- int cardsize(string str)
- {
- int pos=0;
- int i=0;
- int count=1;
- while((pos=str.find(' ',i))!=string::npos)
- {
- i=pos+1;
- count++;
- }
- return count;
- }
- int main()
- {
- string input,first,second,result;
- getline(cin,input);
- int position=input.find('-');
- first=input.substr(0,position);
- second=input.substr(position+1);
- int firstsize=cardsize(first);
- int secondsize=cardsize(second);
- if(firstsize==secondsize)//若两者属于相同的长度
- {
- if(first=="JOKER"||second=="JOKER")
- {
- cout<<"JOKER"<<endl;
- return 0;
- }
- else if(first=="JOKER joker"||second=="JOKER joker")
- {
- cout<<"JOKER joker"<<endl;
- return 0;
- }
- else
- NULL;
- char a=first.at(0);
- char b=second.at(0);
- if(compare(a,b))//若a大于b
- {
- result=first;
- }
- else
- {
- result=second;
- }
- }
- else//若两者长度不同,取炸弹和王炸作为最大值
- {
- if(first=="JOKER joker"||first=="joker JOKER")
- {
- result=first;
- }
- else if(second=="JOKER joker"||second=="joker JOKER")
- {
- result=second;
- }
- else if(cardsize(first)==4)
- {
- result=first;
- }
- else if(cardsize(second)==4)
- {
- result=second;
- }
- else
- result="ERROR";
- }
- cout<<result<<endl;
- return 0;
- }
附加优秀程序源码:
<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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。