본문 바로가기
PROGRAMMING CODE/C

[BAEKJOON] No.2798 C언어

by daye_ 2021. 10. 19.

블랙잭

 

 

//틀린코드

#include <stdio.h>
#include <stdlib.h>

int nqCheck(int n){
    if(n<0) n*=-1;
    return n;
}

int main(){

    int n,m,max=0,num=0;
    scanf("%d %d",&n,&m);

    int *arr;
    arr = (int*)malloc(sizeof(int)*n);

    for(int i=0; i<n; i++){
        scanf("%d",&arr[i]);
    }

    for(int j=0; j<n; j++){
        for(int k=j+1; k<n; k++){
            for(int l=k+1; l<n; l++){
                num = arr[j]+arr[k]+arr[l];
                if(nqCheck(m-num)<nqCheck(m-max))max=num;
            }
        }
    }

    printf("%d\n",max);
    return 0;
}

 

//성공

#include <stdio.h>
#include <stdlib.h>

int main(){
    int n,m,max=0,num=0;
    scanf("%d %d",&n,&m);
    int *arr;
    arr = (int*)malloc(sizeof(int)*n);
    for(int i=0; i<n; i++){
        scanf("%d",&arr[i]);
    }
    for(int j=0; j<n; j++){
        for(int k=j+1; k<n; k++){
            for(int l=k+1; l<n; l++){
                num = arr[j]+arr[k]+arr[l];
                if(num<=m&&num>max)max=num;
            }
        }
    }
    printf("%d\n",max);
    return 0;
}

'PROGRAMMING CODE > C' 카테고리의 다른 글

[BAEKJOON] No.2750 C언어  (0) 2021.10.19
[BAEKJOON] No.2231 C언어  (0) 2021.10.19
[BAEKJOON] No.10870 C언어  (0) 2021.10.19
[BAEKJOON] No.10872 C언어  (0) 2021.10.19
[BAEKJOON] No.2581 C언어  (0) 2021.10.19