OJ_阶乘的和

题干

OJ_阶乘的和_第1张图片

c语言实现

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
using namespace std;

int main() {
	vector<int> factorialArr;
	//把0!放入数组
	factorialArr.push_back(1);
	int curFactorial = 1;
	for (int i = 1; i < 10; i++) {
		curFactorial = curFactorial * i;
		factorialArr.push_back(curFactorial);
	}

	set<int> allPossible; //所有阶乘和的可能性

	allPossible.insert(0);
	for (int i = 0; i < 10; i++)
	{
		set<int> temp;
		set<int>::iterator it;
		for (it = allPossible.begin(); it != allPossible.end(); ++it) {
			temp.insert(*it + factorialArr[i]);
		}
		for (it = temp.begin(); it != temp.end(); ++it) {
			allPossible.insert(*it);
		}
	}
	allPossible.erase(0);
	int n;
	while (scanf("%d", &n) != EOF) {
		if (n < 0) {
			break;
		}
		if (allPossible.count(n) == 0) {
			printf("NO\n");
		}
		else {
			printf("YES\n"); 
		}
	}
	return 0;
}

你可能感兴趣的:(数据结构与算法,算法,c语言)