728x90
반응형
https://www.acmicpc.net/problem/10448]
1. Logic
- 세 합이 100을 넘는 경우는 n이 45인경우부터 1000을 넘기 때문에 45까지 for문을 돌며 합을 계산해 놓는다.
- 계산하고 싶은 삼각수를 받은 후 3중 for문을 돌며 찾게되면 값을 반환한다
2. Code
#include<bits/stdc++.h>
using namespace std;
int triangleNum[1001];
bool FindTri(int n) {
for(int i = 1; i <= 45; i++) {
for(int j = 1; j <= 45; j++) {
for(int k = 1; k <= 45; k++) {
if(triangleNum[i] + triangleNum[j] + triangleNum[k] == n) {
return true;
}
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0);
int t;
cin >> t;
for(int i = 1; i <= 45; i++) {
triangleNum[i] = i * (i + 1) / 2;
}
for(int i = 0; i < t; i++) {
int num;
cin >> num;
cout << FindTri(num) << "\n";
}
}
728x90
반응형
'Algorithm > Beakjoon' 카테고리의 다른 글
[백준/Baekjoon] 1065 한수 C++ :: Greedy (0) | 2023.08.08 |
---|---|
[백준/Baekjoon] 2437 저울 C++ :: Greedy (0) | 2023.08.08 |
[백준/Baekjoon] 2309 일곱 난쟁이 C++ :: Brute force (0) | 2023.08.08 |
[백준/Baekjoon] 10026 적록색약 C++ :: BFS (0) | 2023.08.07 |
[백준/Baekjoon] 1753 최단경로 C++ :: Dijkstra (0) | 2023.08.07 |