hdu 1015 Safecracker

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1882    Accepted Submission(s): 996

     本题非常简单,却花费我好长的时间来调试,原因竟是我忘了一条RETURN 1 的语句;

思路:本题就是求怎样组合能够达到TAGET,因为题意给出求最大的,所以可以相对字符串进行处理,转化为整形的之后再排序,然后从大到小的搜索就行了,第一个符合条件的就输出,以后的就不用再搜索了

代码:

  
    
1 #include < stdio.h >
2 #include < string .h >
3 #include < stdlib.h >
4 #include < stack >
5 #include < math.h >
6   using namespace std;
7   int mark[ 15 ],t,a[ 15 ],mark1;
8 int com( const void * a, const void * b)
9 {
10 return * ( int * )b -* ( int * )a;
11 };
12 stack < char > st;
13 int test( int x, int len, int l, int s)
14 {
15 int i;
16 if (l % 2 == 0 )
17 s -= ( int )pow(a[x],l);
18 else
19 s += ( int )pow(a[x],l);
20 if (l == 5 && s == t)
21 {
22 mark1 = 1 ;
23 return 1 ;
24 }
25 if (l == 5 )
26 return 0 ;
27 if (mark1)
28 return 0 ;
29 for (i = 0 ;i < len;i ++ )
30 {
31 if (mark[i])
32 continue ;
33 mark[i] = 1 ;
34 if (test(i,len,l + 1 ,s))
35 {
36 st.push(a[i] - 1 + ' A ' );
37 return 1 ;
38 }
39 mark[i] = 0 ;
40 }
41 return 0 ;
42 }
43 int main()
44 {
45 char str[ 15 ];
46 int len,i,j;
47 while (scanf( " %d%s " , & t,str) != EOF)
48 {
49 mark1 = 0 ;
50 if (t == 0 && strcmp(str, " END " ) == 0 )
51 break ;
52 len = strlen(str);
53 for (i = 0 ;i < len;i ++ )
54 {
55 a[i] = str[i] - ' A ' + 1 ;
56 }
57 qsort(a,len, sizeof (a[ 0 ]),com);
58 memset(mark, 0 , sizeof (mark));
59 for (i = 0 ;i < len;i ++ )
60 {
61 if (mark1)
62 break ;
63 mark[i] = 1 ;
64 if (test(i,len, 1 , 0 ))
65 {
66 st.push(a[i] - 1 + ' A ' );
67 break ;
68 }
69 mark[i] = 0 ;
70 }
71 if ( ! mark1)
72 printf( " no solution\n " );
73 else
74 {
75 while ( ! st.empty ())
76 {
77 printf( " %c " ,st.top ());
78 st.pop ();
79 }
80 printf( " \n " );
81 }
82 }
83 return 0 ;
84 }
85
86
87

 

你可能感兴趣的:(rack)