Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么WA呢

Posted by zhangzhiwei at 2009-04-03 22:00:35 on Problem 2562
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator