숫자가 주어지면 입력된 숫자가 행운의 숫자인지 확인하고 그 결과를 출력하는 작업입니다.
행운이란 무엇입니까
행운의 숫자는 모든 자리가 다른 숫자로, 하나 이상의 숫자가 반복되는 경우에는 행운의 숫자로 간주되지 않습니다.
예시
Input-: n = 1234 Output-: it is a lucky number Explanation-: As there is no repeating digit in a number n so it is a lucky number Input-: n = 3434 Output-: it is not a lucky number Explanation-: In the given number n, 3 and 4 are repeating twice so it is not a lucky number
주어진 프로그램에서 사용하는 접근 방식은 다음과 같습니다. -
- 사용자의 숫자 n을 입력하여 행운의 숫자인지 아닌지 확인
- 숫자 크기까지 전체 자릿수 탐색
- 방문할 때마다 방문한 숫자를 표시하고 이미 찾았는지 여부를 확인합니다.
- 주어진 숫자가 행운의 숫자인지 아닌지 표시
알고리즘
Start
Step1-> declare function to check whether a given number is lucky or not
bool check_lucky(int size)
declare bool arr[10]
Loop For int i=0 and i<10 and i++
Set arr[i] = false
End
Loop While(size > 0)
declare int digit = size % 10
IF (arr[digit])
return false
End
set arr[digit] = true
Set size = size/10
End
return true
Step 2-> In main()
Declare int arr[] = {0,34,2345,1249,1232}
calculate int size = sizeof(arr)/sizeof(arr[0])
Loop For int i=0 and i<size and i++
check_lucky(arr[i])?
print is Lucky : print is not Lucky
End
Stop 예시
#include<iostream>
using namespace std;
//return true if a number if lucky.
bool check_lucky(int size) {
bool arr[10];
for (int i=0; i<10; i++)
arr[i] = false;
while (size > 0) {
int digit = size % 10;
if (arr[digit])
return false;
arr[digit] = true;
size = size/10;
}
return true;
}
int main() {
int arr[] = {0,34,2345,1249,1232};
int size = sizeof(arr)/sizeof(arr[0]);
for (int i=0; i<size; i++)
check_lucky(arr[i])? cout << arr[i] << " is Lucky \n": cout << arr[i] << " is not Lucky \n";
return 0;
} 출력
19 is Lucky 34 is Lucky 2345 is Lucky 1249 is Lucky 1232 is not Lucky