BJUTACM 1095:震惊!这道题的解法竟然是这样的

这道题巧妙的地方在于输入的数一定在100 0000 以内,根据阶乘数据估算,很快就会超过这个数据规模,所以我把所有没有超过数据规模的计算出来,之后进行排列组合 即每次从中选取x个数字进行累加,存入一个数组中,这样在输入的时候直接把输入数据和数组中的数据进行比较即可

比较坑的地方在于非负整数包含0,所以还要加一再存一遍或者在判断时候加入一些-1的判断语句

 题目链接

代码堆垃圾。。抛砖引玉了

#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cmath>
#include <cstdlib>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
//这道题转化为判断一个数能否由1-9的阶乘 加起来
//9 + C92 + C93 +C94 +C95 +C96 +C97 +C98 +C99 
//	1 9 36 84  126 126  84 36 9 1 一共有这么多种情况 
int main(int argc, char** argv) {
	int count = 0;
	int aa[3005] = {0};
	for(int i=1;i<10;i++)
	{
		long long a = 1;
		for(int j=1;j<=i;j++)
		{
			a *= j;
		}
		aa[count++] = a;
//		printf("%d\n",a);
	}
	for(int i=0;i<9;i++)//两个数位加起来的情况 
	{
		for(int j=i+1;j<9;j++)
		{
			aa[count++] = aa[i] + aa[j];
		}
	}
//	printf("count = %d\n",count);
//	for(int i=0;i<count;i++)
//	{
//		printf("%lld\n",aa[i]);
//	}
	for(int i=0;i<9;i++)
		for(int j=i+1;j<9;j++)
			for(int k=j+1;k<9;k++)
				aa[count++] = aa[i] + aa[j] + aa[k]; 
	

	for(int i=0;i<9;i++)
		for(int j=i+1;j<9;j++)
			for(int k=j+1;k<9;k++)
				for(int l=k+1;l<9;l++)
					aa[count++] = aa[i] + aa[j] + aa[k] + aa[l];
	
//	printf("count = %d",count);
//	for(int i=0;i<count;i++)
//	{
//		printf("%lld\n",aa[i]);
//	}
	for(int i=0;i<9;i++)
	for(int j=i+1;j<9;j++)
	for(int k=j+1;k<9;k++)
	for(int l=k+1;l<9;l++)
	for(int m=l+1;m<9;m++)
	aa[count++] = aa[i] + aa[j] + aa[k] + aa[l] + aa[m];
	
	for(int i=0;i<9;i++)
	for(int j=i+1;j<9;j++)
	for(int k=j+1;k<9;k++)
	for(int l=k+1;l<9;l++)
	for(int m=l+1;m<9;m++)
	for(int n=m+1;n<9;n++)
	aa[count++] = aa[i] + aa[j] + aa[k] + aa[l] + aa[m] + aa[n];

	for(int i=0;i<9;i++)
	for(int j=i+1;j<9;j++)
	for(int k=j+1;k<9;k++)
	for(int l=k+1;l<9;l++)
	for(int m=l+1;m<9;m++)
	for(int n=m+1;n<9;n++)
	for(int o=n+1;o<9;o++)
	aa[count++] = aa[i] + aa[j] + aa[k] +aa[l] + aa[m] +aa[n] + aa[o];
	
	for(int i=0;i<9;i++)
	for(int j=i+1;j<9;j++)
	for(int k=j+1;k<9;k++)
	for(int l=k+1;l<9;l++)
	for(int m=l+1;m<9;m++)
	for(int n=m+1;n<9;n++)
	for(int o=n+1;o<9;o++)
	for(int p=o+1;p<9;p++)
	aa[count++] = aa[i] + aa[j] + aa[k] + aa[l] + aa[m]+aa[n]+aa[o]+aa[p];

	aa[count] = 409113;
//	for(int i=0;i<count+1;i++)
//	{
//		printf("%d\n",aa[i]);
//	 } 
//	 system("pause");
	int t = count+1;
	for(int i=0;i<t;i++)
	{
		aa[count++] = aa[i] + 1;
	}
	aa[count++] = 409114; 
	//以上存储部分完成
//	for(int i=0;i<count;i++)
//	{
//		printf("%d\n",aa[i]);
//	} 
//	 system("pause");
	while(1)
	{
		int temp;
		int flag = 0;
		scanf("%d",&temp);
		if(temp==-1)
		{
			return 0;
		}
		else
		{
			if(temp==0)
			{
				printf("NO\n");
				continue;
			}
			for(int i=0;i<count+5;i++)
			{
				if(aa[i]==temp)
				{ 
					flag = 1;
					break;
				} 
			}
			if(flag == 1)
			{
				printf("YES\n");
			}
			if(flag==0)
			{
				printf("NO\n");
			}
		}
	}
	

//	printf("count = %d\n",count);
//	for(int i=0;i<count;i++)
//	{
//		printf("%lld\n",aa[i]);
//	}
		//system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/CuriousLiu/article/details/80270156