• 【刷题篇】笔试真题


    复数乘法

    复数 可以用字符串表示,遵循 “实部+虚部i” 的形式,并满足下述条件:
    实部 是一个整数,取值范围是 [-100, 100]
    虚部 也是一个整数,取值范围是 [-100, 100]
    i^2 == -1
    给你两个字符串表示的复数 num1 和 num2 ,请你遵循复数表示形式,返回表示它们乘积的字符串。
    在这里插入图片描述

    class Solution {
    public:
        int convert1(string& str)
        {
            int num=0;
            int flag=1;
            for(auto e :str)
            {
                if(e=='-')
                {
                    flag=-1;
                }
                else
                {
                    if(e>='0'&&e<='9')
                    {
                        num=num*10+e-'0';
                    }
                }
            }
            return num*flag;
        }
    
    
        string complexNumberMultiply(string num1, string num2) {
            vector<int> arr1(0);
            vector<int> arr2(2,0);
            vector<int> arr(2,0);
            int pos=num1.find('+');
            string str1=num1.substr(0,pos);
            string str2=num1.substr(pos+1);
            arr1[0]=convert1(str1);
            arr1[1]=convert1(str2);
    
            int pos1=num2.find('+');
            string str3=num2.substr(0,pos1);
            string str4=num2.substr(pos1+1);
            arr2[0]=convert1(str3);
            arr2[1]=convert1(str4);
    
            arr[0]=arr1[0]*arr2[0]-arr1[1]*arr2[1];
            arr[1]=arr1[0]*arr2[1]+arr1[1]*arr2[0];
            return to_string(arr[0]) + "+" + to_string(arr[1]) + "i";
        }
            // string ans = "";
            // int idx1 = num1.find('+'), idx2 = num2.find('+');
            // int a = stoi(num1.substr(0, idx1));
            // int b = stoi(num1.substr(idx1 + 1, num1.size() - idx1 - 1));
            // int c = stoi(num2.substr(0, idx2));
            // int d = stoi(num2.substr(idx2 + 1, num2.size() - idx2 - 1));
            // ans += to_string(a * c - b * d);
            // ans += "+";
            // ans += to_string(a * d + c * b);
            // ans += "i";
            //return ans;
        //}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    一年中的第几天

    给你一个字符串 date ,按 YYYY-MM-DD 格式表示一个 现行公元纪年法 日期。返回该日期是当年的第几天
    在这里插入图片描述

    class Solution {
    public:
        int dayOfYear(string date) {
            int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
            
            string yy = date.substr(0, 4);
            string mm = date.substr(5, 2);
            string dd = date.substr(8, 2);
    
            int y = stoi(yy);
            int m = stoi(mm);
            int d = stoi(dd);
    
            int ans = 0;
            if (((y%4==0)&&(y%100!=0))||(y%400==0)) days[1]++;
    
            for (int i = 0; i < m - 1; i++) {
                ans += days[i];
            }
            ans += d;
    
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    字符串相加

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和并同样以字符串形式返回。
    你不能使用任何內建的用于处理大整数的库(比如 BigInteger), 也不能直接将输入的字符串转换为整数形式。

    class Solution {
    public:
        int Sum(int num1,int num2,int& sign)
        {
            int sum=num1+num2+sign;
            if(sum>=10)
            {
                sum-=10;
                sign=1;
            }
            else
                sign=0;
            return sum;
        }
        string addStrings(string num1, string num2) {
            int i=0,j=0,sign=0;
            string newstr;
            //方便尾插
            reverse(num1.begin(),num1.end());
            reverse(num2.begin(),num2.end());
            while(i<num1.size()&&j<num2.size())
                newstr.push_back('0'+(Sum(num1[i++]-'0',num2[j++]-'0',sign)));
            while(i<num1.size())//防止sign有数据
                newstr.push_back('0'+(Sum(num1[i++]-'0',0,sign)));
            while(j<num2.size())//防止sign有数据
                newstr.push_back('0'+(Sum(num2[j++]-'0',0,sign)));
            //考虑最后的值大于十
            if(sign==1)
                newstr.push_back('1');
            //最后在反转回来
            reverse(newstr.begin(),newstr.end());
            return newstr;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    字符串相乘

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
    注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。

    在这里插入图片描述

    class Solution {
    public:
    
        int Sum(int num1,int num2,int& sign)
        {
            int sum=num1+num2+sign;
            if(sum>=10)
            {
                sum-=10;
                sign=1;
            }
            else
                sign=0;
            return sum;
        }
        string addStrings(string& num1, string& num2) {
            int i=0,j=0,sign=0;
            string newstr;
            //方便尾插
            reverse(num1.begin(),num1.end());
            reverse(num2.begin(),num2.end());
            while(i<num1.size()&&j<num2.size())
                newstr.push_back('0'+(Sum(num1[i++]-'0',num2[j++]-'0',sign)));
            while(i<num1.size())//防止sign有数据
                newstr.push_back('0'+(Sum(num1[i++]-'0',0,sign)));
            while(j<num2.size())//防止sign有数据
                newstr.push_back('0'+(Sum(num2[j++]-'0',0,sign)));
            //考虑最后的值大于十
            if(sign==1)
                newstr.push_back('1');
            //最后在反转回来
            reverse(newstr.begin(),newstr.end());
            return newstr;
        }
    
        string multiply(string num1, string num2) {
            if (num1 == "0" || num2 == "0") {
                return "0";
            }
            string ans = "0";
    
            int m=num1.size();
            int n=num2.size();
            
            for(int i=n-1;i>=0;i--)
            {
                string str;
                int add=0;
                for(int j=n-1;j>i;j--)
                {
                    str.push_back(0);
                }
                int y=num2.at(i)-'0';
                for(int j=m-1;j>=0;j--)
                {
                    int x=num1.at(j)-'0';
                    int product=x*y+add;
                    str.push_back(product%10);
                    add=product/10;
                }
                while(add!=0)
                {
                    str.push_back(add%10);
                    add/=10;
                }
                reverse(str.begin(), str.end());
                for (auto &c : str) {
                    c += '0';
                }
                ans = addStrings(ans, str);
            }
            return ans;
        }
    
    
        // string addStrings(string &num1, string &num2) {
        //     int i = num1.size() - 1, j = num2.size() - 1, add = 0;
        //     string ans;
        //     while (i >= 0 || j >= 0 || add != 0) {
        //         int x = i >= 0 ? num1.at(i) - '0' : 0;
        //         int y = j >= 0 ? num2.at(j) - '0' : 0;
        //         int result = x + y + add;
        //         ans.push_back(result % 10);
        //         add = result / 10;
        //         i--;
        //         j--;
        //     }
        //     reverse(ans.begin(), ans.end());
        //     for (auto &c: ans) {
        //         c += '0';
        //     }
        //     return ans;
        // }
    
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
  • 相关阅读:
    C++17future类+可变参模板实现线程池
    Parity Game——种类并查集、权值并查集、离散化
    Dynamic Head: Unifying Object Detection Heads with Attentions
    threadx netxduo stm32f407上实现http server
    字符集导致19c集群安装故障
    Java基础 面试题
    回调函数的简单使用0717
    51单片机学习:DS18B20温度传感器实验
    状态管理Vuex
    10月12日
  • 原文地址:https://blog.csdn.net/m0_66599463/article/details/133917667