简单
2024-07-29 15:51:52
发布于:浙江
8阅读
0回复
0点赞
// 老师这太简单了
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <queue> /* 队列 */
#include <vector> /* 动态数组 */
#include <map> /* 映射 */
#include <set> /* 集合 */
#include <stack> /* 栈 */
#include <list> /* 链表 */
//#include <private_queue>
const int MAXM = 2005;
const int MAXN = 1005;
using namespace std;
int a[1000005],b[1000005],c[1000005],bb;
//容器 -> Containers
void add(string s1,string s2) {
int len1 = s1.size();
int len2 = s2.size();
for (int i = 0; i < len1; i ++) a[i]=s1[len1-i-1]-'0'; // 逆序存储数字a到数组a里面
for (int i = 0; i < len2; i ++) b[i]=s2[len2-i-1]-'0'; // 逆序存储数字b到数组b里面
int len = max(len1,len2);
for (int i = 0; i < len; i ++) {
c[i] += a[i] + b[i]; // 同为相加
if (c[i] > 9) { //进位
c[i] -= 10;
c[i+1] += 1;
}
}
while (c[len] == 0) len --; // 寻找最高位的下标
for (int i = len; i >= 0; i --) cout << c[i];
}
void minuss(string s1,string s2) {
// 判断大小决定是否输出负号
if(s1.size() == s2.size()) {
if(s2 > s1) {
cout << "-";
swap(s1,s2);
}
} else {
if(s1.size() < s2.size()) {
cout << "-";
swap(s2,s1);
}
}
int len1=s1.size();
int len2=s2.size();
for(int i = 0; i < len1; i ++) a[i] = s1[len1 - i - 1] - '0';
for(int i = 0; i < len2; i ++) b[i] = s2[len2 - i - 1] - '0';
// 减法计算
int len=max(len1,len2);
for(int i = 0; i < len; i ++) {
c[i] = a[i] - b[i];
if(c[i] < 0) {
a[i+1] --;
c[i] += 10;
}
}
// 输出答案
while(c[len] == 0 && len >= 1) len --;
for(int i = len; i >= 0; i --) {
cout << c[i];
}
}
void multiplication(string s1,string s2) {
int len1=s1.size();
int len2=s2.size();
for(int i = 0; i < len1; i ++) a[i] = s1[len1 - i - 1] - '0';
for(int i = 0; i < len2; i ++) b[i] = s2[len2 - i - 1] - '0';
for (int i = 0;i < len1;i ++) {
// 枚举数字a的每一位
for (int j = 0;j < len2;j ++) {
// 枚举数字b的每一位
c[i + j] += a[i] * b[j];
if (c[i + j] >= 10) { // 进位
c[i + j + 1] += c[i + j] / 10;
c[i + j] %= 10;
}
}
}
int len = len1 + len2;
while (c[len] == 0) len --;
for (int i = len;i >= 0;i --) {
cout << c[i];
}
}
void division_low_order(string s1,string s2) {
int len = s1.size();
for (int i = 0;i < len;i ++) {
a[i]=s1[i] - '0';
}
long long ute=0; // 保存余数和当前可以除的数字
// 从高往低进行运算
for (int i = 0;i < len; ++ i) {
ute = ute * 10 + a[i];
if (ute >= bb) {
c[i] = ute / bb;
ute %= bb;
}
}
int left = 0;
while (c[left] == 0 && left <= len) left ++;
for (int i = left;i < len;i ++) cout << c[i];
cout << "\n" << ute;
}
int main() {
string s1,s2;
cin >> s1 >> s2;
add(s1,s2);
// minuss(s1,s2);
// multiplication(s1,s2);
// division_low_order(s1,bb);
return 0;
}
这里空空如也
有帮助,赞一个