C#题库

0024-大整数乘法

2017-03-14  本文已影响83人  指尖极光

问题描述

求两个不超过 200 位的非负整数的积。

输入

有两行,每行是一个不超过 200 位的非负整数,没有多余的前导 0。

输出

一行,即相乘后的结果。结果里不能有多余的前导 0,即如果结果是 342,那么就不能输出为 0342。

输入样列

12345678900
98765432100

输出样例

1219326311126352690000

算法实现

using System;

namespace Questions{
    class Program{
        public static void Main(string[] args){
            string m = Console.ReadLine();
            string n = Console.ReadLine();
            int[] k=new int[400];

            for (int i = 0; i < n.Length; i++)
            {
                for (int j = 0; j < m.Length; j++)
                {
                    int temp = k[j + i] + (n[n.Length - i - 1] - '0') * (m[m.Length - j - 1] - '0');
                    if (temp >= 10)
                    {
                        k[j + i + 1]+= temp/10;
                        k[j + i] = temp - 10*(temp / 10);
                    }
                    else
                        k[j + i] = temp;
                }
            }

            int l = 400;
            while (k[l - 1] == 0)
                l--;
            for (int i = l-1; i >= 0; i--)
                Console.Write(k[i]);
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读