[백준 5026] P=NP? (C#, C++) - soo:bak
작성일 :
문제 링크https://soo-bak.github.io/algorithm/boj/sumOrSkipped-8/#문제-링크
설명https://soo-bak.github.io/algorithm/boj/sumOrSkipped-8/#설명
문자열로 주어진 입력에 대하여, 컴퓨터 과학 문제인지, 덧셈 문제인지 판별하여 각각 다른 출력을 하는 조건 분기 문제입니다.
- 먼저 테스트케이스의 개수가 주어지고, 이어서 각 테스트케이스마다 하나의 문자열이 입력됩니다.
- 각 문자열은 두 가지 중 하나입니다:
P=NP
인 경우 →"skipped"
출력a+b
형태인 경우 → 정수 덧셈으로a + b
를 계산하여 출력
접근법https://soo-bak.github.io/algorithm/boj/sumOrSkipped-8/#접근법
- 테스트케이스의 개수를 입력받습니다.
- 각 테스트케이스에 대해 문자열을 입력받고 조건에 따라 처리합니다.
- 문자열이
"P=NP"
인지 비교합니다.- 맞으면
"skipped"
출력 - 아니면
+
기호를 기준으로 나눈 뒤, 각각 정수로 변환하여 합을 출력합니다.
- 맞으면
Codehttps://soo-bak.github.io/algorithm/boj/sumOrSkipped-8/#code
C#https://soo-bak.github.io/algorithm/boj/sumOrSkipped-8/#c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
class Program {
static void Main() {
int t = int.Parse(Console.ReadLine());
while (t-- > 0) {
string s = Console.ReadLine();
if (s == "P=NP") Console.WriteLine("skipped");
else {
var parts = s.Split('+');
Console.WriteLine(int.Parse(parts[0]) + int.Parse(parts[1]));
}
}
}
}
C++https://soo-bak.github.io/algorithm/boj/sumOrSkipped-8/#c-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--) {
string s; cin >> s;
if (s == "P=NP") cout << "skipped\n";
else {
int pos = s.find('+');
cout << stoi(s.substr(0, pos)) + stoi(s.substr(pos + 1)) << "\n";
}
}
return 0;
}