HDU1716(全排列)
生活随笔
收集整理的這篇文章主要介紹了
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(全排列)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用日志记录功能查看PHP扩展的执行过程
- 下一篇: Java加密算法 AES