杭电OJ 1015(C++)

最简单的暴力破解,枚举所有情况,选择出最佳。

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

bool flag;
string Klein(int target, string &s);

int main()
{
	int target;
	string s;
	while (cin >> target >> s)
	{
		if (target == 0 && s == "END")
			break;

		flag = false;
		string K = Klein(target, s);
		if (flag)
			cout << K << endl;
		else
			cout << "no solution" << endl;
	}
	return 0;
}

string Klein(int target, string &s)
{
	int v, w, x, y, z;
	int toNum[30]; 
	string temp, max = "AAAAA";
	int len = s.length();
	for (int i = 0; i < len; i++)
	{
		toNum[i] = int(s[i] - 64); //将字母转换为对应字母表中的顺序
	}
	for (int v = 0; v < len; v++)
		for (int w = 0; w < len; w++)
			for (int x = 0; x < len; x++)
				for (int y = 0; y < len; y++)
					for (int z = 0; z < len; z++)
						if (toNum[v] - pow(toNum[w], 2) + pow(toNum[x], 3) - pow(toNum[y], 4) + pow(toNum[z], 5) == target)
						{
							if (v != w && v != x && v != y && v != z && w != x && w != y && w != z && x != y && x != z && y != z)
							{
								temp.push_back(s[v]); //在字符串temp之后添加字符s[v]
								temp.push_back(s[w]);
								temp.push_back(s[x]);
								temp.push_back(s[y]);
								temp.push_back(s[z]);
							}
							if (temp > max)
							{
								max = temp;
								flag = true;
							}
							temp.clear(); //清空字符串temp
						}
	return max;
}

继续加油。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值