C++全排列函数

tech2022-10-28  103

升序:next_permutation(start,end)

do { printf("%d %d %d\n",s[0],s[1],s[2]); } while(next_permutation(s,s+3));

降序:prev_permutation(start,end)

do { printf("%d %d %d\n",s[0],s[1],s[2]); } while(prev_permutation(s,s+3));

相关实列:

#include<iostream> #include<cstdlib> #include<algorithm> #include<string> using namespace std; int ans = 0; int main() { string s = "123456789"; /**下面为next_permutation的用法*/ do { for (int i = 1; i <= 7; i++) { string a = s.substr(0, i); int inta = atoi(a.c_str()); if (inta > 100) break; for (int j = 1; j <= 9-i-1; j++) { string b = s.substr(i,j); string c = s.substr(i+j); int intb = atoi(b.c_str()); int intc = atoi(c.c_str()); if (intb%intc==0&&inta + intb / intc==100) { cout << inta << ' ' << intb << ' ' << intc << endl; ans++; } } } } while (next_permutation(s.begin(), s.end())); cout << ans << endl; /**下面为prev_permutation的用法*/ /*do { printf("%d %d %d\n",s[0],s[1],s[2]); } while(prev_permutation(s,s+3));*/ system("pause"); return 0; }
最新回复(0)