zoukankan      html  css  js  c++  java
  • 大数乘法

    #include <iostream>
    #include <cstring>
    using namespace std;
    #define MAX 1000000
    struct Node{
        int data;
        Node* next;
    };
    
    void output(Node* head){
        if(!head->next && !head->data) return;
        output(head->next);
        cout << head->data;
    }
    
    void Mul(char *a, char *b){
        char *ap = a, *bp = b;
        Node *head = new Node;
        head->data = 0;
        head->next = NULL;
        Node *p, *q=head, *p1;
        int temp=0, temp1, bit;
        while(*bp){
            p = q->next;
            p1 = q;
            bit = *bp-48;
            while(*ap || temp){
                if(!p){
                    p = new Node;
                    p->data = 0;
                    p->next = NULL;
                    p1->next = p;
                }
                if(*ap == 0)
                    temp1 = temp;
                else{
                    temp1 = p1->data + (*ap-48)*bit + temp;
                    ap++;
                }
                p1->data = temp1 % 10;
                temp = temp1 / 10;
                p1 = p;
                p = p->next;
            }
            ap = a;
            bp++;
            q = q->next;
        }
        p = head;
        output(p);
        cout << endl;
        while(head){
            p = head->next;
            delete head;
            head = p;
        }
    }
    int main(){
        char a[MAX], b[MAX];
        cin.getline(a, MAX, '
    ');
        cin.getline(b, MAX, '
    ');
        Mul(strrev(a), strrev(b));
        return 0;
    }
  • 相关阅读:
    C#面向对象--类
    Unity DOTS--面向数据编程前的准备
    C#面向对象--命名空间
    C#面向对象--多态
    C#面向对象--继承
    C#中堆和栈的区别
    sql执行顺序
    cookie session
    栈和队列
    c# 单例模式
  • 原文地址:https://www.cnblogs.com/yingl/p/5839267.html
Copyright © 2011-2022 走看看