Safecracker HDU - 1015

这里写自定义目录标题


这是一道很简单的水题:
我们将给你一个数字n和一个字符串,请你从字符串中选出五个字母,字母的大小按照字母表的顺序,比如(A=1, B=2, …, Z=26)。假设依次选出的字母为v、w、x、y、z 使其满足以下式子

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

比如:给出一个n = 1 和字符串 ABCDEFGHIJKL,一个可能的结果为FIECB,6 - 9^2 + 5^3 - 3^4 + 2^5 = 1。

请输出满足条件字典序最大的字符串
Input
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END
Output
LKEBA
YOXUZ
GHOST
no solution
代码:在这里插入代#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <ctime> #include <set> #include <map> #include <cmath> using namespace std; const int maxn = 1001; int ta, m, len,vis[maxn],num[maxn]; char s[maxn],ch[maxn],m_ch[maxn]; map<char,int>mm; void fun(){ char c = 'A'; for(int i=1;i<=26;i++) mm[c++] = i; } int is(int v,int w,int x,int y,int z,int tar){ if(v - w*w + x*x*x - y*y*y*y + z*z*z*z*z == tar) return 1; else return 0; } void sear(){ for(int a=0; a<len; a++){ for(int b=0; b<len; b++){ if(a==b) continue; for(int c=0; c<len; c++){ if(c==b)continue; for(int d=0; d<len;d++){ if(c==d)continue; for(int e=0; e<len; e++){ if( is(mm[s[a]],mm[s[b]],mm[s[c]],mm[s[d]],mm[s[e]],ta)){ ch[0] = s[a],ch[1] = s[b]; ch[2] = s[c],ch[3] = s[d]; ch[4] = s[e],ch[5] = '\0'; if(strcmp(ch,m_ch)>0) strcpy(m_ch,ch); } } } } } } } int main(){ fun(); while(~scanf("%d %s",&ta,s)&&(ta||strcmp(s,"END"))){ memset(ch,'\0',sizeof(ch)); memset(m_ch,'\0',sizeof(m_ch)); len = strlen(s); sort(s,s+len); sear(); if(strlen(m_ch)==0) printf("no solution\n"); else printf("%s\n",m_ch); } return 0; }码片

总结:
题如期名,是到水题,可以五重暴力,我的代码写的没有上面的规范,就用上面的了,要注意这里用char类型的数组比较方便,只是我之前有点混乱。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值