标签:memory new time strong cstring for 个数 puts from
要是没有next_permutation这个函数,这些题认为还不算特别水,只是也不一定,那样可能就会有对应的模板了。
反正正是由于next_permutation这个函数。这些题包含之前的POJ1226,都变得简单起来。
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 17486 | Accepted: 6970 |
Description
比方:n = 3,k=2 给出排列2 3 1,则它的下1个排列为3 1 2,下2个排列为3 2 1,因此答案为3 2 1。
Input
Output
Sample Input
3 3 1 2 3 1 3 1 3 2 1 10 2 1 2 3 4 5 6 7 8 9 10
Sample Output
3 1 2 1 2 3 1 2 3 4 5 6 7 9 8 10
直接用next_permutation这个函数就可以。
代码:
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <string> #include <cstring> using namespace std; int num[1030]; int main() { int Test,N,Q,i; cin>>Test; while(Test--) { scanf_s("%d%d",&N,&Q); for(i=0;i<N;i++) scanf_s("%d",&num[i]); for(i=1;i<=Q;i++) next_permutation(num,num+N); for(i=0;i<N;i++) printf("%d ",num[i]); printf("\n"); } return 0; }
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5072 | Accepted: 2923 |
Description
3 1 2 4 4 3 6 7 9 16Behind FJ‘s back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ‘s mental arithmetic capabilities.
Input
Output
Sample Input
4 16
Sample Output
3 1 2 4
Hint
题意是要输出N个数,这N个数是从1到N这些数的一个顺序。这种顺序依照杨辉三角的模式相加起来等于sum,输出相等时的第一个字典顺序。
一看到N是大于1小于10的我就想暴力了。。
。
代码:
#include <iostream> #include <string> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int main() { int i,n,sum,a[12]; cin>>n>>sum; for(i=1;i<=10;i++) a[i]=i; if(n==1) { cout<<1<<endl; } else if(n==2) { cout<<1<<" "<<2<<endl; } else if(n==3) { while(1*a[1]+2*a[2]+1*a[3]!=sum) { next_permutation(a+1,a+3+1); } cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<endl; } else if(n==4) { while(1*a[1]+3*a[2]+3*a[3]+1*a[4]!=sum) { next_permutation(a+1,a+4+1); } cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<endl; } else if(n==5) { while(1*a[1]+4*a[2]+6*a[3]+4*a[4]+1*a[5]!=sum) { next_permutation(a+1,a+5+1); } cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<endl; } else if(n==6) { while(1*a[1]+5*a[2]+10*a[3]+10*a[4]+5*a[5]+1*a[6]!=sum) { next_permutation(a+1,a+n+1); } cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<endl; } else if(n==7) { while(1*a[1]+6*a[2]+15*a[3]+20*a[4]+15*a[5]+6*a[6]+1*a[7]!=sum) { next_permutation(a+1,a+n+1); } cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<endl; } else if(n==8) { while(1*a[1]+7*a[2]+21*a[3]+35*a[4]+35*a[5]+21*a[6]+7*a[7]+1*a[8]!=sum) { next_permutation(a+1,a+n+1); } cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<endl; } else if(n==9) { while(1*a[1]+8*a[2]+28*a[3]+56*a[4]+70*a[5]+56*a[6]+28*a[7]+8*a[8]+1*a[9]!=sum) { next_permutation(a+1,a+n+1); } cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<" "<<a[9]<<endl; } else if(n==10) { while(1*a[1]+9*a[2]+36*a[3]+84*a[4]+126*a[5]+126*a[6]+84*a[7]+36*a[8]+9*a[9]+1*a[10]!=sum) { next_permutation(a+1,a+n+1); } cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<" "<<a[9]<<" "<<a[10]<<endl; } return 0; }
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 979 | Accepted: 717 |
Description
Input
Output
Sample Input
3 1 123 2 279134399742 3 987
Sample Output
1 132 2 279134423799 3 BIGGEST
还是直接使用next_permutation。这个函数是有返回值的,返回值是0时表示已经没有下一个字典顺序了,它要变成第一个字典顺序。返回值是1时表示还有字典顺序的下一个顺序,所以利用函数的这个性质就OK了。
代码:
#include <iostream> #include <string> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int main() { int Test,num; char s[100]; cin>>Test; while(Test--) { cin>>num>>s; cout<<num<<" "; int n=next_permutation(s,s+strlen(s)); if(n==0) cout<<"BIGGEST"<<endl; else cout<<s<<endl; } return 0; }
POJ1833 & POJ3187 & POJ3785 next_permutation应用
标签:memory new time strong cstring for 个数 puts from
原文地址:http://www.cnblogs.com/gccbuaa/p/7047616.html