Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 백트래킹
- 그리디 알고리즘
- 수학
- 유니온 파인드
- VR
- Unreal Engine 5
- 정렬
- 누적 합
- 그래프
- 투 포인터
- ue5
- 스택
- 알고리즘
- 재귀
- 브루트포스
- BFS
- Team Fortress 2
- 다익스트라
- 백준
- DFS
- 트리
- c++
- 유니티
- 문자열
- 다이나믹 프로그래밍
- 우선순위 큐
- XR Interaction Toolkit
- 시뮬레이션
- 자료구조
- 구현
Archives
- Today
- Total
1일1알
백준 1699번 제곱수의 합 C++ 본문
https://www.acmicpc.net/problem/1699
1699번: 제곱수의 합
어떤 자연수 N은 그보다 작거나 같은 제곱수들의 합으로 나타낼 수 있다. 예를 들어 11=32+12+12(3개 항)이다. 이런 표현방법은 여러 가지가 될 수 있는데, 11의 경우 11=22+22+12+12+12(5개 항)도 가능하다
www.acmicpc.net
dp[n]의 최솟값은 dp[n - a^2] + 1 중 최솟값이다. (a^2 <=n)
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> dp(n + 1);
for (int i = 1; i <= n; i++) dp[i] = i;
for (int i = 1; i <= n; i++) {
int a = 1;
while (true) {
int b = pow(a, 2);
if (b == i) {
dp[i] = 1;
break;
}
if (b > i) break;
dp[i] = min(dp[i], dp[i - b] + 1);
a++;
}
}
cout << dp[n];
}
'알고리즘' 카테고리의 다른 글
백준 2056번 작업 C++ (0) | 2022.10.10 |
---|---|
백준 5397번 키로거 C++ (0) | 2022.10.09 |
백준 17822번 원판 돌리기 C++ (0) | 2022.10.04 |
백준 2212번 센서 C++ (1) | 2022.10.03 |
백준 2473번 세 용액 C++ (0) | 2022.10.02 |