杭电 1015题

#include <iostream>
#include <algorithm>
#include <string.h>  //引入strcmp和strlen的头文件
using namespace std;
long int target;
char alpstr[13];
int length;
//将字符从大到小进行排序(本题的关键)
/*int cmp(const void *a,const void *b) //qsort的cmp的写法
{
    return *((char*)b)-*((char*)a);
} */
bool cmp(const char &a,const char &b) //sort的cmp的写法,返回值是bool类型(由于此,wa了n次)
{
 return (b-a)>=0?false:true;

int main()
{
    //freopen("1.txt","r",stdin);
    int i,j,k,p,q;
    int v,w,x,y,z;
    while(cin>>target>>alpstr)
    {
        if(target==0 && !strcmp(alpstr,"END")) //strcmp比较字符串数组,如果相等返回0
        {
            break;
        }
        length = strlen(alpstr);    //此处使用strlen,不是sizeof
        //qsort(alpstr,length,sizeof(char),cmp);
  sort(alpstr,alpstr+length,cmp);
        bool isOk = false;
        for(i=0;i<length;++i)
        {
            v = alpstr[i]-64;
            for(j=0;j<length;++j)
            {
                if(j==i)    continue;
                w =alpstr[j]-64; //将字符映射到字母表中的相应位置,A的ASCII码65
                for(k=0;k<length;++k)
                {
                    if(k==j || k==i)    continue;
                    x = alpstr[k]-64;
                    for(p=0;p<length;++p)
                    {
                        if(p==k || p==i || p==j) continue;
                        y = alpstr[p]-64;
                        for(q=0;q<length;++q)
                        {
                            if(q==k ||q==p ||q==j ||q==i)    continue;
                            z = alpstr[q]-64;
                            if(v-w*w+x*x*x-y*y*y*y+z*z*z*z*z==target)
                            {
                                isOk = true;
                                break;
                            }
                        }
                        if(isOk)
                            break;
                    }
                    if(isOk)
                        break;
                }
                if(isOk)
                    break;
            }
            if(isOk)
                break;
        }
        if(isOk)
            cout<<alpstr[i]<<alpstr[j]<<alpstr[k]<<alpstr[p]<<alpstr[q]<<endl;
        else
            cout<<"no solution"<<endl;
    }
    return 0;
}

posted @ 2010-05-03 11:38  北海小龙  阅读(352)  评论(0编辑  收藏  举报