hdu 1015 Safecracker

本文介绍了一个简单的算法竞赛题目,通过暴力枚举的方法来找出符合特定数学公式的五个不同字符。使用C++实现,并详细展示了如何遍历所有可能的组合以找到满足条件的字符集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值