高精度加减乘_有负数
Description
给定两个整数(可能为负数,不含前导 00,不含 −0−0),计算它们相加,相减,相乘的结果。
Input
第一行包含一个正整数 T,表示数据的组数
接下来 T 行,每行包含 22 个整数(可能为负),用空格分开,1≤整数长度≤1001≤整数长度≤100。
Output
对于每组数据输出 3 行
第 1 行为两数之和,第 2 行为两数之差,第 3 行为两数之积
注意:当结果为 0 的时候,请直接输出 0(不要输出 000,-0 等)
#include<bits/stdc++.h>
using namespace std;
const int N = 1100005;
struct BigInt {
int n;
int a[N];
int f;
void set(int x) {
f = (x >= 0 ? 1 : -1), x = abs(x);
n = 0;
while (x != 0) a[n++] = x % 10, x /= 10;
}
void set(string s) {
f = (s[0] == '-' ? -1 : 1);
n = 0;
for (int i = (int) s.size() - 1; i >= (f == -1 ? 1 : 0); i--) a[n++] = s[i] - '0';
}
BigInt& operator =(BigInt x) {
n = x.n;
for (int i = 0; i < n; i++) a[i] = x.a[i];
return *this;
}
void arrange() {
f = 1;
for (int i = n - 1; i >= 0; i--) if (a[i] != 0) {
f = (a[i] > 0 ? 1 : -1);
break;
}
int d = 0;
for (int i = 0; i < n; i++) {
int sum = d + f * a[i];
d = sum / 10;
a[i] = sum % 10;
if (a[i] < 0) a[i] += 10, d--;
}
if (d > 0) a[n++] = d;
while (n > 1 && a[n - 1] == 0) n--;
}
BigInt operator +(BigInt x) {
BigInt ans;
ans.n = max(n, x.n);
// 1、按位 + -
for (int i = 0; i < ans.n; i++) ans.a[i] = 0;
for (int i = 0; i < n; i++) ans.a[i] += f * a[i];
for (int i = 0; i < x.n; i++) ans.a[i] += x.f * x.a[i];
ans.arrange();
return ans;
}
BigInt operator -(BigInt x) {
BigInt y = x;
y.f = 0 - y.f; // 1 => -1; -1 => 1
return *this + y;
}
BigInt operator *(BigInt x) {
BigInt ans;
ans.n = n + x.n - 1;
for (int i = 0; i < ans.n; i++) ans.a[i] = 0;
for (int i = 0; i < n; i++) for (int j = 0; j < x.n; j++) {
ans.a[i + j] += f * a[i] * x.f * x.a[j];
}
ans.arrange();
return ans;
}
BigInt operator *(int x) {
BigInt y;
y.set(x);
return *this * y;
}
BigInt operator /(int x) {
BigInt ans;
ans.n = n;
int d = 0;
for (int i = n - 1; i >= 0; i--) {
d = d * 10 + a[i];
ans.a[i] = d / x;
d = d % x;
}
while (ans.n > 1 && ans.a[ans.n - 1] == 0) ans.n--;
return ans;
}
int operator %(int x) {
BigInt ans;
ans.n = n;
int d = 0;
for (int i = n - 1; i >= 0; i--) {
d = d * 10 + a[i];
ans.a[i] = d / x;
d = d % x;
}
return d;
}
void print() {
if (f == -1) cout << '-';
for (int i = n - 1; i >= 0; i--) cout << a[i];
cout << '\n';
}
};
int main(){
int T;
cin>>T;
while(T--){
string s1,s2;
cin>>s1>>s2;
BigInt a,b;
a.set(s1);b.set(s2);
BigInt c = a + b;
c.print();
BigInt x = a-b;
x.print();
BigInt y = a * b;
y.print();
}
return 0;
}
高精度除法和取模_仅正数
Description
给定两个非负整数(不含前导 00) A,B,请你计算 A/B 的商和余数。
Input
共两行,第一行包含整数 A,第二行包含整数 B。
Output
共两行,第一行输出所求的商,第二行输出所求余数。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> div(vector<int>& A, int b, int& r)
{
vector<int> C;
for (int i = A.size() - 1; i >= 0; i -- )
{
int val = r * 10 + A[i];
C.push_back(val / b);
r = val % b;
}
reverse(C.begin(), C.end());
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a;
int b;
cin >> a >> b;
vector<int> A;
for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
int r = 0;
auto C = div(A, b, r);
for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i];
cout << endl << r << endl;
return 0;
}
贸易活动
Description
在一个古老的王国中,有两位商人,甲和乙。他们经常进行各种贸易活动,由于涉及的金额通常都非常大,他们不得不采用一种特殊的方式来记录他们的账目——使用长串的数字字符串来表示金额。最近,他们进行了一次大额的货物交易,甲商人需要向乙商人支付一笔巨款,但支付完成后,甲商人发现乙商人提供的货物数量似乎不对,于是他们决定通过高精度减法来重新核对账目。
如果最终结果为负数,则返回'error',表示支付金额不足以覆盖应收金额。
Input
两行,分别表示支付金额和应收金额,金额长度不超过103
Output
支付后的剩余金额,若为0,则直接输出0,若不够支付,则输出error
#include <iostream>
#include <string>
using namespace std;
void printBIG(int a[])
{
int la = a[0];
for (int i = la; i >= 1; i--)
{
cout << a[i];
}
cout << endl;
}
void subBIG(int x[], int y[], int z[])
{
z[0] = max(x[0], y[0]);
for (int i = 1; i <= z[0]; i++)
{
z[i] = x[i] - y[i];
}
for (int i = 1; i <= z[0]; i++)
{
if (z[i] < 0)
{
z[i] += 10;
z[i + 1]--;
}
}
while (z[z[0]] == 0 && z[0] > 1)
{
z[0]--;
}
}
void s2BIG(string s, int a[])
{
int la = s.length();
for (int i = 1; i <= la; i++)
{
a[i] = s[la - i] - '0';
}
a[0] = la;
}
bool cmpBIG(int a[], int b[])
{
int la = a[0], lb = b[0];
if (la != lb)
{
return la < lb;
}
for (int i = la; i >= 1; i--)
{
if (a[i] != b[i])
{
return a[i] < b[i];
}
}
return false;
}
int a[1010], b[1010], c[1010];
int main()
{
string s1, s2;
cin >> s1 >> s2;
s2BIG(s1, a);
s2BIG(s2, b);
if(cmpBIG(a, b))
{
cout << "error" << endl;
return 0;
}
subBIG(a, b, c);
printBIG(c);
return 0;
}
计算年度净利润
Description
假设某公司在进行年度财务结算时,需要计算其年度净利润。公司的年度总收入为一个大额数字,而年度总支出由两部分组成:一部分是运营成本,另一部分是税费。请计算公司的年度净利润,即年度总收入减去运营成本再减去税费后的结果。如果结果为负数,则表示公司本年度亏损。为了准确计算年度净利润,公司需要进行高精度运算。
Input
三行,每行字符长度不超过103
第一行,一个大整数,表示公司的年度总收入
第二行是运营成本
第三行是税费
Output
年度净利润,若利润为负,则输出负数
#include <iostream>
#include <string>
using namespace std;
void printBIG(int a[])
{
int la = a[0];
for (int i = la; i >= 1; i--)
{
cout << a[i];
}
cout << endl;
}
void addBIG(int a[], int b[], int c[])
{
c[0] = max(a[0], b[0]);
for (int i = 1; i <= c[0]; i++)
{
c[i] = a[i] + b[i];
}
for (int i = 1; i <= c[0]; i++)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
if (c[c[0] + 1] > 0)
{
c[0]++;
}
}
}
void subBIG(int x[], int y[], int z[])
{
z[0] = max(x[0], y[0]);
for (int i = 1; i <= z[0]; i++)
{
z[i] = x[i] - y[i];
}
for (int i = 1; i <= z[0]; i++)
{
if (z[i] < 0)
{
z[i] += 10;
z[i + 1]--;
}
}
while (z[z[0]] == 0 && z[0] > 1)
{
z[0]--;
}
}
void s2BIG(string s, int a[])
{
int la = s.length();
for (int i = 1; i <= la; i++)
{
a[i] = s[la - i] - '0';
}
a[0] = la;
}
bool cmpBIG(int a[], int b[])
{
int la = a[0], lb = b[0];
if (la != lb)
{
return la < lb;
}
for (int i = la; i >= 1; i--)
{
if (a[i] != b[i])
{
return a[i] < b[i];
}
}
return false;
}
int a[110], b[110], c[110], d[110], e[110];
int main()
{
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
s2BIG(s1, a);
s2BIG(s2, b);
s2BIG(s3, c);
addBIG(b, c, d);
if(cmpBIG(a, d))
{
subBIG(d, a, e);
cout << "-";
printBIG(e);
return 0;
}
subBIG(a, d, e);
printBIG(e);
return 0;
}
游戏得分
Description
在一个大型在线游戏中,玩家的得分通常是非常大的数字,需要用到高精度整数来表示。游戏有一个特殊功能,允许玩家使用一种消耗品来增加得分,每次使用都会根据消耗品的等级来增加不同数量的得分。消耗品的等级用一个单精度整数来表示。玩家的当前得分和消耗品的等级都需要通过高精度乘以单精度的计算来确定使用消耗品后的新得分。
Input
两行,分别表示玩家的当前得分(长度不超过100位)和消耗品的等级(大于1)
Output
使用消耗品后的新的分
#include <iostream>
#include <string>
using namespace std;
int a[1010], b[1010], c[1010];
void printBIG(int a[])
{
int la = a[0];
for (int i = la; i >= 1; i--)
{
cout << a[i];
}
cout << endl;
}
void s2BIG(string s, int a[])
{
int la = s.length();
for (int i = 1; i <= la; i++)
{
a[i] = s[la - i] - '0';
}
a[0] = la;
}
void mulBIG(int a[], int b, int c[])
{
c[0] = a[0];
for (int i = 1; i <= c[0]; i++)
{
c[i] = a[i] * b;
}
for (int i = 1; i <= c[0]; i++)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
if(c[c[0] + 1] > 0)
{
c[0]++;
}
}
}
int main()
{
string s1;
int b;
cin >> s1 >> b;
s2BIG(s1, a);
mulBIG(a, b, c);
printBIG(c);
return 0;
}