加载中...

参考代码

洛谷 P1601 高精度 A+B

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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
string s1, s2;
int a[maxn], b[maxn];
void read(string s, int n[]) {
for (int i = 0; i < s.size(); ++i) {
n[i] = s[s.size() - i - 1] - '0';
}
}
void add() {
for (int i = 0; i <= 500; ++i) {
a[i] = a[i] + b[i];
while (a[i] >= 10) {
a[i] -= 10;
a[i + 1]++;
}
}
}
void print() {
int p = 501;
while (p >= 0 && a[p] == 0) p--;
if (p < 0) {
cout << 0;
}
while (p >= 0) cout << a[p--];
cout << endl;
}
int main()
{
cin >> s1 >> s2;
read(s1, a), read(s2, b);
add();
print();
return 0;
}

一本通 1169 高精度 A-B

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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
string s1, s2;
int a[maxn], b[maxn];
void read(string s, int n[]) {
for (int i = 0; i < s.size(); ++i) {
n[i] = s[s.size() - i - 1] - '0';
}
}
void add() {
for (int i = 0; i <= 500; ++i) {
a[i] = a[i] + b[i];
while (a[i] >= 10) {
a[i] -= 10;
a[i + 1]++;
}
}
}
void minu() {
for (int i = 0; i <= 500; ++i) {
a[i] = a[i] - b[i];
while (a[i] < 0) {
a[i] += 10;
a[i + 1]--;
}
}
}
void print() {
int p = 501;
while (p >= 0 && a[p] == 0) p--;
if (p < 0) {
cout << 0;
}
while (p >= 0) cout << a[p--];
cout << endl;
}
int main()
{
cin >> s1 >> s2;
read(s1, a), read(s2, b);
minu();
print();
return 0;
}

一本通 1172 高精度阶乘

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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50005;
string s1, s2;
int a[maxn], b[maxn], c[maxn];
void read(string s, int n[]) {
for (int i = 0; i < s.size(); ++i) {
n[i] = s[s.size() - i - 1] - '0';
}
}
void add(int a[], int b[]) {
for (int i = 0; i < maxn / 2; ++i) {
a[i] = a[i] + b[i];
a[i + 1] += a[i] / 10;
a[i] %= 10;
}
}
void minu(int a[], int b[]) {
for (int i = 0; i < maxn / 2; ++i) {
a[i] = a[i] - b[i];
while (a[i] < 0) {
a[i] += 10;
a[i + 1]--;
}
}
}
void print(int a[]) {
int p = maxn;
while (p >= 0 && a[p] == 0) p--;
if (p < 0) {
cout << 0;
}
while (p >= 0) cout << a[p--];
cout << endl;
}
int main() {
int n;
cin >> n;
a[0] = 1;
int weishu = 0;
for (int i = 2; i <= n; ++i) {
for (int j = 0; j <= weishu; ++j) {
a[j] = a[j] * i;
}
for (int j = 0; j < weishu + 5; ++j) {
if (a[j] >= 10) {
weishu = j + 1;
}
if (a[j] >= 10) {
a[j + 1] += a[j] / 10;
a[j] = a[j] % 10;
}
}
}
print(a);
}

一本通 1171 大整数因子

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;
const int maxn = 50005;
string s1, s2;
int a[maxn], b[maxn];
void print(int a[]) {
int p = maxn;
while (p >= 0 && a[p] == 0) p--;
if (p < 0) {
cout << 0;
}
while (p >= 0) cout << a[p--];
cout << endl;
}
int main() {
cin >> s1;
bool flag = 0;
int len = s1.size();
for (int k = 2; k <= 9; ++k) {
int mod = 0;
for (int i = 0; i < len; ++i) {
mod = (mod * 10 + (s1[i] - '0')) % k;
}
if (!mod) {
cout << k << " ";
flag = 1;
}
}
if (!flag) cout << "none";
cout << endl;
}

一本通 1181 整数奇偶排序

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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 15;
int a[maxn];
int b[maxn];
void bubble_sort1(int l, int r) {
for (int i = 0; i < r - l + 1; ++i) {
for (int j = l; j < r; ++j) {
if (b[j] > b[j + 1]) {
swap(b[j], b[j + 1]);
}
}
}
}
void bubble_sort2(int l, int r) {
for (int i = 0; i < r - l + 1; ++i) {
for (int j = l; j < r; ++j) {
if (b[j] < b[j + 1]) {
swap(b[j], b[j + 1]);
}
}
}
}
signed main()
{
for (int i = 0; i < 10; ++i) {
cin >> a[i];
}
int p1 = 0, p2 = 9;
for (int i = 0; i < 10; ++i) {
if (a[i] % 2 == 0) {
b[p2] = a[i];
p2--;
}
else {
b[p1] = a[i];
p1++;
}
}
bubble_sort2(0, p1 - 1);
bubble_sort1(p1, 9);
for (int i = 0; i < 10; ++i)
cout << b[i] << " ";
cout << endl;
return 0;
}

一本通 1184 明明的随机数(去重与排序)

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;
const int maxn = 15;
bool v[1001];
int a[105];
int b[105];
void bubble_sort(int l, int r) {
for (int i = 0; i < r - l + 1; ++i) {
for (int j = l; j < r; ++j) {
if (b[j] > b[j + 1]) {
swap(b[j], b[j + 1]);
}
}
}
}
signed main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int p = 0;
for (int i = 0; i < n; ++i) {
if (!v[a[i]]) {
v[a[i]] = 1;
b[p] = a[i];
p++;
}
}
bubble_sort(0, p - 1);
cout << p << endl;
for (int i = 0; i < p; ++i) {
cout << b[i] << " ";
}
cout << endl;
return 0;
}

P1177 【模板】快速排序

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;
const int maxn = 1e5 + 5;
int a[maxn];
void quick_sort(int l, int r) {
if(r <= l) {
return;
}
int i = l, j = r;
int mid = a[(l + r) >> 1];
while(i <= j){
while(a[i] < mid) i++;
while(a[j] > mid) j--;
if(i <= j){
swap(a[i], a[j]);
i++;
j--;
}
}
quick_sort(l, j);
quick_sort(i, r);
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
quick_sort(1, n);
for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
return 0;
}

P1223 排队接水 贪心+排序

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
#include <bits/stdc++.h>
using namespace std;
const int maxn =1005;
struct node {
int x, id;
bool operator < (const node & y) {
return x < y.x;
}
}a[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) a[i].id = i, cin >> a[i].x;
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; ++i) cout << a[i].id << " ";
cout << endl;
double ans = 0;
for (int i = 1; i < n; ++i) {
ans += (n - i) * a[i].x;
}
ans /= n;
cout << fixed << setprecision(2) << ans << endl;
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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
int b[maxn];
void merge_sort(int l, int r) {
if (l == r) return;
int mid = (l + r) / 2;
merge_sort(l, mid);
merge_sort(mid + 1, r);
int p1 = l, p2 = mid + 1, p3 = l;
while (p1 <= mid || p2 <= r) {
if (p1 <= mid && p2 <= r) {
if (a[p1] < a[p2]) {
b[p3] = a[p1];
p3++; p1++;
}
else {
b[p3] = a[p2];
p3++; p2++;
}
}
else if (p1 <= mid) {
b[p3] = a[p1];
p3++; p1++;
}
else {
b[p3] = a[p2];
p3++; p2++;
}
}
for (int i = l; i <= r; ++i) a[i] = b[i];
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
merge_sort(1, n);
for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
return 0;
}

评论