fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 34585 (C++) Twin Guardians
최초 업로드: 2025-10-25 18:02:19
최근 수정 시간: 2025-10-25 18:02:19
게시자: rlatjwls3333
카테고리: 백준
조회수: 10
# [Silver IV] Twin Guardians [문제 링크](https://www.acmicpc.net/problem/34585) ## 문제 설명 <p>In a distant Mathematical Kingdom, there stands an ancient gate known as the “Twin Gate.” Legend has it that the gate will only open when two “Twin Guardians” stand before it at the same time.</p> <p>The king has summoned a wise adventurer to help determine whether the two given numbers are “Twin Primes.” If they genuinely are Twin Guardians (i.e., Twin Primes), the gate will shine brightly and open; otherwise, it will remain firmly shut.</p> <p><strong>Definition of Twin Primes</strong>: If two numbers $i$ and $i+2$ are both prime, they are called “twin primes.”</p> ## 입력 <p>Each test contains multiple test cases. The first line contains the number of test cases $t$. The description of the test cases follows.</p> <p>The only line of each test case contains two integers $a$ and $b$, representing a task assigned by the king that asks you to check these two numbers.</p> ## 출력 <p>For each test case, if $a$ and $b$ are twin primes, output <code>Y</code> (indicating “Yes, the Twin Gate opens!”); otherwise, output <code>N</code>.</p> ## 풀이 a와 b가 2차이가 나고, 둘 다 소수인지 확인하는 문제입니다. ``` c++ #include<bits/stdc++.h> using namespace std; bool isPrime(int n) { for(int i=2;i*i<=n;i++) { if(n%i==0) return false; } return n!=1; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { int a, b; cin >> a >> b; if(a+2==b && isPrime(a) && isPrime(b)) cout << "Y\n"; else cout << "N\n"; } } ```