#include <stdio.h>
int main(void) {
int teste1 = carries(9, 1);
int teste2 = carries(99, 1);
int teste3 = carries(999, 1);
int teste4 = carries(999, 10);
printf("9+1: %i carries\n", teste1
); printf("99+1: %i carries\n", teste2
); printf("999+1: %i carries\n", teste3
); printf("999+10: %i carries\n", teste4
);
return 0;
}
int carries(int a, int b) {
return (sumDigits(a) + sumDigits(b) - sumDigits(a+b)) / 9;
}
int sumDigits(int num) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbih2b2lkKSB7CglpbnQgdGVzdGUxID0gY2Fycmllcyg5LCAxKTsKCWludCB0ZXN0ZTIgPSBjYXJyaWVzKDk5LCAxKTsKCWludCB0ZXN0ZTMgPSBjYXJyaWVzKDk5OSwgMSk7CglpbnQgdGVzdGU0ID0gY2Fycmllcyg5OTksIDEwKTsKCQoJcHJpbnRmKCI5KzE6ICVpIGNhcnJpZXNcbiIsIHRlc3RlMSk7CglwcmludGYoIjk5KzE6ICVpIGNhcnJpZXNcbiIsIHRlc3RlMik7CglwcmludGYoIjk5OSsxOiAlaSBjYXJyaWVzXG4iLCB0ZXN0ZTMpOwoJcHJpbnRmKCI5OTkrMTA6ICVpIGNhcnJpZXNcbiIsIHRlc3RlNCk7CgkKCXJldHVybiAwOwp9CgppbnQgY2FycmllcyhpbnQgYSwgaW50IGIpIHsKCXJldHVybiAoc3VtRGlnaXRzKGEpICsgc3VtRGlnaXRzKGIpIC0gc3VtRGlnaXRzKGErYikpIC8gOTsKfQoKaW50IHN1bURpZ2l0cyhpbnQgbnVtKSB7CglpbnQgc3VtID0gMDsKICAgIHdoaWxlIChudW0gIT0gMCkgewogICAgICAgIHN1bSArPSBudW0gJSAxMDsKICAgICAgICBudW0gLz0gMTA7CiAgICB9CiAgICByZXR1cm4gc3VtOwp9