---
# System prepended metadata

title: 13586 - Patrick's Calculator
tags: [math]

---

# 13586 - Patrick's Calculator
>author: Utin
###### tags: `math`

---

## Brief
See the code below

## Solution 0

```c=
#include<stdio.h>

int main() {
    char a[140], b[140];
    int c[35] = {0}, d[35] = {0}, digit_c = 0, digit_d = 0, ans[36];
    scanf("%s", a);
    scanf("%s", b);
    //轉成int陣列
    int temp_sum = 0;
    for (int i = 0; i < sizeof(a); i++){
        if (a[i] == ',') {
            c[digit_c] = temp_sum;
            digit_c += 1;
            temp_sum = 0;
        }
        else if (a[i] == '\0') {
            c[digit_c] = temp_sum;
            digit_c += 1;
            temp_sum = 0;
            break;
        }
        else {
            temp_sum *= 10;
            temp_sum += a[i] - '0';
        }
    }
    for (int i = 0; i < sizeof(b); i++){
        if (b[i] == ',') {
            d[digit_d] = temp_sum;
            digit_d += 1;
            temp_sum = 0;
        }
        else if (b[i] == '\0') {
            d[digit_d] = temp_sum;
            digit_d += 1;
            temp_sum = 0;
            break;
        }
        else {
            temp_sum *= 10;
            temp_sum += b[i] - '0';
        }
    }
    //計算
    int digit_minus, constant_digit_minus, digit_ans;
    if (digit_c >= digit_d) {
        digit_ans = digit_c;
        digit_minus = digit_c - digit_d;
        constant_digit_minus = digit_c - digit_d;
        for (int i = 0; i < digit_c; i++) {
            if (digit_minus > 0) {
                ans[i] = c[i];
                digit_minus -= 1;
            }
            else {
                ans[i] = c[i] + d[i - constant_digit_minus];
            }
        }
    }
    else {
        digit_ans = digit_d;
        digit_minus = digit_d - digit_c;
        constant_digit_minus = digit_d - digit_c;
        for (int i = 0; i < digit_d; i++) {
            if (digit_minus > 0) {
                ans[i] = d[i];
                digit_minus -= 1;
            }
            else {
                ans[i] = c[i - constant_digit_minus] + d[i];
            }
        }
    }
    //進位
    for (int i = digit_ans - 1; i > 0; i--) {
        if (ans[i] >= 1000) {
            ans[i - 1] += ans[i] / 1000;
            ans[i] %= 1000;
        }
    }
    //輸出
    for (int i = 0; i < digit_ans; i++) {
        if (ans[i] >= 1000 && i == 0) {
            printf("%d,%03d", ans[i] / 1000, ans[i] % 1000);
        }
        else if (ans[i] < 1000 && i == 0) {
            printf("%d", ans[i]);
        }
        else {
            printf(",%03d", ans[i]);
        }
    }
    printf("\n");
}

// By Utin
```

## Reference