[백준 10698] Ahmed Aly (C#, C++) - soo:bak
작성일 :
문제 링크
설명
간단한 등식 형태의 문자열이 주어졌을 때, 해당 식이 맞는지를 판별하는 문제입니다.
접근법
- 테스트케이스 개수를 입력받고, 각 수식을 문자열로 입력받습니다.
- 수식을 공백 기준으로 나누어
X
, 연산자,Y
,=
,Z
로 구분합니다. - 연산자에 따라 덧셈 혹은 뺄셈을 수행하고, 결과가
Z
와 같은지 비교합니다. - 문제에서 요구한 출력 형식에 따라 결과를 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
class Program {
static void Main() {
int t = int.Parse(Console.ReadLine());
for (int i = 1; i <= t; i++) {
var parts = Console.ReadLine().Split();
int x = int.Parse(parts[0]);
string op = parts[1];
int y = int.Parse(parts[2]);
int z = int.Parse(parts[4]);
bool correct = op == "+" ? x + y == z : x - y == z;
Console.WriteLine($"Case {i}: {(correct ? "YES" : "NO")}");
}
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
for (int i = 1; i <= t; i++) {
int l, r, a;
string o, tmp;
cin >> l >> o >> r >> tmp >> a;
bool ok = (o == "+" ? l + r == a : l - r == a);
cout << "Case " << i << ": " << (ok ? "YES" : "NO") << "\n";
}
return 0;
}