반응형
코드
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
int n, i, j, sum, num, cnt, cnt2 = 0;
bool flag;
cin >> n;
i = n / 2;
if (n % 2 != 0) i += 1;
for (; i >= 0; --i)
{
sum = 0;
cnt = 0;
num = i;
flag = false;
while (true)
{
sum += num;
if (sum == n)
{
flag = true;
++cnt2;
break;
}
else if (sum > n)
break;
--num;
++cnt;
}
if (flag)
{
for (j = cnt; j > 0; --j)
cout << i - j << " + ";
cout << i << " = " << n << endl;
}
}
cout << cnt2 << endl;
return 0;
}
출력 결과
채점 결과
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
int a, b = 1, cnt = 0, tmp, i;
cin >> a;
tmp = a;
a--;
while (a > 0)
{
b++;
a -= b;
if (a % b == 0)
{
for (i = 1; i < b; ++i)
cout << a / b + i << " + ";
cout << a / b + i << " = " << tmp << endl;
cnt++;
}
}
cout << cnt << endl;
return 0;
}
선생님 코드 너무 깔끔하다... 😥
해당 포스트는 'it 취업을 위한 알고리즘 문제풀이 (with C/C++) : 코딩테스트 대비' 강의를 수강하며 개인 백업용으로 메모하였습니다.
인프런: https://www.inflearn.com/course/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98#
반응형