题目描述:
A A 2 2 3 3 4 4, 一共4对扑克牌。请你把它们排成一行。
要求:两个A中间有1张牌,两个2之间有2张牌,两个3之间有3张牌,两个4之间有4张牌。
请填写出所有符合要求的排列中,字典序最小的那个。
例如:22AA3344 比 A2A23344 字典序小。当然,它们都不是满足要求的答案。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
string s = "223344AA";
do {
int iab = s.find("A", 0); //找到从下标0(包括下标为0时)开始的第一个A
int iae = s.find("A", iab + 1);
int i2b = s.find("2", 0);
int i2e = s.find("2", i2b + 1);
int i3b = s.find("3", 0);
int i3e = s.find("3", i3b + 1);
int i4b = s.find("4", 0);
int 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())); //在algorithm里
}