zoukankan      html  css  js  c++  java
  • 长整数加法运算

    问题描述 :

    假设2个任意长度的整数x、y分别由双向链表A和B存储,现要求设计一个算法,实现x+y。计算结果存储在链表C中。

    说明:

    由于A和B输出时需要从头至尾遍历,而做加法时需要从尾至头遍历,因此使用双向链表存储。

    可以从长整数的低位开始拆分(4位为一组,即不超过9999的非负整数),依次存放在链表的每个结点的数据域中;头结点的数据域存放正负数标志(正数或0:1,负数:-1)。

    输入说明 :

    第一行:长整数x

    第二行:长整数y 

    输出说明 :

    第一行:格式化后的长整数x(从低位到高位每4位用","分开)

    第二行:格式化后的长整数y(从低位到高位每4位用","分开)

    第三行:空行

    第四行:单链表C的遍历结果

    第五行:格式化后的计算结果(从低位到高位每4位用","分开)

    输入范例 :

    -53456467576846547658679870988098
    435643754856985679

    输出范例 :

    -5345,6467,5768,4654,7658,6798,7098,8098
    43,5643,7548,5698,5679

    5345->6467->5768->4611->2014->9250->1400->2419
    -5345,6467,5768,4611,2014,9250,1400,2419

      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 #include <iostream>
      6 #include <algorithm>
      7 #include<iomanip>
      8 #include <vector>
      9 #define MY_MAX_LEN 100000
     10 using namespace std;
     11 typedef struct node
     12 {
     13     int  val;//做头结点时 -1为负数 0为正数
     14     struct node* next;
     15     struct node* pre;
     16     node() {}
     17     node(int v) :val(v), next(0), pre(0) {}
     18 }Node;
     19 void display(Node* head)
     20 {
     21     Node* p = head->next;
     22     while (p && p->val == 0)p = p->next;
     23     if (!p) { cout << 0 << endl; return; }
     24     if (p)
     25     {
     26         cout << p->val;
     27         p = p->next;
     28     }
     29     while (p)
     30     {
     31         cout << "->";
     32         cout << setw(4) << setfill('0') << p->val;
     33         p = p->next;
     34     }
     35     cout << endl;
     36 }
     37 void my_print(Node* head)
     38 {
     39     Node* p = head->next;
     40     while (p && p->val == 0)p = p->next;
     41     if (!p) { cout << 0 << endl; return; }
     42     if (head->val == -1)cout << "-";
     43     if (p)
     44     {
     45         cout << p->val;
     46         p = p->next;
     47     }
     48     while (p)
     49     {
     50         cout << ",";
     51         cout << setw(4) << setfill('0') << p->val;
     52         p = p->next;
     53     }
     54     cout << endl;
     55 }
     56 Node* creatByVector(vector<Node*>& node_vec)
     57 {
     58     int i;
     59     if (node_vec.size() >= 2)
     60     {
     61         node_vec[0]->next = node_vec[1];
     62         node_vec[node_vec.size() - 1]->pre = node_vec[node_vec.size() - 2];
     63         node_vec[node_vec.size() - 1]->next = NULL;
     64     }
     65     for (i = 1; i < node_vec.size() - 1; i++)
     66     {
     67         node_vec[i]->next = node_vec[i + 1];
     68         node_vec[i]->pre = node_vec[i - 1];
     69     }
     70     return node_vec[0];
     71 }
     72 //使用atoi时一定要记得将字符串末尾设为
     73 Node* creatByStr(char* str)
     74 {
     75     Node* head = new Node();
     76     if (str[0] == '-')
     77     {
     78         head->val = -1;
     79         strcpy(str, &str[1]);
     80     }
     81     else
     82         head->val = 0;
     83     vector<Node*> node_vec;
     84     node_vec.push_back(head);
     85     char temp[5];
     86     int len = strlen(str);
     87     int x = len % 4;
     88     if (x)
     89     {
     90         strncpy(temp, str, x);
     91         temp[x] = '';
     92         node_vec.push_back(new Node(atoi(temp)));
     93         len -= x;
     94         strcpy(str, &str[x]);
     95     }
     96     while (len)
     97     {
     98         strncpy(temp, str, 4);
     99         temp[4] = '';
    100         node_vec.push_back(new Node(atoi(temp)));
    101         strcpy(str, &str[4]);
    102         len -= 4;
    103     }
    104     return creatByVector(node_vec);
    105 }
    106 //符号相同时 直接算加法
    107 Node* add_fun(Node* head_a, Node* head_b)
    108 {
    109     //* ta = NULL, * tb = NULL,
    110     Node* pa = head_a->next, * pb = head_b->next;
    111     while (pa->next)pa = pa->next; //ta = pa;
    112     while (pb->next)pb = pb->next; //tb = pb;
    113     vector<Node*> node_vec;
    114     int carry = 0, cur;
    115     while (pa != head_a && pb != head_b)
    116     {
    117         cur = pa->val + pb->val + carry;
    118         //cout << pa->val << " " << pb->val << " " << carry << " " << cur % 10000 << " " << endl;
    119         carry = cur / 10000;
    120         node_vec.push_back(new Node(cur % 10000));
    121         pa = pa->pre;
    122         pb = pb->pre;
    123     }
    124     while (pa != head_a)
    125     {
    126         cur = pa->val + carry;
    127         carry = cur / 10000;
    128         node_vec.push_back(new Node(cur % 10000));
    129         pa = pa->pre;
    130     }
    131     while (pb != head_b)
    132     {
    133         cur = pb->val + carry;
    134         carry = cur / 10000;
    135         node_vec.push_back(new Node(cur % 10000));
    136         pb = pb->pre;
    137     }
    138     if (carry)
    139         node_vec.push_back(new Node(carry));
    140     node_vec.push_back(new Node(head_a->val));
    141     //for (auto i : node_vec)cout << i->val << ' ';
    142     //cout << endl;
    143     reverse(node_vec.begin(), node_vec.end());
    144     //for (auto i : node_vec)cout << i->val<<' ';
    145     //cout << endl;
    146     return creatByVector(node_vec);
    147 }
    148 //符号相反时 比较绝对值的大小 最后的符号与绝对值大的相同 head_a为绝对值大的数
    149 Node* sub_fun(Node* head_a, Node* head_b)
    150 {
    151     //* ta = NULL, * tb = NULL,
    152     Node* pa = head_a->next, * pb = head_b->next;
    153     while (pa->next)pa = pa->next; //ta = pa;
    154     while (pb->next)pb = pb->next; //tb = pb;
    155     vector<Node*> node_vec;
    156     int carry = 0, cur;
    157     while (pa != head_a && pb != head_b)
    158     {
    159         //cout << pa->val << " " << pb->val << " " << carry << " " << pa->val - carry - pb->val 
    160         //    << " " << pa->val + 10000 - carry - pb->val<< endl;
    161         if (pa->val - carry >= pb->val)//若可以减
    162         {
    163             node_vec.push_back(new Node(pa->val - carry - pb->val));
    164             //cout << "1 "<<pa->val - carry - pb->val << endl;
    165             carry = 0;
    166         }
    167         else//若不够减
    168         {
    169             node_vec.push_back(new Node(pa->val + 10000 - carry - pb->val));
    170             //cout << "2 " << pa->val + 10000 - carry - pb->val << endl;
    171             carry = 1;
    172         }
    173         pa = pa->pre;
    174         pb = pb->pre;
    175     }
    176     while (pa != head_a)//被减数还有剩余
    177     {
    178         cur = pa->val - carry;
    179         if (cur > 0)
    180         {
    181             node_vec.push_back(new Node(cur));
    182             //cout << "3 " << cur << endl;
    183             carry = 0;
    184         }
    185         else if (cur < 0)
    186         {
    187             node_vec.push_back(new Node(cur + 1000));
    188             //cout << "4 " << cur+10000 << endl;
    189             carry = 1;
    190         }
    191         pa = pa->pre;
    192     }
    193     node_vec.push_back(new Node(head_a->val));
    194     //for (auto i : node_vec)cout << i->val << ' ';
    195     //cout << endl;
    196     reverse(node_vec.begin(), node_vec.end());
    197     //for (auto i : node_vec)cout << i->val<<' ';
    198     //cout << endl;
    199     return creatByVector(node_vec);
    200 }
    201 Node* my_fun(Node* ha, Node* hb, int flag)
    202 {
    203     if (ha->val == hb->val) return add_fun(ha, hb);
    204     else if (flag == -1) return sub_fun(hb, ha);
    205     else if (flag == 1)return sub_fun(ha, hb);
    206     else return new Node(0);
    207 }
    208 //比较a与b的绝对值 谁大
    209 int judge(char* a, char* b)
    210 {
    211     char ta[1000], tb[1000];
    212     if (a[0] == '-')strcpy(ta, &a[1]);
    213     else strcpy(ta, a);
    214     if (b[0] == '-')strcpy(tb, &b[1]);
    215     else strcpy(tb, b);
    216     int lena = strlen(ta), lenb = strlen(tb);
    217     if (lena > lenb) return 1;
    218     else if (lenb > lena) return -1;
    219     else
    220     {
    221         if (!strcmp(ta, tb))return 0;
    222         int i = 0;
    223         while (i != lena)
    224         {
    225             if (ta[i] > tb[i])return 1;
    226             else if (ta[i] < tb[i])return -1;
    227             i++;
    228         }
    229     }
    230 }
    231 int main()
    232 {
    233     char str_a[MY_MAX_LEN];
    234     char str_b[MY_MAX_LEN];
    235     scanf("%s", str_a); getchar();
    236     scanf("%s", str_b);
    237     int flag = judge(&str_a[0], &str_b[0]);
    238     Node* head_a = creatByStr(str_a);
    239     Node* head_b = creatByStr(str_b);
    240     my_print(head_a);
    241     my_print(head_b);
    242     cout << endl;
    243     Node* res = my_fun(head_a, head_b, flag);
    244     display(res);
    245     my_print(res);
    246     return 0;
    247 }
  • 相关阅读:
    Jetson AGX Xavier初始化
    Jetson AGX Xavier更换apt-get源
    Mac远程访问Ubuntu
    Anaconda安装和环境的搭建
    C# NotifyIcon 托盘控件
    VS2010+OpenMP的简单使用
    VS2010+OpenCV3.4.1+zbar 64位
    datatbales 使用笔记
    ubuntu16.04 常用软件
    crontab不能正常执行的五种原因
  • 原文地址:https://www.cnblogs.com/lancelee98/p/13222120.html
Copyright © 2011-2022 走看看