profile image

L o a d i n g . . .

반응형

코드

#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#

 

it 취업을 위한 알고리즘 문제풀이 (with C/C++) : 코딩테스트 대비 - 인프런

알고리즘과 자료구조를 이용해 문제해결력을 기르는게 주 목적입니다. 초급 취업 ・ 이직 프로그래밍 언어 알고리즘 C++ 알고리즘 코딩 테스트 개발자취업 온라인 강의 알고리즘

www.inflearn.com

반응형
복사했습니다!