Last active
August 29, 2015 14:17
-
-
Save henrybear327/3c52c1d4f76b3dfd9fae to your computer and use it in GitHub Desktop.
uva 10035 wrong.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/* | |
carry = add1%10 + add2%10 + carry; | |
carry /= 10; | |
add1 /= 10; | |
add2 /= 10; | |
if( carry ) | |
count++; | |
*/ | |
int main() | |
{ | |
char inp[30]; | |
while(fgets(inp, 30, stdin)) { | |
char *number1, *number2; | |
number1 = strtok(inp, " "); | |
number2 = strtok(NULL, "\n"); | |
if(atoi(number1) == 0 || atoi(number2) == 0) | |
break; | |
int min_length = (strlen(number1) > strlen(number2)) ? \ | |
strlen(number2) : strlen(number1); | |
int i, count = 0, carry = 0; | |
for(i = 0; i < min_length; i++) { | |
int sum = number1[strlen(number1) - 1 - i] - '0' + number2[strlen(number2) - 1 - i] - '0' + carry; | |
if(sum > 9) { | |
count++; | |
carry = 1; | |
} else | |
carry = 0; | |
} | |
//wrong 999 99999999 | |
if((int)strlen(number1) == (int)min_length) { | |
if(number2[strlen(number2) - 1 - i] == '9'&& carry == 1) { | |
count += 2; | |
} | |
} else if((int)strlen(number2) == (int)min_length) { | |
if(number1[strlen(number1) - 1 - i] == '9' && carry == 1) { | |
count += 2; | |
} | |
} | |
if(count == 0) | |
printf("No carry operation.\n"); | |
else if(count == 1) | |
printf("1 carry operation.\n"); | |
else | |
printf("%d carry operations.\n", count); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment