인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

rudqla's profile image
rudqla

asked

Introduction to Algorithm Problem Solving for IT Employment (with C/C++): Coding Test Preparation

60. Subsets with Equal Sum (Amazon Interview Problem: DFS Exhaustive Search)

이러한 문제에 도달했을때 즉시 종료할수있다면 하는것이 좋을까요?

Written on

·

218

0

#include <stdio.h>
#include <algorithm>
using namespace std;
int n, i, arr[11], total=0;
bool flag = false;

void DFS(int level, int sum){
	if(sum > total/2){
		return;
	}
	if(level == n+1){
		if(sum == (total-sum)){
			flag = true;
			return;
		}
	} else {
		DFS(level+1, sum+arr[level]);
		DFS(level+1, sum);
	}
}

int main(){
	//freopen("input.txt", "rt", stdin);
	scanf("%d", &n);
	for(i=1; i<=n; i++){
		scanf("%d", &arr[i]);
		total += arr[i];
	}
	if(total%2 == 1){
		printf("NO");
		return 0;
	}
	DFS(1, 0);
	if(flag){
		printf("YES");
	} else {
		printf("NO");	
	}
	
	return 0;
}

선생님 말씀대로 코드를 짰는데요 우선 total%2 ==  1 (홀수)이면 즉시 종료하는것으로 하는건 좋은 답일까요 안좋은 답일까요?

C++코테 준비 같이 해요!

Answer 1

1

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

좋은 아이디어 입니다. 잘 하신 코드입니다.

rudqla's profile image
rudqla

asked

Ask a question