Chinaunix首页 | 论坛 | 博客
  • 博客访问: 434863
  • 博文数量: 103
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 971
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-11 17:22
文章分类
文章存档

2008年(77)

2007年(26)

我的朋友

分类: C/C++

2008-05-07 20:10:57

#include <stdio.h>
#include <string.h>

int a[101], b[101], c[202];
int mul(int l1, int l2, int a1[101], int a2[101], int c[202])
{
        int i, j;
        int y;//进位

        int x;//乘积

        int z;//余数

        int w;//乘积结果保存位置


        memset((void *)&c[0], 0, sizeof(int));
        for(i = 0; i < 101; i++){
                for(j = 0; j < 101; j++){
                        x = a1[i] * a2[j];
                        z = x % 10;
                        y = x/10;
                        w = i + j;
                        c[w] += z;
                        c[w+1] += y + c[w]/10;
                        c[w] %= 10;
                }
        }

        w = l1 + l2;
        if(c[w-1] == 0){
                w--;
        }

        return w;
}

int main(int argc, char **argv)
{
        char str[101];
        int i, pos, len1, len2;

        if(fgets(str, 101, stdin) != NULL){
                len1 = strlen(str);
                for(i = 0; i < len1-1; i++){
                        a[i] = str[i] - 48;
                }
        }

        if(fgets(str, 101, stdin) != NULL){
                len2 = strlen(str);
                for(i = 0; i < len2-1; i++){
                        b[i] = str[i] - 48;
                }
        }

        pos = mul(len1-2, len2-2, a, b, c);
        for(i = 0; i <= pos; i++){
                printf("%d", c[i]);
        }
        printf("\n");

        return 0;
}

阅读(1790) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~