[백준 16017] Telemarketer or not? :: C언어
문제
Here at the Concerned Citizens of Commerce (CCC), we have noted that telemarketers like to use seven-digit phone numbers where the last four digits have three properties. Looking just at the last four digits, these properties are:
- the first of these four digits is an 8 or 9;
- the last digit is an 8 or 9;
- the second and third digits are the same.
For example, if the last four digits of the telephone number are 8229, 8338, or 9008, these are telemarketer numbers.
Write a program to decide if a telephone number is a telemarketer number or not, based on the last four digits. If the number is not a telemarketer number, we should answer the phone, and otherwise, we should ignore it.
입력
The input will be 4 lines where each line contains exactly one digit in the range from 0 to 9.
출력
Output either ignore if the number matches the pattern for a telemarketer number; otherwise, output answer.
처음에 if문의 중첩으로 문제를 해결했는데, 실행은 제대로 되지만 틀렸다는 결과가 나왔다.
그래서 논리연산자 &&를 사용해서 문제를 해결했다.
if문 중첩도 맞는데...
#include<stdio.h>
int main() {
int arr[4];
for (int a = 0; a < 4; a++) {
scanf("%d", &arr[a]);
}
if (arr[0] > 7 && arr[1] == arr[2] && arr[3] > 7) printf("ignore\n");
else printf("answer\n");
return 0;
}
틀렸지만 if문 중첩으로 푼 알고리즘 (if문 중첩에도 논리연산자 ||가 사용되었다)
#include<stdio.h>
int main() {
int arr[4];
for (int a = 0; a < 4; a++) {
scanf("%d", &arr[a]);
}
if (arr[0] == 8 || arr[0] == 9) {
if (arr[3] == 8 || arr[3] == 9) {
if (arr[1] == arr[2]) {
printf("ignore\n");
}
}
}
else printf("answer\n");
return 0;
}