zoukankan      html  css  js  c++  java
  • 大数的乘法(C++)

    题目:POJ 2398

    Bull Math
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13410   Accepted: 6903

    Description

    Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros). 

    FJ asks that you do this yourself; don't use a special library function for the multiplication.

    Input

    * Lines 1..2: Each line contains a single decimal number.

    Output

    * Line 1: The exact product of the two input lines

    Sample Input

    11111111111111
    1111111111

    Sample Output

    12345679011110987654321

    Source

    嘴笨,直接上代码。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    using namespace std;
    
    const int maxn = 100;
    void reverse(char a[])
    {
        int len = strlen(a);
        for(int i = 0 ; i < len / 2; i++)
        {
            int temp = a[i];
            a[i] = a[len - i - 1];
            a[len - i -1] = temp;
        }
    }
    int main()
    {
        char a[maxn],b[maxn];
        int t[100] = {0};
        //printf("Please enter 2 numbers: ");
        scanf("%s%s",a,b);
        reverse(a);
        reverse(b);
        if(strcmp(a,"0")==0||strcmp(b,"0")==0)
            cout<<"0"<<endl;
        else
        {
            int i,j;
            for(i = 0; i <strlen(b); i++)
            {
                int cnt = 0;
                for(j = 0; j < strlen(a); j++)
                {
                    int temp = (b[i] - '0') * (a[j] - '0');
                    int tt= t[i+j] + temp + cnt;
                    t[j+i] = tt % 10;
                    cnt = tt / 10;
                }
                while(cnt != 0)
                {
                    t[j+i] = cnt % 10;
                    cnt = cnt / 10;
                    j++;
                }
            }
            for(int k = i + j - 2; k >= 0; k--)
            {
                cout<<t[k];
            }
        }
        return 0;
    }
    

      

    The quieter you become, the more you are able to hear.
  • 相关阅读:
    MVC4 中Remote的使用
    NHibernate遇到的问题集 持续更新。
    2014总结,2015展望
    Redis结合EntityFramework结合使用的操作类
    Entity Framwork db First 中 Model验证解决办法。
    「面经」阿里蚂蚁金服 offer 之路
    最长公共子序列-LCS
    阿里面试题解答-倒排索引
    如何解决ubuntu下Chromium 新建的应用快捷方式图标模糊的问题
    join sleep yield
  • 原文地址:https://www.cnblogs.com/yqbeyond/p/4390998.html
Copyright © 2011-2022 走看看