您的位置:首页 > 其它

ZOJ 3196 Give me the result

2010-11-09 18:07 232 查看
给出n个数,和数字k,对于这n个数他们之前可以有不同的运算。。+-*/

注意他的减法是绝对值的运算,还有就是数据类型要用long long,因为运算的数据可能很大,我因此wa了一次

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
long long cases,n,k;
long long max,i,j;
long long a[10];
long long nok(long long x)
{
long long i;
char s[100];
sprintf(s,"%lld",x);
for( i = 0; s[i] != '/0'; i++ )
if( s[i] == k + '0' ) return 0;
return 1;
}
void dfs( long long x, long long sum)
{
if( x > n )
{
if( sum > max && nok(sum)) max = sum;
return ;
}
if( a[x] != 0 ) dfs(x+1,sum/a[x]);
dfs(x+1,sum+a[x]);
dfs(x+1,sum>a[x]?(sum-a[x]):(a[x]-sum));;
dfs(x+1,sum*a[x]);
}
int main(void)
{
scanf("%lld",&cases);
while( cases-- )
{
scanf("%lld%lld",&n,&k);
for( i = 1; i <= n; i++ )
scanf("%lld",&a[i]);
max = -999999999;
dfs(2,a[1]);
if( max == -999999999 ) printf("No result/n");
else printf("%lld/n",max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: