행운의 숫자는 특별한 정수입니다. 기본 번호에서 일부 특수 번호는 위치별로 제거됩니다. 값 대신 위치에 대해 숫자가 제거됩니다. 삭제되지 않은 숫자는 행운의 숫자입니다.
번호 삭제는 몇 가지 규칙을 따릅니다. 처음에는 두 번째 번호마다 삭제되고 그 다음에는 세 번째 번호가 모두 삭제되는 방식으로 진행됩니다.
다음은 몇 가지 예입니다 -
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.