poj 1248 Safecracker

简介:

同样的理解题意很重要,我觉得可能这题作者的本意不是这么水,没那么简单可以暴力过的,但是我是暴力A了。。。

网上有用回溯的,字典树的。。。也有5重暴力,反正我是懒得看,暴力我还是有自信自己写的,说不定写的还好一些。。哈哈

我这个解法的亮点应该就是转换以后的sort(),这样算出来的一定是最大字典序的。。。一次A


题意:给定一个长度为5……12个不同字符组成的字符串,从中选取5个,设为v,w,x,y,z,要满足等式:v-w^2+x^3-y^4+z^5=target,现在给出字符串和target,求满足该条件的5个字符(字典序要最大)


AC的代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

char str[15];	//存输入的字符串
int strNum[15];	//上面字符串对应的数字


void Trans()
{
	int i;
	for(i=0;i<strlen(str);i++)
		strNum[i]=str[i]-'A'+1;
}


void forceSearch(int len,int target)
{
	int i,j,k,l,m;	//5重暴力counter

	//暴力主体  i是最高位的变量
	for(i=len-1;i>=0;i--)
		for(j=len-1;j>=0;j--)
		{
			if(j==i)
				continue;
			
			for(k=len-1;k>=0;k--)
			{
				if(k==i || k==j)
					continue;
				
				for(l=len-1;l>=0;l--)
				{
					if(l==i || l==j || l==k)
						continue;
					
					for(m=len-1;m>=0;m--)
					{
						if(m==i || m==j || m==k || m==l)
							continue;

						else if(strNum[i] - strNum[j]*strNum[j] + strNum[k]*strNum[k]*strNum[k] - strNum[l]*strNum[l]*strNum[l]*strNum[l] + strNum[m]*strNum[m]*strNum[m]*strNum[m]*strNum[m] == target)
						{
							printf("%c%c%c%c%c\n",strNum[i]-1+'A',strNum[j]-1+'A',strNum[k]-1+'A',strNum[l]-1+'A',strNum[m]-1+'A');
							return;
						}
					}
				}
			}
		}

		printf("no solution\n");
}


int main()
{
	int i;
	int target;
	while(scanf("%d%s",&target,str))
	{
		if(target==0 && strcmp(str,"END")==0)
			return 0;
		
		int len=strlen(str);		//存每次字符串的长度
		Trans();
		sort(strNum,strNum+len);
		
		//test      ok
		/*for(i=0;i<len;i++)
			printf("%d,",strNum[i]);
		printf("\n");*/
		
		forceSearch(len,target);
	}
	
	return 0;
}


相关文章
|
测试技术
POJ 1001
此题用最朴素的思路实现即可,需模拟加法器,乘法器,最烦人的地方是特殊情形,如末位是小数点(12.^2=144,取小数点),整数末位是0(100^2=10000),0次幂,测试用例可能超出题目中说的范围,可能包含0次幂(100.0^0=0, 0.10^1=0.1)。
729 0
|
人工智能 BI
|
测试技术
poj-1218 THE DRUNK JAILER 喝醉的狱卒
自己去看看原题; 题目大意: 就是一个狱卒喝醉了,他第一趟吧所有的监狱都带开,第二趟把能把二整除的监狱关闭,第三趟操作能把三整除的监狱; 求最后能逃跑的罪犯数 输入第一个数是代表 测试数据组数 每个数据代表狱卒来回的次数 当作开关问题即可 #include using names...
960 0
|
人工智能 BI
poj-3185-开关问题
描述   牛一行20他们喝的水碗。碗可以那么(面向正确的为清凉水)或颠倒的(一个位置而没有水)。他们希望所有20个水碗那么,因此用宽鼻子翻碗。   嘴太宽,他们不仅翻转一碗还碗的碗两侧(总共三个或三个——在两端的情况下碗——两碗)。
784 0
poj-2551-ones
Description Given any integer 0
749 0
POJ-1003-hangover
Description How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length.
737 0
|
存储 索引
|
算法 数据建模 机器学习/深度学习
poj1273Drainage Ditches
1 #include 2 /* 3 题意:就是寻找从源点到汇点的最大流! 4 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来 5 就是这两个点之间的流量了! 6 ...
827 0
|
算法 计算机视觉
最小割-poj-2914
poj-2914-Minimum Cut Description Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must b
1532 0