航电1015 五重循环暴力 水题

http://acm.hdu.edu.cn/showproblem.php?pid=1015

题目罗嗦了半天,其实意思很简单,就是给定一个目标值target,再给你一个备选字符串(5~12个字符),要你在这个字符串里选5个出来,满足题中给定的等式,并且你选择的这5个字符组成的字符串必须是所有可能情况中按字典序最大的情况。

      简单分析下就可以看出,就是一个组合问题,问题解的最大规模就是12取5,就是12*11*10*9*8*7,而最小规模是5取5,所以应该用枚举法就可以搞定。

不过,枚举之前先排个序,就可以保证输出的是符合要求的最大的那个了


#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;
}


原文地址:https://www.cnblogs.com/SSYYGAM/p/4509647.html