1015

1
2  /*Description: to
3 *
4 * Author: Vincent
5 *
6 * Date:2010/04/
7 * Contact:agilely@126.com
8 * Zju CS Lab
9 */
10 /*Problem Description
11 === Op tech briefing, 2002/11/02 06:42 CST ===
12 "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in World War II. Fortunately old Brumbaugh from research knew Klein's secrets and wrote them down before he died. A Klein safe has two distinguishing features: a combination lock that uses letters instead of numbers, and an engraved quotation on the door. A Klein quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentences, and mentions one or more numbers. Five of the uppercase letters form the combination that opens the safe. By combining the digits from all the numbers in the appropriate way you get a numeric target. (The details of constructing the target number are classified.) To find the combination you must select five letters v, w, x, y, and z that satisfy the following equation, where each letter is replaced by its ordinal position in the alphabet (A=1, B=2, ..., Z=26). The combination is then vwxyz. If there is more than one solution then the combination is the one that is lexicographically greatest, i.e., the one that would appear last in a dictionary."
13
14 v - w^2 + x^3 - y^4 + z^5 = target
15
16 "For example, given target 1 and letter set ABCDEFGHIJKL, one possible solution is FIECB, since 6 - 9^2 + 5^3 - 3^4 + 2^5 = 1. There are actually several solutions in this case, and the combination turns out to be LKEBA. Klein thought it was safe to encode the combination within the engraving, because it could take months of effort to try all the possibilities even if you knew the secret. But of course computers didn't exist then."
17
18 === Op tech directive, computer division, 2002/11/02 12:30 CST ===
19
20 "Develop a program to find Klein combinations in preparation for field deployment. Use standard test methodology as per departmental regulations. Input consists of one or more lines containing a positive integer target less than twelve million, a space, then at least five and at most twelve distinct uppercase letters. The last line will contain a target of zero and the letters END; this signals the end of the input. For each line output the Klein combination, break ties with lexicographic order, or 'no solution' if there is no correct combination. Use the exact format shown below."
21
22
23
24 Sample Input
25 1 ABCDEFGHIJKL
26 11700519 ZAYEXIWOVU
27 3072997 SOUGHT
28 1234567 THEQUICKFROG
29 0 END
30
31
32 Sample Output
33 LKEBA
34 YOXUZ
35 GHOST
36 no solution
37
38 字符编码 找满足v-w^2+x^3-y^4+z^5=target的最大字母序vwxyz
39 */
40 #include <stdio.h>//若有多解尽量按字典顺序输出
41 #include <string.h>
42 void paixu(char a[],int lenth)
43 {
44 char key;
45 int j,i;
46 for(i=1;i<lenth;i++)
47 {
48 key=a[i];
49 j=i-1;
50 while(j>=0&&key>=a[j])
51 {
52 a[j+1]=a[j];
53 j--;
54 }
55 a[j+1]=key;
56 }
57 }
58 int n,m,a,i,k,e[100000],flag;
59 int j,q[100000],v,w,x,y,z;
60 char ch[100000];
61 int main()
62 {
63 while(scanf("%d %s",&n,ch)!=EOF)
64 {//n存target ch存字符串信息
65 if(!n&&!strcmp(ch,"END"))break;
66 a=strlen(ch);
67 k=0;
68 paixu(ch,a);
69 for(j=0;j<a;j++)//输入字母相对序列
70 q[j]=ch[j]-'A'+1;//q存字母序列
71 flag=0;
72 for(v=0;v<a;v++)//五重循环找 保证尽量按字典顺序输出
73 {
74 for(w=0;w<a;w++)
75 {
76 if(w==v)continue;
77 for(x=0;x<a;x++)
78 {
79 if(x==w)continue;
80 for(y=0;y<a;y++)
81 {
82 if(y==x)continue;
83 for(z=0;z<a;z++)
84 {
85 if(z==y)continue;
86 if( q[v] //题目要求
87 -q[w]*q[w]
88 +q[x]*q[x]*q[x]
89 -q[y]*q[y]*q[y]*q[y]
90 +q[z]*q[z]*q[z]*q[z]*q[z]==n)
91 {//找到就跳出 记得跳五次
92 flag=1;break;
93 }
94 }
95 if(flag)break;
96
97 }
98 if(flag)break;
99 }
100 if(flag)break;
101 }
102
103 if(flag)break;
104 }
105 if(flag==0) printf("no solution\n");
106 else
107 {
108 printf("%c",q[v]+'A'-1);
109 printf("%c",q[w]+'A'-1);
110 printf("%c",q[x]+'A'-1);
111 printf("%c",q[y]+'A'-1);
112 printf("%c",q[z]+'A'-1);
113 printf("\n");
114 }
115
116 }
117 }
118

 

posted @ 2010-04-15 22:57  にんじゃ  阅读(156)  评论(0)    收藏  举报