hdu 2062(DFS)

这题仔细想想就能看出来是DFS,代码好烂,以后要学学怎么把代码弄的好一点了。算法第一篇。

 

 

AC代码
   
     
1 #include < stdio.h >
2 __int64 nn, in [ 30 ]; // 记录哪些输出过
3 __int64 num[ 30 ]; // 打表每个i总的子序列个数
4
5 void DFS(__int64 n,__int64 m)
6 {
7 __int64 num1,j,numm,temp,i;
8 num1 = m / ( num[n - 1 ] + 1 ); // 顺序找到该输出的是哪个数
9 temp = m % ( num[n - 1 ] + 1 ); if (temp) num1 ++ ;
10 for (i = 1 ;i <= nn; i ++ ){ // 找到当前输出值
11 if ( in [i] == 0 ) num1 -- ;
12 if (num1 == 0 )
13 { printf( " %I64d " , i); in [ i ] ++ ; break ;}
14 }
15 if (m % (num[n - 1 ] + 1 ) == 0 )
16 m = num[n - 1 ] + 1 ;
17 else
18 m = m % (num[n - 1 ] + 1 );
19 if (m > 1 ) DFS(n - 1 ,m - 1 );
20 }
21 int main()
22 {
23 __int64 i,n,m,temp;
24
25 num[ 1 ] = 1 ;num[ 2 ] = 4 ;
26 for (i = 3 ;i <= 20 ;i ++ )
27 num[i] = (num[i - 1 ] + 1 ) * i;
28 while (scanf( " %I64d %I64d " , & n, & m) != EOF)
29 {
30 if (m == 1 ) {printf( " %d\n " , 1 ); continue ;}
31 nn = n;
32 for (i = 0 ;i <= 25 ;i ++ ) in [i] = 0 ; // cun zai?
33 for (i = 0 ;i < 20 ;i ++ ){
34 if (m > num[i] && m <= num[i + 1 ]){
35 temp = m / (num[n - 1 ] + 1 );
36 if (m % (num[n - 1 ] + 1 )) temp ++ ;
37 break ;
38 }
39 }
40 in [temp] ++ ;
41 printf( " %I64d " ,temp);
42 if (m % (num[n - 1 ] + 1 ) == 0 )
43 m = num[n - 1 ] + 1 ;
44 else
45 m = m % (num[n - 1 ] + 1 );
46 if (m > 1 )
47 DFS(n - 1 ,m - 1 );
48 printf( " \n " );
49
50 }
51 return 0 ;
52 }

 

 

 

 

 

 

你可能感兴趣的:(HDU)