FZU Problem 1068 An Interesting Set

Problem 1068 An Interesting Set

Accept: 641 Submit: 1172 Time Limit: 1000 mSec Memory Limit : 32768 KB

img Problem Description

Thomas, a mathematician, is interested in a set S consisting of positive integers. Set S is defined as follows:

(1) If a positive integer satisfies the property that the sum of all digits of it is equal to the product of them, then it belongs to S.

(2) If m belongs to S, then 2m belongs to S. Thomas wants to find out the Kth smallest integer in set S. Design a program to solve the problem.

img Input

The first line in the input is an integer N representing the number of test cases. The following N lines are N test cases and each line contains an integer K (1<=K<=500).

img Output

For each test case, output a line containing an integer that is the Kth smallest integer in set S.

img Sample Input

2

5

16

img Sample Output

5

22

img Source

FJNUPC 2005

解决方案

使用打表的方法

让i从1开始判断,直到得到500个符合条件的数值,存入数组S

判断的条件有两个,一是判断i是否是S中原有数值的2倍,二是判断i的各位数字的总和是否和乘积相等

判断条件一可以利用set迅速查找指定key值的特性

判断条件二利用取余即可

int num[505];
set<int> s;
set<int>::iterator it;
bool isin(int a) {
	int total = 0, muti = 1;
	int num;
	//条件一,需要判断是否偶数
	if (a % 2 == 0) {
		//否则 11 / 2 = 5,即所有数字都会被认为符合条件一
		it = s.find(a / 2);
		if (it != s.end()) {
			return true;
		}
	}
	//条件二
	while (a) {
		num = a % 10;
		if (num == 0) {
			return false;
		}
		total += num;
		muti *= num;
		a /= 10;
	}
	if (total != muti) {
		return false;
	}
	return true;
}
//打表初始化
void init() {
	int i, j;
	for (i = 1, j = 1; j <= 500; i++) {
		if (isin(i)) {
			num[j++] = i;
			s.insert(set<int>::value_type(i));
		}
	}
}
int main() {
	int N, k;
	cin >> N;
	init();
	while (N--) {
		cin >> k;
		cout << num[k] << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值