본문 바로가기

C언어/프로그래머스

[c언어] 두 수의 연산값 비교하기

1. 정수 a,b 문자열 반환(sprintf)

2. a+b 정의 (strcpy, strcat 함수)

3. a+b를 정수로 변환한 값과 2*a*b 대소 비교 

 

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

int solution(int a, int b) {
    int answer = 0;
    
    char stra[10], strb[10];
    sprintf(stra, "%d", a);
    sprintf(strb, "%d", b);
    
    char* sa = (char*)malloc(sizeof(char)*10);
    char* sb = (char*)malloc(sizeof(char)*10);
    
    strcpy(sa,&stra);
    strcat(sa,strb);
    
    int ia = atoi(sa);
    
    if((a<1) || (b>10000))
        return -1;
    if(ia> 2*a*b)
        return ia;
    else
        return 2*a*b;
        
    return answer;
}