欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

HDU1716(全排列)

发布时间:2025/4/9 编程问答 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 HDU1716(全排列) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

排列2

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7151    Accepted Submission(s): 2723


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 题目不难,代码写得有点挫。 1 //2016.8.30 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <set> 7 8 using namespace std; 9 10 int a[4], vis[4], ans[4]; 11 set<int> s; 12 13 void dfs(int step) 14 { 15 if(step == 4) 16 { 17 int tmp = 1000*ans[0]+100*ans[1]+10*ans[2]+ans[3]; 18 s.insert(tmp); 19 return ; 20 } 21 for(int i = 0; i < 4; i++) 22 { 23 if(step == 0 && a[i] == 0)continue; 24 if(vis[i])continue; 25 vis[i] = 1; 26 ans[step] = a[i]; 27 dfs(step+1); 28 vis[i] = 0; 29 } 30 } 31 32 int main() 33 { 34 int pre, cnt = 0; 35 while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])) 36 { 37 if(!a[0]&&!a[1]&&!a[2]&&!a[3])break; 38 if(cnt)cout<<endl; 39 cnt = 1; 40 s.clear(); 41 sort(a, a+4); 42 memset(vis, 0, sizeof(vis)); 43 dfs(0); 44 for(set<int>::iterator it = s.begin(); it != s.end(); it++) 45 { 46 int tmp = *it; 47 if(it==s.begin()){ 48 cout<<tmp; 49 pre = tmp/1000; 50 }else 51 { 52 if(tmp/1000 == pre)cout<<" "<<tmp; 53 else { 54 cout<<endl<<tmp; 55 pre = tmp/1000; 56 } 57 } 58 } 59 cout<<endl; 60 } 61 62 return 0; 63 }

 

 

转载于:https://www.cnblogs.com/Penn000/p/5823879.html

总结

以上是生活随笔为你收集整理的HDU1716(全排列)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。