10824

#include <stdio.h>
#include <string.h>


int main(void){
    // a,b,c,d 입력 받기
    char a[16] = {0},b[8] = {0},c[16] = {0},d[8] = {0};
    scanf("%s %s %s %s", &a, &b, &c, &d);

    // ab cd 합치기
    strcat(a, b);
    strcat(c, d);

    int a_len = strlen(a)-1;
    int c_len = strlen(c)-1;

    int i = 0;
    char result[16] = {0};
    while (1)
    {
        // 두 인덱스의 길이가 음수면 break;
        if((a_len < 0)&&(c_len < 0)){
            result[i] += 48;
            break;
        }

        // a길이가 0또는 양수이면 result[i] + 숫자 
        if(a_len >= 0){
            result[i] += (a[a_len]-48);
        }
        // b길이가 0또는 양수이면 result[i] + 숫자 
        if(c_len >= 0){
            result[i] += (c[c_len]-48);
        }

        // 만약 10 이상이면 그 다음 인덳스에 1을 추가하고 현재 인덱스에 -10을 한다.
        if(result[i] > 9){
            result[i+1] = 1;
            result[i] -= 10;
        }

        // 문자열 숫자로 바꾸기 위해 +48
        result[i] += 48;
        i++;
        a_len--;
        c_len--;
    }

    // 만약 010 이면 앞의 0은 제외하며 출력
    int len = strlen(result)-1;
    if(result[len] == '0'){
        len --;
    }
    for(int j = len; j >= 0; j--){
        
        printf("%c", result[j]);
    }
    
    return 0;
}

+ Recent posts