| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
为什么WA呢#include <stdio.h>
#include <string.h>
#define SIZE 100
int cal_carry(int len_a, int len_b, char a[], char b[]);
int main()
{
char in[SIZE];
char a[SIZE], b[SIZE];
char * tokseps = " ";
char * p;
int carry;
int len_a, len_b;
freopen("D:\\in.txt", "r", stdin);
while(gets(in) && strcmp(in, "0 0") == 1)
{
p = strtok(in, tokseps);
strcpy(a, p);
p = strtok(NULL, tokseps);
strcpy(b, p);
len_a = strlen(a);
len_b = strlen(b);
carry = 0;
if(len_a >= len_b)
carry = cal_carry(len_a, len_b, a, b);
else
carry = cal_carry(len_b, len_a, b, a);
if(carry == 1)
printf("%d carry operation.\n", carry);
else if(carry > 1)
printf("%d carry operations.\n", carry);
else
printf("No carry operation.\n");
}
return 0;
}
int cal_carry(int len_a, int len_b, char a[], char b[])
{
int i, j;
int carry;
int tmp;
for(i = len_a - 1, j = len_b - 1, tmp = 0; j >= 0; i--, j--)
{
tmp = ((a[i] - '0') + (b[j] - '0') + tmp) / 10;
carry += tmp;
}
for(; i >= 0; i--)
{
tmp = ((a[i] - '0') + tmp) / 10;
carry += tmp;
}
return carry;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator