题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1015
题目罗嗦了半天,其实意思很简单,就是给定一个目标值target,再给你一个备选字符串(5~12个字符),要你在这个字符串里选5个出来,满足题中给定的等式,并且你选择的这5个字符组成的字符串必须是所有可能情况中按字典序最大的情况。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
简单分析下就可以看出,就是一个组合问题,问题解的最大规模就是12取5,就是12*11*10*9*8*7,而最小规模是5取5,所以应该用枚举法就可以搞定。
#include<iostream>
#include<string>
#include<vector>
usingnamespacestd;
boolisOk(intv,intw,intx,inty,intz,longinttarget)
{//判断是否符合要求
if((v-w*w+x*x*x-y*y*y*y+z*z*z*z*z)==target)
returntrue;
else
returnfalse;
}
intcharToInt(charch)
{
returnch-'A'+1;
}
voidmySort(string&p,intlen)
{//让字符串按字母逆序排列,这样就可以很快找到最大符合要求的情况
inti,j;
chartemp;
for(i=0;i<len-1;i++)
{
for(j=0;j<len-i-1;j++)
{
if(p[j]<p[j+1])
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
intmain(intargc,char*argv[])
{
longinttarget;
inttv,tw,tx,ty,tz;
stringstrTmp;
START:
while(cin>>target>>strTmp&&!(target==0&&strTmp=="END"))
{
intlen=strTmp.length();
mySort(strTmp,len);
intv,w,x,y,z;
//下面开始用枚举法检验可能性,各个字符不能取同一个位置
for(v=0;v<len;++v)
{
for(w=0;w<len;++w)
{
if(v!=w)
{
for(x=0;x<len;++x)
{
if(x!=v&&x!=w)
{
for(y=0;y<len;++y)
{
if(y!=v&&y!=w&&y!=x)
{
for(z=0;z<len;++z)
{
if(z!=v&&z!=w&&z!=x&&z!=y)
{
tv=charToInt(strTmp[v]);
tw=charToInt(strTmp[w]);
tx=charToInt(strTmp[x]);
ty=charToInt(strTmp[y]);
tz=charToInt(strTmp[z]);
if(isOk(tv,tw,tx,ty,tz,target))
{
cout<<strTmp[v]<<strTmp[w]<<strTmp[x]<<strTmp[y]<<strTmp[z]<<endl;
gotoSTART;//遇到最大的符合要求的值,直接跳出,进入下一个
}
}
}
}
}
}
}
}
}
}
cout<<"nosolution"<<endl;
}
return0;
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
HDU的一题........HDU DP动态规
hdu 1574 passed sorce
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
HDU最全ac代码
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
hdu动态规划算法集锦
hdu 1166线段树代码
hdu题目分类
HDU图论题目分类
自己做的HDU ACM已经AC的题目
hdu 1005.比较简单的一道题,有兴趣的可以看看。