문제: 1193번: 분수찾기
basic-algo-lecture/workbook/0x12.md at master · encrypted-def/basic-algo-lecture
basic-algo-lecture/workbook/0x12.md at master · encrypted-def/basic-algo-lecture
바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.
github.com
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
using namespace std;
pair<int, int> table[10000001]; // 0번 인덱스는 안 써여~_~
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int cnt = 1; // 맨 처음 시작 값
int tmpCnt = 1; // 이 값이 cnt 랑 같아지면 이제 cnt 값 갱신하는거임
table[1] = { 1,1 }; // 1/1
for (int i = 2; i < 10000001; i++) {
if (cnt == tmpCnt) {
if (cnt % 2 != 0) {
// 만약 현재 cnt 가 홀수면 분모 갱신
table[i] = { table[i - 1].first, cnt + 1 };
}
else {
// 현재 cnt 가 짝수면 분자 갱신
table[i] = { cnt + 1, table[i - 1].second };
}
cnt++; // 갱신!
tmpCnt = 0;
}
else {
if (cnt % 2 != 0) {
// cnt 값이 홀수면 분자 감소, 분모 증가
table[i] = { table[i - 1].first - 1, table[i - 1].second + 1};
}
else {
// cnt 값이 짝수면 분자 증가, 분모 감소
table[i] = { table[i - 1].first + 1, table[i - 1].second - 1};
}
}
tmpCnt++;
}
int x;
cin >> x;
cout << table[x].first << "/" << table[x].second;
return 0;
}
흠.. 그냥 규칙 찾고 이에 맞게 for문 돌면서 미리 table 에 값 채워놨다. 마지막에 x 값 입력받고 바로 table[x] 출력해주면 끝~