zoukankan      html  css  js  c++  java
  • SDNU 1232.A*B Problem(高精度)

    这道题虽然数据很大,但是可以用数学的乘法运算来解决

    Description

    Calculate a*b

    Input

    Input contains multiple test cases.
    Each test case contains two number a and b (0<=a,b<=10^1000),

    Output

    Output a*b

    Sample Input

    12345678987654321 98765432123456789

    Sample Output

    1219326320073159566072245112635269

    Hint

    练习模拟大数乘法
    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    string a, b;
    int x[1000+8], y[1000+8], sum[1000000+8], t;
    int main()
    {
        while(cin >> a >> b)
        {
            int lena = a.size();
            int lenb = b.size();
            int lens = 0;
            memset(sum, 0, sizeof(sum));
            for(int i = 0; i<lena; i++)
                {
                    x[lena-i] = a[i]-48;//-48等于-'0',并且把这个字符串倒序输入数列X中
                }
            for(int i = 0; i<lenb; i++)
            {
                y[lenb-i] = b[i]-48;
            }
            for(int i = 1; i<=lena; i++)//像数学乘法一样,把所有的数都乘起来
            {
                t = 0;//记录要进的位数
                for(int j = 1; j <= lenb; j++)
                {
                    sum[i+j-1] += x[i]*y[j]+t;
                    t = sum[i+j-1]/10;
                    sum[i+j-1] %= 10;
                }
                sum[i+lenb] = t;
            }
            lens = lena+lenb;
            while(sum[lens] == 0 &&lens>1) lens--;
            for(int i = lens; i >= 1; i--)printf("%d", sum[i]);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    c语言指针讲解第一节初识指针
    linux的的一些入门常识
    sql手注的思路
    mysql主从备份配置
    CentOS 6.5 nginx+tomcat+ssl配置
    mysql 5.7.18安装教程
    minIO分布式集群搭建+nginx负载均衡
    Linux常用命令
    使用python连接mysql数据库——pymysql模块的使用
    with与上下文管理器
  • 原文地址:https://www.cnblogs.com/RootVount/p/10371456.html
Copyright © 2011-2022 走看看