幸运数字是一些特殊的整数。在基本数字中,某些特殊数字因其位置而被消除。代替它们的值,就消除它们的位置。未删除的数字,它们是幸运数字。
数字删除遵循某些规则。首先,删除第二个数字,然后删除所有第三个数字,依此类推。
这是一些例子-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 (1 – 25 all) 1 3 5 7 9 11 13 15 17 19 21 23 25 (deleting all 2nd numbers) 1 3 7 9 13 15 19 21 25 (All 3rd numbers are deleted, starting from 5) 1 3 7 9 13 15 21 25 (All 7th numbers are deleted starting from 19)
输入:输入数字以检查是否幸运。设数字为13输出:13为幸运数字。
isLuckyNumber(number)
输入-一个数字。
输出- 检查号码是否幸运。
Begin counter := 2 (It is static data, not be initialized again in recursion call) if counter > n, then return 1 if n mod counter = 0, then return 0 n := n – (n / counter) counter := counter + 1 isLuckyNumber(n) End
#include <iostream> using namespace std; int counter = 2; //used during recursion bool isLuckyNumber(int n) { if(counter > n) return 1; if(n%counter == 0) return 0; n -= n/counter; //n will be next position for recursion counter++; return isLuckyNumber(n); } int main() { int x = 13; if(isLuckyNumber(x)) cout << x<<" is a lucky number."; else cout << x<<" is not a lucky number."; }
输出结果
13 is a lucky number.