【杭电1002】A + B Problem II

本文介绍了一种方法,用于将两个字符数组转换为整型数组,并实现它们的加法运算。通过逐位读取字符并减去字符'0'的ASCII值来完成转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述
这里写图片描述
字符数组转化为整型数组。

#include<stdio.h>
#include<string.h>
int main() {
    int x[1010],y[1010],t=0,n;
    char str1[1010],str2[1010];
    scanf("%d",&n);
    while(n--) {
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        scanf("%s%s",str1,str2);
        int u=strlen(str1);
        int v=strlen(str2);
        for(int l=0; l<u; l++)
            x[l]=str1[u-1-l]-'0';
        for(int l=0; l<v; l++)
            y[l]=str2[v-1-l]-'0';
        int s=u>v?u:v;
        for(int l=0; l<s; l++) {
            x[l]=x[l]+y[l];
            if(x[l]>=10) {
                x[l+1]++;
                x[l]=x[l]%10;
            }
        }
        t++;
        printf("Case %d:\n",t);
        printf("%s + %s = ",str1,str2);
        if(x[s]!=0)
            for(int l=s; l>=0; l--)
                printf("%d",x[l]);
        else
            for(int l=s-1; l>=0; l--)
                printf("%d",x[l]);
        printf("\n");
        if(n)
            printf("\n");
    }
    return 0;
}

http://acm.hdu.edu.cn/showproblem.php?pid=1002

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值