hdu 1002

杭电1002,本打算用字符串直接相加的,后来一想,换成整形数组来算也未必不可呀,只是一位一位算了也可以的。

#include <stdio.h>
#include <string.h>
void strr(char a[])
{
    int i, j;
    char t;
    for(i = 0, j = strlen(a) -1; i < strlen(a)/2; i++, j--)
    {
        t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
}//杭电上没有倒置字符串的函数,自己写一个就ok了。
int main()
{
    char a[1500], b[1500];
    int i, t = 1, k, n,c[1500],d[1500], e[1500];
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",a);
        scanf("%s",b);
        strr(a);
        strr(b);

        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        memset(e,0,sizeof(e));
        for(i = 0;i< strlen(a);i++) c[i] = a[i] - '0';
        for(i = 0; i< strlen(b); i++) d[i] = b[i]-'0';

        for(i = 0; i < strlen(a) || i < strlen(b); i++) e[i] = c[i] + d[i];

        k= i;
        for(i = 0; i< k; i++)     e[i+1] += e[i] / 10,e[i] = e[i]%10 ;

        if(e[i] > 0) k = i;
        else k = i-1;

        strr(a);
        strr(b);
        printf("Case %d:\n", t++);
        printf("%s + %s = ",a,b);
        for(i = k; i >= 0 ; i--) printf("%d",e[i]);
        printf("\n");
        if(n) printf("\n");
        }
    return 0;
}
原来提交上去一直pe, 额,后来认真看了题“Note there are some spaces int the equation. Output a blank line between two test cases

前一句说的是注意方程之间有空格,后一句说的是两个实例之间有一空行,前一句好说,后一句还包含的意思是最后一个实例后面没有空格。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值