洛谷题单-高精度

加法

在这里插入图片描述
传送门

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
#include <bits/stdc++.h>
using namespace std;
// https://www.luogu.com.cn/problem/P1601
char n1[10000], n2[10000];
int num1[10000], num2[10000];
int ans[10000];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n1;
cin >> n2;
int len1 = strlen(n1);
int len2 = strlen(n2);
memset(num1, 0, sizeof(num1));
memset(num2, 0, sizeof(num2));
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= len1; i++)
{
num1[i] = n1[len1 - i] - '0';
// cout << num1[i];
}
// cout << endl;
for (int i = 1; i <= len2; i++)
{
num2[i] = n2[len2 - i] - '0';
// cout << num2[i];
}
// cout << endl;
int lenc = 1;
int mod1 = 0;
while (lenc <= len1 || lenc <= len2)
{
ans[lenc] = num1[lenc] + num2[lenc] + mod1;
mod1 = ans[lenc] / 10;
ans[lenc] %= 10;
lenc++;
}
ans[lenc] = mod1;
if (ans[lenc] == 0)
lenc--; //防止上一位0
for (int i = lenc; i > 0; i--)
{
cout << ans[i];
}
return 0;
}

减法

在这里插入图片描述
传送门

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
#include <bits/stdc++.h>
using namespace std;
// https://www.luogu.com.cn/problem/P2142
char n1[1000000], n2[1000000];
char remp[1000000];
int num1[1000000], num2[1000000];
int ans[1000000];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n1 >> n2;
if (strlen(n1) < strlen(n2) || (strlen(n1) == strlen(n2) && strcmp(n1, n2) < 0))
{
//当被减数小于减数时,两者交换,且加上“-”
strcpy(remp, n1);
strcpy(n1, n2);
strcpy(n2, remp);
cout << "-";
}
int len1 = strlen(n1);
int len2 = strlen(n2);
for (int i = 1; i <= len1; i++)
{
num1[i] = n1[len1 - i] - '0';
}
for (int i = 1; i <= len2; i++)
{
num2[i] = n2[len2 - i] - '0';
}

int lenc = 1;
while (lenc <= len1 || lenc <= len2)
{
if (num1[lenc] < num2[lenc])
{
num1[lenc] += 10;
num1[lenc + 1]--;
}
ans[lenc] = num1[lenc] - num2[lenc];
// cout << num1[lenc] << " " << num2[lenc] << " " << ans[lenc] << " " << lenc << endl;
lenc++;
}
while (ans[lenc] == 0 && lenc > 1) // lenc大于1不是大于等于!
{
lenc--;
}
for (int i = lenc; i >= 1; i--)
{
cout << ans[i];
}
}

乘法

在这里插入图片描述
传送门

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
#include <bits/stdc++.h>
using namespace std;
char n1[1000000], n2[1000000];
int num1[1000000], num2[1000000], ans[1000000];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n1;
cin >> n2;
int len1 = strlen(n1);
int len2 = strlen(n2);
for (int i = 1; i <= len1; i++)
{
num1[i] = n1[len1 - i] - '0';
}
for (int i = 1; i <= len2; i++)
{
num2[i] = n2[len2 - i] - '0';
}
for (int i = 1; i <= len1; i++)
{
int mod1 = 0; //用来进位
for (int j = 1; j <= len2; j++)
{
ans[i + j - 1] += num1[i] * num2[j] + mod1;
mod1 = ans[i + j - 1] / 10;
ans[i + j - 1] %= 10;
}
ans[i + len2] = mod1;
}
int lenc = len1 + len2;
while (lenc > 1 && ans[lenc] == 0)
lenc--;
for (int i = lenc; i > 0; i--)
cout << ans[i];
return 0;
}

除法

在这里插入图片描述
传送门

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
#include <bits/stdc++.h>
using namespace std;
char n1[1000000];
int num1[1000000], ans[1000000];
long long num2;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n1;
cin >> num2;
int len1 = strlen(n1);
for (int i = 1; i <= len1; i++)
{
num1[i] = n1[i - 1] - '0';
}
long long mod1 = 0;
for (int i = 1; i <= len1; i++)
{
ans[i] = (mod1 * 10 + num1[i]) / num2;
// cout<<ans[i]<<endl;
mod1 = (mod1 * 10 + num1[i]) % num2;
}
int lenc = 1;
while (lenc < len1 && ans[lenc] == 0)
lenc++;

for (int i = lenc; i <= len1; i++)
cout << ans[i];
return 0;
}