B - Safecracker--排序--暴力求解

来源:互联网 发布:笔记本电脑录视频软件 编辑:程序博客网 时间:2024/06/05 09:04

B - Safecracker
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u


Description


=== Op tech briefing, 2002/11/02 06:42 CST ===
"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."

v - w^2 + x^3 - y^4 + z^5 = target

"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."

=== Op tech directive, computer division, 2002/11/02 12:30 CST ===

"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."



Input

1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END


Output

LKEBA
YOXUZ
GHOST
no solution



= = = Op科技简报,2002/11/02 06:42中科= = =

”项目是克莱恩锁在一个安全的背后在图书馆二楼的一幅画。克莱恩保险箱是极其罕见的,他们中的大多数,克莱恩和他的工厂,第二次世界大战中被毁。幸运的是旧Brumbaugh研究知道克莱因的秘密和写下来之前他就死了。克莱恩安全有两个特点:一个密码锁,使用字母而不是数字,和一个门上刻的报价。克莱因报价总是包含五到十二个不同的大写字母,通常在句子的开头,提到了一个或多个数字。五个大写字母的组合形式,打开安全。通过结合所有数字的位数以适当的方式得到一个数字目标。(构建目标数的细节进行分类)。找到结合您必须选择五个字母v,w,x,y,和z满足下列等式,每个字母是取代了字母表的顺序位置(A = 1,B = 2,…,Z = 26)。然后vwxyz相结合。如果有多个解决方案则是是按最大的组合,即。,将出现在一个词典。”

v - w x ^ 2 + y ^ ^ 3 - 4 + z ^ 5 =目标

“例如,给定目标1和字母组ABCDEFGHIJKL,一个可能的解决方案是FIECB,自6 - 9 ^ 2 + 5 ^ 3 - 3 ^ 4 + 2 ^ 5 = 1。有几个解决方案在这种情况下,原来是LKEBA和组合。克莱恩认为这是安全的编码组合在雕刻,因为它可能需要数月之久的努力尝试所有的可能性,即使你知道这个秘密。当然,电脑不存在。”

= = = Op技术指令、电脑部门,2002/11/02 12:30中科= = =

“开发一个程序来找到克莱恩组合在准备部署。使用标准的测试方法按部门规定。输入由一个或多个行包含一个正整数目标不到一千二百万,一个空间,然后至少五最多12个不同的大写字母。最后一行将包含一个零的目标和字母结束;这结束的信号输入。对于每一行输出克莱因组合,打破与词典顺序的关系,或“无解”如果没有正确的组合。使用如下所示的确切格式。”

qsort,包含在stdlib.h头文件里,函数一共四个参数,没返回值.一个典型的qsort的写法如下qsort(s,n,sizeof(s[0]),cmp);其中第一个参数是参与排序的数组名(或者也可以理解成开始排序的地址,因为可以写&s[i]这样的表达式,这个问题下面有说明); 第二个参数是参与排序的元素个数; 第三个三数是单个元素的大小,推荐使用sizeof(s[0])这样的表达式,下面也有说明 :) ;第四个参数就是很多人觉得非常困惑的比较函数啦,关于这个函数,还要说的比较麻烦...我们来讨论cmp这个比较函数,典型的cmp的定义是int cmp(const void *a,const void *b);返回值必须是int,两个参数的类型必须都是const void *,假设是对int排序的话,如果是升序,那么就是如果a比b大返回一个正值,小则负值,相等返回0#include <stdio.h>#include <string.h>#include <stdlib.h>int s[10000],n,i;int cmp(const void *a, const void *b){     return(*(int *)a-*(int *)b);}int main(){     scanf("%d",&n);     for(i=0;i<n;i++) scanf("%d",&s[i]);         qsort(s,n,sizeof(s[0]),cmp);         for(i=0;i<n;i++) printf("%d ",s[i]);         return(0);}本题用的暴力解题。。。#include <stdio.h>#include <string.h>#include <stdlib.h>int n,c[15];char a[15];char b[30]="1ABCDEFGHIJKLMNOPQRSTUVWXYZ";int cmp(const void *a,const void *b){    return *(int*)b - *(int*)a; //降序排列!!!,a,b互换则为升序}void find(){    int i,j,k,p,q,l;    l=strlen(a);    for(i=0; i<l; i++)        c[i]=a[i]-'A'+1;//将字符转化为数字    qsort(c,l,4,cmp);    for(i=0; i<l; i++)    {        for(j=0; j<l; j++)        {            if(j==i) continue;//如果i,j相等,跳过下面的循环进入j=i+1            for(k=0; k<l; k++)            {                if(k==i||k==j) continue;//排除k与i或者j相等                for(p=0; p<l; p++)                {                    if(p==i||p==j||p==k) continue;                    for(q=0; q<l; q++)                    {                        if(q==i||q==j||q==k||q==p) continue;//多次循环保证这五个字母不重复!!!                        if(c[i]-c[j]*c[j]+c[k]*c[k]*c[k]-c[p]*c[p]*c[p]*c[p]+c[q]*c[q]*c[q]*c[q]*c[q]==n)                        {                            printf("%c%c%c%c%c\n",b[c[i]],b[c[j]],b[c[k]],b[c[p]],b[c[q]]);//因为是降序,所以当找到较大的那个组合就结束                            return ;                        }                    }                }            }        }    }    puts("no solution");}int main(){    while(~scanf("%d%s",&n,a))    {        if(!n &&!strcmp(a,"END"))break;        find();    }    return 0;}



0 0