算法------大整数

大整数

1.大整数加法

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<ctime>
#include<cstdlib>
#include<stack>
#include<queue>
using namespace std;
#define max_n 1000
char str1[max_n], str2[max_n];
int num1[max_n], num2[max_n];



int main() {
    memset(str1, 0, sizeof(str1));
    memset(str2, 0, sizeof(str2));
    memset(num1, 0, sizeof(num2));
    memset(num2, 0, sizeof(num2));
    scanf("%s%s", str1, str2);
    int len1 , len2;
    len1 = strlen(str1);
    len2 = strlen(str2);
    int j = 0;
    int max = len1 > len2 ? len1 : len2;//取长度最大的赋值给max
    for (int i = len1 - 1 ;i >= 0 ;i--) 
    num1[j++] = str1[i] - '0';
  	//到过来存储
    j = 0;
    for (int i = len2 - 1 ;i >= 0 ;i--) 
    num2[j++] = str2[i] - '0';
  	//到过来存储
    for (int i = 0 ;i < max; i++) {//以最长的长度为基准
        num2[i] += num1[i];
        if (num2[i] >= 10) {
            num2[i] -= 10;
            num2[i + 1] += 1;
        }
    }
    if (num2[max]) printf("%d", num2[max]);
    for (int i = max - 1;i >= 0; i--) {
        printf("%d", num2[i]);
    }
    printf("\n");
    return 0;
}

代码解析

1.第一种要注意到的就是位数不想等的情况,所以我门要先对齐位数,再去模仿竖式

​ 就用 int max = len1 > len2 ? len1 : len2

2.做完之后将数字到着存在数组中,为了方便思考。

3.之后就是核心思想进位 10位进1, 向下一位进,这就是到这存储的原因

2.大整数减法

思想就是比大整数加法繁琐一点

最主要的一点就是大整数减法是要去借位,去向上一位借1位, 上一位减一位

#include <iostream>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <algorithm>
using namespace std;

string str1, str2, str3;
int aa[1005], bb[1005], cc[1005], la, lb, lc;

int main(){
    cin >> str1 >> str2;
    la = str1.size();
    lb = str2.size();
    if(la < lb || ((la == lb) && (str1 < str2))){
        str3 = str2;
        str2 = str1;
        str1 = str3;
        cout << "-";
    }

    la = str1.size();
    lb = str2.size();
    for(int i = 0; i < la; i++){
        aa[i] = str1[la - i -1] - '0';
    }

    for(int i = 0; i < lb; i++){
        bb[i] = str2[lb - i -1] - '0';
    }
    int k = 0;
    for(int i = 0; i < la; i++){
        k = aa[i] - bb[i];
        if(k < 0){
            k += 10;
            aa[i + 1] -= 1;
        }
        cc[i] = k;
    }

    while(cc[la] == 0) la--;
    for(int i = la; i >= 0 ; i--){
        cout << cc[i];
    }
    cout << endl;
    return 0;
}

3.大整数乘法

image-20200913153519868

模拟竖式,将乘的结果放在数组中,最后处理一下今进位的问题

image-20200913153832280

数组的0位是长度,倒着存储。

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <map>
#include <stack>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
char num1[105], num2[105];
int num3[105], num4[105], ans[305];
//用数组的0位去存长度可以节省i部分的空间
int main() {
    cin >> num1 >> num2;
    for (int i = strlen(num1) - 1, j = 1; i >= 0; i--, j++) {
        num3[j] = num1[i] - '0';
    }
    for (int i = strlen(num2) - 1, j = 1; i >= 0; i--, j++) {
        num4[j] = num2[i] - '0';
    }
    num3[0] = strlen(num1);
    num4[0] = strlen(num2);
    for (int i = 1; i <= num3[0]; i++) {
        for (int j = 1; j <= num4[0]; j++) {
            ans[i + j - 1] += num3[i] * num4[j];
        }
    }
  //核心思想
    for (int i = 1; i <= num3[0] + num4[0]; i++) {
        if (ans[i] >= 10) {
            ans[i + 1] += ans[i] / 10;
            ans[i] %= 10;
        }
        if (ans[i] != 0) {
            ans[0] = i;
        }
    }
    for (int i = ans[0]; i > 0; i--) {
        cout << ans[i];
    }
    cout << endl;
    return 0;
}

4.大整数除法

1.模拟竖式,每次判断当前缓存是否大于除数

2.若大于则减去一次除数,答案加一 并继续判断

3.若不是则将除数的下一位加入缓存 同时答案进一位 并继续判断

4.直到被除数全部加入缓存 且缓存不大于除数

5.此时答案即为商 缓存即为余数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下贪心算法在装船问题上的应用。 装船问题是一个典型的贪心算法应用。题意为:给定一个数组 weights,其中 weights[i] 表示第 i 个物品的重量;另外给定一个正整数 D,表示船的载重量。现在需要将所有物品装到船上,且每个物品必须被装进船里。问最少需要多少艘船才能完成任务。 贪心策略为:每次尽可能地装满一艘船。具体实现过程为: 1. 将物品按照重量从小到大排序。 2. 从左右两端分别取物品,尽可能地让它们的重量相加不超过 D。 3. 如果两个物品的重量之和超过了 D,那么只能将较重的物品装入一艘船,另一艘船再单独装。 4. 重复以上过程,直到所有物品被装入船中。 下面是一个示例代码: ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; int findMinBoats(vector<int>& weights, int D) { sort(weights.begin(), weights.end()); // 按照重量从小到大排序 int left = 0, right = weights.size() - 1; int boats = 0; while(left <= right) { if(weights[left] + weights[right] <= D) { // 尽可能地让两个物品的重量相加不超过 D left++; right--; } else { // 如果两个物品的重量之和超过了 D,那么只能将较重的物品装入一艘船,另一艘船再单独装 right--; } boats++; // 每次运载完成后船数加 1 } return boats; } int main() { vector<int> weights = {1, 3, 2, 2, 1}; int D = 3; int minBoats = findMinBoats(weights, D); cout << minBoats << endl; // 输出最少需要的船数 return 0; } ``` 输出结果为:3,表示最少需要三艘船才能完成任务。 希望能帮到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值