前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[笔试强训day06]

[笔试强训day06]

作者头像
南桥
发布2024-05-02 07:54:59
810
发布2024-05-02 07:54:59
举报
文章被收录于专栏:南桥谈编程南桥谈编程

NC10 大数乘法

NC10 大数乘法

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
#include <string>
#include <vector>
class Solution {
  public:

    string solve(string s, string t) {
        int m = s.size(), n = t.size();
        reverse(s.begin(), s.end());
        reverse(t.begin(), t.end());

        //1.无进位相加并相乘
        vector<int> tmp(m + n);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                tmp[i + j] += (s[i] - '0') * (t[j] - '0');
            }
        }
        //2.处理进位
        int c = 0;
        string ans;
        for (auto x : tmp) {
            c = c + x;
            ans += c % 10 + '0';
            c = c / 10;
        }
        while (c) {
            ans += c % 10 + '0';
            c /= 10;
        }

        //3.处理前置0
        while (ans.size() > 1 && ans.back() == '0') ans.pop_back();

        reverse(ans.begin(), ans.end());
        return ans;

    }
};

NC1 大数加法

NC1 大数加法

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
#include <type_traits>
class Solution {
public:

    string solve(string s, string t) {
        int i=s.size()-1,j=t.size()-1;
        int tmp=0;  //进位符
        string ans;
        while(i>=0||j>=0||tmp)
        {
            int sum=0;
            if(i>=0) tmp+=s[i--]-'0';
            if(j>=0) tmp+=t[j--]-'0';
            ans+=tmp%10+'0';
            tmp/=10;
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
};

NC40 链表相加(二)

NC40 链表相加(二)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
#include <new>
class Solution {
public:
    ListNode* reverse(ListNode* head)
    {
        ListNode* newhead=new ListNode(0);
        ListNode* cur=head;

        while(cur)
        {
            ListNode* next=cur->next;
            cur->next=newhead->next;
            newhead->next=cur;
            cur=next;
        }
        cur=newhead->next;
        delete newhead;
        return cur;
    }
    ListNode* addInList(ListNode* head1, ListNode* head2) {
        //逆序
        head1=reverse(head1),head2=reverse(head2);
        ListNode *cur1=head1,*cur2=head2;
        ListNode *ret=new ListNode(0);
        ListNode *prve=ret;
        int t=0;
        while(cur1||cur2||t)
        {
            if(cur1)
            {
                t+=cur1->val;
                cur1=cur1->next;
            }
            if(cur2)
            {
                t+=cur2->val;
                cur2=cur2->next;
            }
            prve=prve->next=new ListNode(t%10);
            t/=10;
        }
        cur1=ret->next;
        delete ret;
        return reverse(cur1);

    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NC10 大数乘法
  • NC1 大数加法
  • NC40 链表相加(二)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档