v - w^2 + x^3 - y^4 + z^5 = target 一个计算公式,输入一个整数t,一串大写的字符串。要求次字符串中5个不相同字母按上公式所得的值target == t 则输出这5个字符
负责输出 no solution
暴力就可以过。
// File Name: hdu1015.cpp
// Author: Toy
// Created Time: 2013年03月09日 星期六 16时38分18秒
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cctype>
#include <cmath>
#include <string>
#include <algorithm>
#include <cstdlib>
using namespace std;
int t, len;
char str[15];
double A[15];
bool cmp ( char a, char b ) {
return a > b;
}
void find ( ) {
double n;
int v;
for ( 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 ) {
n = A[v] - pow ( A[w], 2 ) + pow ( A[x], 3 ) - pow ( A[y], 4 ) + pow ( A[z], 5 );
if ( n == t && v != w && v != x && v != y && v != z && w != x && w != y && w != z && x != y && x != z && y != z ) {
cout << str[v] << str[w] << str[x] << str[y] << str[z] << endl;
return;
}
}
if ( v == len ) cout << "no solution" << endl;
}
int main ( ) {
while ( 1 ) {
cin >> t >> str;
if ( t == 0 && str[0] == 'E' && str[1] == 'N' && str[2] == 'D' ) break;
len = strlen ( str );
sort ( str, str + len, cmp );
for ( int i = 0; i < len; ++i )
A[i] = ( double ) ( str[i] - 64 );
find ( );
}
return 0;
}