Hdu 1716 (dfs), hdu1716dfs

Source: Internet
Author: User

Hdu 1716 (dfs), hdu1716dfs

Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1716

Arrange 2 Problem DescriptionRay and become interested in the column of numbers:
Four cards can be used to list many different 4-digit numbers, which must be output in ascending order.

 

Each group of Input data occupies one row, representing the number (0 <= number <= 9) on the four cards. If all four cards are 0, the Input ends.

 

Each group of cards is Output in the ascending order of all the four digits that can be composed of the four cards. The same row contains the same thousands of digits, each four-digit row is separated by spaces.
There is a blank row between each group of output data, and there is no blank row behind the last group of data.

 

Sample Input1 2 3 4 1 1 2 3 0 1 2 3 0 0 0

 

Sample Output1234 1243 1324 1342 1423 14322134 2143 2314 2341 2413 24313124 3142 3214 3241 3412 34214123 4132 4213 4231 4312 4321 1123 1132 1213 1231 1312 13212113 2131 23113112 3121 3211 1023 1032 1203 1230 1302 13202013 2031 2103 2130 2301 23103012 3021 3102 3120 3201 3210 a fully-arranged search question, the idea is to first arrange the four numbers from small to large, then find all the four possible digits, and finally remove the ones that do not match the question. 1 # include <cstdio> 2 # include <cmath> 3 # include <cstring> 4 # include <algorithm> 5 using namespace std; 6 7 int a [4], vis [4], s [25]; 8 int t, c; 9 10 void dfs (int num) 11 {12 if (num = 4) 13 {14 if (c> = 1000) 15 s [t ++] = c; 16 return; 17} 18 for (int I = 0; I <4; I ++) 19 {20 if (! Vis [I]) // tag access 21 {22 vis [I] = 1; 23 c = c * 10 + a [I]; 24 dfs (num + 1 ); 25 vis [I] = 0; 26 c = (c-a [I])/10; 27} 28} 29} 30 31 int main () 32 {33 int flag = 0; 34 while (scanf ("% d", & a [0], & a [1], & a [2], & a [3]) 35 {36 sort (a, a + 4); 37 if (a [3] = 0) 38 break; 39 if (flag) 40 {41 printf ("\ n"); 42} 43 flag = 1; 44 memset (vis, 0, sizeof (vis )); 45 t = c = 0; 46 dfs (0); 47 sort (s, s + t); 48 int temp; 49 printf ("% d ", S [0]); 50 temp = s [0]/1000; // remember thousands of 51 for (int I = 1; I <t; I ++) 52 {53 if (s [I] = s [I-1]) // because the order has passed, the same four digits are all together 54 continue; 55 if (s [I]/1000! = Temp) // The number of thousands is different. The line feed is 56 {57 printf ("\ n % d", s [I]); 58 temp = s [I]/1000; // record the new kilobytes 59} 60 else61 {62 printf ("% d", s [I]); 63} 64} 65 printf ("\ n "); 66} 67 return 0; 68}View Code


Today, I accidentally saw a great god blog. I used a next_permutation (a. begin (), a. end () function to easily complete the arrangement.

Attaches the great god blog link and his code: http://www.cnblogs.com/jackge/archive/2013/05/22/3093089.html

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 6 using namespace std; 7 8 int main () {9 10 // freopen ("input.txt", "r", stdin); 11 12 int a [5], tag = 0; 13 while (scanf ("% d", & a [0], & a [1], & a [2], & a [3]) {14 if (a [0] = 0 & a [1] = 0 & a [2] = 0 & a [3] = 0) 15 break; 16 if (tag) 17 printf ("\ n"); 18 tag = 1; 19 int flag = 1, tmp; 20 do {21 if (a [0] = 0) 22 continue; 23 if (flag) {24 printf ("% d ", a [0], a [1], a [2], a [3]); 25 flag = 0; 26} else if (tmp = a [0]) 27 printf ("% d", a [0], a [1], a [2], a [3]); 28 else29 printf ("\ n % d", a [0], a [1], a [2], a [3]); 30 tmp = a [0]; 31} while (next_permutation (a, a + 4); 32 printf ("\ n"); 33} 34 return 0; 35}View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.