2014-07-10 11:41:47
题意&思路:一个长为n的串里有h个1,n-h个0,输出全排列,next_permutation。不说啥了,注意输出。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 7 int main(){ 8 char s[20]; 9 int Case,n,h; 10 scanf("%d",&Case); 11 while(Case--){ 12 scanf("%d %d",&n,&h); 13 for(int i = 0; i < 20; ++i) s[i] = '0'; 14 s[n] = '\0'; 15 int len = n - 1; 16 while(h--) 17 s[len--] = '1'; 18 cout << s << endl; 19 while(next_permutation(s,s + n)) cout << s << endl; 20 if(Case) cout << endl; 21 } 22 return 0; 23 }
本文介绍了一个使用 C++ 实现的 next_permutation 函数来生成长度为 n 的字符串的所有可能排列的方法,该字符串包含 h 个 '1' 和 n-h 个 '0'。通过示例代码展示了如何初始化字符串并输出所有可能的排列。
1113

被折叠的 条评论
为什么被折叠?



