问题标题: a+b

3
2
已解决
陶春光
陶春光
中级守护
中级守护

1.常规方法

#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b;
    return 0;
}

2.树状数组

#include <bits/stdc++.h>
#define lowbit(a) a & -a
using namespace std;
const int N = 10;
int t[N];
void update(int x, int d) {
    for (; x < N; x += lowbit(x)) {
        t[x] += d;
    }
}
int query(int x) {
    int res = 0;
    for (; x; x -= lowbit(x)) {
        res += t[x];
    }
    return res;
}
int main() {
    for (int i = 1; i <= 2; ++i) {
        int a;
        cin >> a;
        update(i, a);
    }
    cout << query(2);
    return 0;
}

3.线段树

#include <bits/stdc++.h>
using namespace std;
struct node {
    int l, r, sum; 
} t[3];
int a[3]; 
void build(int p, int l, int r) {
    t[p].l = l, t[p].r = r;
    if (l == r) {
        t[p].sum = a[l];
        return;
    }
    int mid = (l + r) / 2;
    build(p*2, l, mid), build(p*2+1, mid+1, r);
    t[p].sum = t[p*2].sum + t[p*2+1].sum;
}
void update(int p, int x, int d) {
    if (t[p].l == t[p].r) {
        t[p].sum += d;
        return;
    }
    int mid = (t[p].l + t[p].r) / 2;
    if (x <= mid) {
        update(p*2, x, d);
    } else {
        update(p*2+1, x, d);
    }
    t[p].sum = t[p*2].sum + t[p*2+1].sum;
}
int query(int p, int l, int r) {
    if (t[p].l <= l && r <= t[p].r) {
        return t[p].sum;
    }
    int mid = (t[p].l + t[p].r) / 2, res = 0;
    if (l <= mid) {
        res += query(p*2, l, r);
    }
    if (r > mid) {
        res += query(p*2+1, l, r);
    }
    return res;
}
int main() {
    cin >> a[1] >> a[2];
    build(1, 1, 2);
    for (int i = 1; i <= 2; ++i) {
        update(1, i, a[i]), update(1, i, -a[i]);
    }
    cout << query(1, 1, 2);
    return 0;
}

4.二分查找

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a, b, l = 0, r = 1e9;
    cin >> a >> b;
    while (l < r) {
        int mid = (l + r) / 2;
        if (a + b <= mid) {
            r = mid;
        } else {
            l = mid + 1;
        }
    }
    cout << l;
    return 0;
}

5.队列

#include <bits/stdc++.h>
using namespace std;
int main() {
    queue<int> q;
    int ans = 0;
    for (int i = 1; i <= 2; ++i) {
        int a;
        cin >> a;
        q.push(a);
    }
    while (!q.empty()) {
        ans += q.front();
        q.pop();
    }
    cout << ans;
    return 0;
}

6.栈

#include <bits/stdc++.h>
using namespace std;
int main() {
    stack<int> q;
    int ans = 0;
    for (int i = 1; i <= 2; ++i) {
        int a;
        cin >> a;
        q.push(a);
    }
    while (!q.empty()) {
        ans += q.top();
        q.pop();
    }
    cout << ans;
    return 0;
}

7.浪费法

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a, b, ans = 0;
    cin >> a >> b;
    while (a) {
        a--, ans++;
    }
    while (b) {
        b--, ans++;
    }
    cout << ans;
    return 0;
}

8.二进制

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a, b, ans = 0;
    cin >> a >> b;
    for (int k = 1; a; a >>= 1, k *= 2) {
        if (a & 1) {
            ans += k;
        }
    }
    for (int k = 1; b; b >>= 1, k *= 2) {
        if (b & 1) {
            ans += k;
        }
    }
    cout << ans;
    return 0;
}

9.高精度

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a[10], b[10], c[20], len1 = 0, len2 = 0, len3, A, B;
    memset(c, 0, sizeof c);
    cin >> A >> B;
    while (A) {
        a[len1++] = A % 10;
        A /= 10;
    }
    while (B) {
        b[len2++] = B % 10;
        B /= 10;
    }
    len3 = max(len1, len2);
    for (int i = 0; i < len3; ++i) {
        c[i] += a[i] + b[i];
        c[i+1] += c[i] / 10, c[i] %= 10;
    }
    if (c[len3]) {
        len3++;
    }
    for (int i = len3 - 1; i >= 0; --i) {
        cout << c[i];
    }
    return 0;
}

10.python3超短代码

print(sum(map(int, input().split())))

11.打表

#include <bits/stdc++.h>
using namespace std;
int A[10] = {3, 45, 123, 91086199, 42267194, 69274392, 5710219, 75601477, 70597795, 82574652};
int B[10] = {4, 55, 321, 18700332, 60645282, 10635835, 85140568, 24005804, 90383234, 22252146};
int C[10] = {7, 100, 444, 109786531, 102912476, 79910227, 90850787, 99607281, 160981029
, 104826798};
int main() {
    int a, b;
    cin >> a >> b;
    for (int i = 0; i < 10; ++i) {
        if (A[i] == a && B[i] == b) {
            cout << C[i];
        }
    }
    return 0;
}

12.快读快写

#include <bits/stdc++.h>
using namespace std;
int read() {
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') {
            f = -1;
        }
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}
void write(int x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x >= 10) {
        write(x/10);
    }
    putchar(x%10+'0');
}
int main() {
    int a = read(), b = read();
    write(a+b);
    return 0;
}

13.极限卡点(9888ms)

#include <bits/stdc++.h>
using namespace std;
int main() {
    for (int i = 1; i <= 421500000; ++i);
    int a, b;
    cin >> a >> b;
    cout << a + b;
    return 0;
}

14.Floyd算法

#include <bits/stdc++.h>
using namespace std;
int f[10][10];
int main() {
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10; ++j) {
            f[i][j] = 1e9;
        }
    }
    int a, b;
    cin >> a >> b;
    f[3][9] = a, f[9][3] = a;
    f[4][9] = b, f[9][4] = b;
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10; ++j) {
            for (int k = 0; k < 10; ++k) {
                f[j][k] = min(f[j][k], f[j][i] + f[k][i]);
            }
        }
    }
    cout << f[3][4];
    return 0;
}

15.暴力枚举

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    for (int i = 0; i <= 200000000; ++i) {
        if (a + b == i) {
            cout << i;
            break;
        }
    }
    return 0;
}

16.分块

#include <bits/stdc++.h>
#define N 10
using namespace std;
int n = 2, t, L[N], R[N], pos[N], a[N], add[N], sum[N];
void update(int l, int r, int d) {
    int p = pos[l], q = pos[r];
    if (p == q) {
        sum[p] += d * (r - l + 1);
        for (int i = l; i <= r; ++i) {
            a[i] += d;
        }
    } else {
        sum[p] += d * (R[p] - l + 1), sum[q] += d * (r - L[q] + 1);
        for (int i = l; i <= R[p]; ++i) {
            a[i] += d;
        }
        for (int i = L[q]; i <= r; ++i) {
            a[i] += d;
        }
        for (int i = p + 1; i < q; ++i) {
            add[i] += d;
        }
    }
}
int query(int l, int r) {
    int res = 0, p = pos[l], q = pos[r];
    if (p == q) {
        for (int i = l; i <= r; ++i) {
            res += a[i];
        }
        res += (r - l + 1) * add[p];
    } else { 
        res += (R[p] - l + 1) * add[p] + (r - L[q] + 1) * add[q];
        for (int i = l; i <= R[p]; ++i) {
            res += a[i];
        }
        for (int i = L[q]; i <= r; ++i) {
            res += a[i];
        }
        for (int i = p + 1; i < q; ++i) {
            res += sum[i] + (R[i] - L[i] + 1) * add[i];
        }
    }
    return res;
}
int main() {
    t = sqrt((double)n);
    for (int i = 1; i <= t; ++i) {
        L[i] = (i - 1) * t + 1, R[i] = i * t;
    }
    if (R[t] < n) {
        t++;
        L[t] = R[t-1] + 1, R[t] = n;
    }
    for (int i = 1; i <= t; ++i) {
        for (int j = L[i]; j <= R[i]; ++j) {
            pos[j] = i;
        }
    }
    for (int i = 1; i <= n; ++i) {
        int p;
        cin >> p;
        update(i, i, p);
    }
    cout << query(1, n);
    return 0;
}

17.define**

#include <bits/stdc++.h>
using namespace std;
#define A int
#define B main
#define C (
#define D )
#define E {
#define F a
#define G ,
#define H b
#define I ;
#define J cin
#define K >>
#define L cout
#define M <<
#define N +
#define O return
#define P 0
#define Q }
#define R A B C D E
#define S A F G H I
#define T J K F K H I
#define U L M F N H I
#define V O P I
#define W R S T U V Q
W

真的只能写出这么多了


0
0
张皓然
张皓然
中级光能
中级光能

emmm....

加一个时间复杂度吧

0
0
0
0
0
我要回答