본문 바로가기

프로그래밍/백준

[C언어/브루트포스] 2309 : 일곱 난쟁이

#include<stdio.h>
#include<stdlib.h>
int compare(const void *a, const void *b)    // 오름차순 비교 함수 구현
{
    int num1 = *(int *)a;    // void 포인터를 int 포인터로 변환한 뒤 역참조하여 값을 가져옴
    int num2 = *(int *)b;    // void 포인터를 int 포인터로 변환한 뒤 역참조하여 값을 가져옴

    if (num1 < num2)    // a가 b보다 작을 때는
        return -1;      // -1 반환
    
    if (num1 > num2)    // a가 b보다 클 때는
        return 1;       // 1 반환
    
    return 0;    // a와 b가 같을 때는 0 반환
}
int main(void){
	int arr[9]={0};
	int i,k,total=0;
	bool exitOutLoop=false;
	
	for(i=0;i<9;i++){
		scanf("%d",&arr[i]);
		total+=arr[i];
	}
	
	qsort(arr,sizeof(arr)/sizeof(int),sizeof(int),compare);

	for(i=0;i<9;i++){
		for(k=0;k<9;k++){
			if(i!=k){
				if(100==total-arr[i]-arr[k]){
					exitOutLoop=true;
					break;
				}
			}
		}
		if(exitOutLoop==true)
			break;
	}
	
	for(int j=0;j<9;j++){
		if(j!=i&&j!=k)
			printf("%d\n",arr[j]);
	}
	
	return 0;
}