哈理工OJ 1159 MAGI System(大数乘法)

原题地址:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1159
下面是AC代码:

#include <stdio.h>
#include <string.h>
int main()
{
    char strNum1[1000],strNum2[1000],strNum3[2000];
    while(~scanf("%s",strNum1))//输入
    {
        int nNum[1000]= {0};
        int i = 0, j = 0, len1 = 0, len2 = 0, len3 = 0, temp = 0,flag = 1;
        scanf("%s",strNum2);//输入
        if(strNum2[0]=='0'||strNum1[0]=='0')
        {
             printf("0\n");
             continue;
        }
        len1 = strlen(strNum1);//长度
        len2 = strlen(strNum2);
        len3 = len1+len2+1;
        temp = len3;
        //相乘-----原理见分析
        for (i = len2-1; i >= 0; --i)//strNum2
        {
            temp = (--len3);
            for (j = len1-1; j >= 0; --j)//strNum1
            {
                nNum[temp--] += (strNum2[i]-'0') * (strNum1[j]-'0');
            }
        }

        len3 = len1+len2+1;

        //进位---原理见分析
        for (i = len3; i > 0; --i)
        {
            while (nNum[i]>9)
            {
                nNum[i-1]+=nNum[i]/10;
                nNum[i] = nNum[i]%10;
            }
        }
        //输出结果
        for (i = 0; i < len3; ++i)
        {
            if (nNum[i] == 0 && flag == 1)//舍去前面没必要的0
            {
                continue;
            }
            else
            {
                flag = 0;
            }
            printf("%d",nNum[i]);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值