hdu1716排列2(stl:next_permutation+优先队列)
2015-07-08 10:36
344 查看
排列2
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5437 Accepted Submission(s): 2072
Problem Description
Ray又对数字的列产生了兴趣:
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。
Input
每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。
Output
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
每组输出数据间空一行,最后一组数据后面没有空行。
Sample Input
1 2 3 4 1 1 2 3 0 1 2 3 0 0 0 0
Sample Output
1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 1123 1132 1213 1231 1312 1321 2113 2131 2311 3112 3121 3211 1023 1032 1203 1230 1302 1320 2013 2031 2103 2130 2301 2310 3012 3021 3102 3120 3201 3210
Source
2007省赛集训队练习赛(2)
还有比这道题格式更牛的吗?我都吐血了。。。
PE了7 8 次。。最后试一试的态度 竟然AC了。。
放一张输出的图片吧
感觉看了输出就懂的了这道题的格式。
全排+优先队列:
#include <stdio.h> #include <queue> #include <algorithm> using namespace std; int main() { int a[4]; priority_queue<int,vector<int>,greater<int> >s;//优先队列,greater换成less就是先出大的 for(int i=0;i<4;i++) scanf("%d",&a[i]); while(1) { if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[0]==0) break; do { int sum=0; for(int i=3;i>=0;i--) sum=sum*10+a[i]; if(sum>1000) s.push(sum); }while(next_permutation(a,a+4));//全排公式 for(int i=1;i<=9;i++) { if(s.top()/1000==i) { printf("%d",s.top()),s.pop(); while(s.top()/1000==i&&!s.empty()) { printf(" %d",s.top()); s.pop(); } printf("\n"); } } for(int i=0;i<4;i++)// scanf("%d",&a[i]);// if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[0]==0)// break;// else// printf("\n");//这么多 我仅仅是为了格式。。这道题 多么恶心。。 } return 0; }
四个for循环做的:
#include<stdio.h> #include <string.h> #include <algorithm> using namespace std; int main() { int a[4],q,stamp[30]; scanf("%d %d %d %d",&a[0],&a[1],&a[2],&a[3]); while(1) { if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[1]==0) break; memset(stamp,0,sizeof(stamp)); q=0; for(int i=0;i<4;i++) for(int j=0;j<4;j++) for(int k=0;k<4;k++) for(int t=0;t<4;t++) { if(i!=j&&i!=k&&i!=t&&j!=k&&j!=t&&k!=t&&a[i]>0) stamp[q++]=a[i]*1000+a[j]*100+a[k]*10+a[t]; } sort(stamp,stamp+q); for(int i=1;i<=9;i++) { for(int j=0;j<q;j++) { if(stamp[j]!=stamp[j+1]&&stamp[j]/1000==i) { printf("%d",stamp[j]),j++; while(stamp[j]/1000==i) { while(stamp[j]==stamp[j+1]) j++; printf(" %d",stamp[j]); j++; } printf("\n"); } } } scanf("%d %d %d %d",&a[0],&a[1],&a[2],&a[3]); if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[1]==0) break; else printf("\n"); } return 0; }
还有个搜索可以做,请小伙伴
们自己做吧~
相关文章推荐
- map循环取值 keyset与entryset区别
- MarkLine
- Java float保留两位小数或多位小数
- jquery精简选项卡
- Java每日学习笔记1
- iOS 支付宝应用(备用参考2)
- Xmpp初始连接交互内容
- Android的Location功能
- 向左无缝滚动
- IOS开发-文件管理(二)
- oracle存储过程调试
- PHP使用CURL模拟登录的方法
- Python抓取百度查询结果的方法
- 巫启贤《太傻》背后的故事
- Android contentprovider
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
- Java I/O 操作及优化建议
- 简单工厂模式
- 快速排序算法
- python初学-01数据类型