c/c++语言开发共享高精度加法实现(c++)

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 示例: 输入:(2 -> 4 -> …

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

程序代码如下:

#include <iostream> #include <cstdio> using namespace std; struct listnode  {     int val;     listnode *next;     listnode(int x) : val(x), next(null) {} };  listnode* addtwonumbers(listnode* l1, listnode* l2);  int main() {     int  m,n;     cin>>m>>n;     int temp_m;     cin>>temp_m;     listnode *head_m = new listnode(temp_m);     listnode *index = head_m;     for(int i = 0;i<m-1;++i)     {         cin>>temp_m;         listnode *new_m_node = new listnode(temp_m);         index->next = new_m_node;         index = new_m_node;     }     int temp_n;     cin>>temp_n;     listnode*head_n = new listnode(temp_n);     index = head_n;     for(int i = 0;i<n-1;++i)     {         cin>>temp_n;         listnode *new_n_node = new listnode(temp_n);         index->next = new_n_node;         index = new_n_node;     }     listnode *ans = addtwonumbers(head_m, head_n);     while (ans->next!=null)     {         cout<<ans->val<<endl;         ans = ans->next;     }     cout<<ans->val;     return 0; }  listnode* addtwonumbers(listnode* l1, listnode* l2)  {     bool flag = true;     listnode *ans = null;     listnode *ans_head = null;     bool ifadd = false;     while (l1!=null&&l2!=null)     {         if(!ifadd)         {             if(flag)             {                 flag = false;                 ans = new listnode((l1->val+l2->val)%10);                 ans_head = ans;             }             else             {                 ans->next = new listnode((l1->val+l2->val)%10);                 ans = ans->next;             }             if((l1->val+l2->val)/10==1)                 ifadd = true;             l1 = l1->next;             l2 = l2->next;         }         else         {             if(flag)             {                 flag = false;                 ans = new listnode((l1->val+l2->val+1)%10);                 ans_head = ans;             }             else             {                 ans->next = new listnode((l1->val+l2->val+1)%10);                 ans = ans->next;             }             if((l1->val+l2->val+1)/10==0)                 ifadd = false;             l1 = l1->next;             l2 = l2->next;         }     }     if(l1==null&&l2==null)     {         if (ifadd)         {             ans->next = new listnode(1);         }         else         {             return ans_head;         }     }     if(l1==null&&l2!=null)     {         l1 = l2;     }     while (l1!=null)     {         if(!ifadd)         {             if(flag)             {                 flag = false;                 ans =                  ans_head = ans;             }             else             {                 ans->next = new listnode((l1->val)%10);                 ans = ans->next;             }                          if((l1->val)/10==1)                 ifadd = true;             l1 = l1->next;         }         else         {             if(flag)             {                 flag = false;                 ans = new listnode((l1->val+1)%10);                 ans_head = ans;             }             else             {                 ans->next = new listnode((l1->val+1)%10);                 ans = ans->next;             }                          if((l1->val+1)/10==0)                 ifadd = false;             l1 = l1->next;         }     }     if (ifadd)     {         ans->next = new listnode(1);     }     else     {         return ans_head;     }     return ans_head; }

在该题目中,我总共犯了两个错误:

1.在对指针赋值时,将空指针赋值给要创建对象的指针,导致新实例化的对象,并没有进入到链表当中。

2.忘记处理最后的进位

如若该题目改成链表的存储方式为由高位到低位存储,则还需要再建立一个反向的单链表由低位到高位存储答案,以便于发生多次进位的时候逐级向高位进位。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/599902.html

(0)
上一篇 2021年5月9日
下一篇 2021年5月9日

精彩推荐