반응형
https://www.acmicpc.net/problem/7490
7490번: 0 만들기
각 테스트 케이스에 대해 ASCII 순서에 따라 결과가 0이 되는 모든 수식을 출력한다. 각 테스트 케이스의 결과는 한 줄을 띄워 구분한다.
www.acmicpc.net
전수조사 문제였습니다.
📕 풀이방법
📔 입력 및 초기화
test case 수 t, 제한 n을 입력받습니다.
📔 풀이과정
두 가지 일에 대해 구현하면 됩니다.1. n까지 수를 오름차순으로 뽑기2. 뽑은 수의 값 계산하기
📔 정답출력
뽑은 수의 식 계산 결과가 0이면 수식을 출력합니다.
📕 Code
#include <bits/stdc++.h>
using namespace std;
int t, n;
int getEval(string s){
string tmpS, tmpNum;
for(auto c : s)
if(c != ' ') tmpS += c;
vector <int> num;
vector <char> op;
for(auto t : tmpS){
if(t == '-' || t == '+') {
op.push_back(t);
num.push_back(stoi(tmpNum));
tmpNum = "";
continue;
}
tmpNum += t;
}
num.push_back(stoi(tmpNum));
int ans = num[0];
for(int i = 1; i < num.size(); i++){
if(op[i-1] == '+') ans += num[i];
else ans -= num[i];
}
return ans;
}
void dfs(int depth, string s){
if (depth == n){
if(!getEval(s)) cout << s << '\n';
return;
}
dfs(depth+1, s + ' ' + to_string(depth + 1));
dfs(depth+1, s + '+' + to_string(depth + 1));
dfs(depth+1, s + '-' + to_string(depth + 1));
}
int main(){
cin >> t;
while(t--){
cin >> n;
dfs(1, "1");
cout << '\n';
}
}
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > Brute Force' 카테고리의 다른 글
(Rust) - 백준(BOJ) 23348 : 스트릿 코딩 파이터 (0) | 2022.08.27 |
---|---|
(Rust) - 백준(BOJ) 25494 : 단순한 문제 (Small) (0) | 2022.08.24 |
(C++) - 백준(BOJ) 9724 번 : Perfect Cube (0) | 2022.07.24 |
(C++) - 백준(BOJ) 10395 번 : Automated Checking Machine (0) | 2022.07.23 |
(C++) - 백준(BOJ) 21665 : Миша и негатив (0) | 2022.07.10 |