//第五屆藍橋杯軟件類國賽真題-C-A-6_撲克序列/*【題目】標題:撲克序列 A A 2 2 3 3 4 4, 一共4對撲克牌。請你把它們排成一行。 要求:兩個A中間有1張牌,兩個2之間有2張牌,兩個3之間有3張牌,兩個4之間有4張牌。 請填寫出所有符合要求的排列中,字典序最小的那個。例如:22AA3344 比 A2A23344 字典序小。當然,它們都不是滿足要求的答案。請通過瀏覽器提交答案?!癆”一定不要用小寫字母a,也不要用“1”代替。字符間一定不要留空格。 *//*【解題思路】解法一:調用C++算法庫里有的api //在字符串中從下標位置為pos開始查找字符c,若找到則返回下標位置 int string::find(Char c, int pos) //bool next_permutation(_BidirectionalIterator _first,_BidirectionalIterator _last) next_permutation函數則是將按字母表順序生成給定序列的下一個較大的排列, 直到整個序列為降序為止。 PRev_permutation函數與之相反,是生成給定序列的上一個較小的排列。 答案:2342A3A4*/#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;int main() { cout<<"符合要求的排列為:"<<endl; string s = "223344AA"; do { unsigned iab = s.find("A", 0); unsigned iae = s.find("A", iab + 1); unsigned i2b = s.find("2", 0); unsigned i2e = s.find("2", i2b + 1); unsigned i3b = s.find("3", 0); unsigned i3e = s.find("3", i3b + 1); unsigned i4b = s.find("4", 0); unsigned i4e = s.find("4", i4b + 1); if(iae - iab == 2 && i2e - i2b == 3 && i3e - i3b == 4 && i4e - i4b == 5) { cout << s << endl; } } while(next_permutation(s.begin(), s.end())); return 0; }
新聞熱點
疑難解答