跳到正文

个人记录

各种算法,但是A+B

Ja50nY0un9约 6 分钟
原文
文章信息与版本

文章信息

发布
2022年11月23日
收录
2026年2月25日
更新
2026年11月12日
篇幅
约 6 分钟

版本

  1. v1当前

这里收录 A+B problem 运用 C++ 从入门到 NOI/NOI+/CTSC 知识的不超过 114514 种写法。

也许持续更新。

本博客全程不使用万能头文件,大部分不使用STL,码风稳定,请放心食用。

虽然大部分时间作者的码风并不是这样。

保证所有代码都能编译成功,但不保证所有代码都能通过 P1001。

只保证在输入均大于 0 且小于 1e9 时正确,但不保证不会 TLE。

顺序

C++
#include <iostream>
using namespace std;

int main(){
    int a, b;
    cin >> a >> b;
    cout << a+b << endl;
    return 0;
}

分支

C++
#include <iostream>
using namespace std;

int ab(int x){
    return x >= 0 ? x : -x;
}

int main(){
    int a, b;
    cin >> a >> b;
    if (b > 0)
        cout << a + b <<endl;
    else
        cout << a - ab(b) <<endl;
    return 0;
}

循环

C++
#include <iostream>
using namespace std;

int main(){
    int a, b;
    cin >> a >> b;
    int ans = 0;
    for (int i = 1; i <= a; i++)
        ans++;
    for (int i = 1; i <= b; i++)
        ans++;
    cout << ans <<endl;
    return 0;
}

数组

C++
#include <iostream>
using namespace std;

const int MAXN = 10;
int num[MAXN];

int main(){
    cin >> num[0] >> num[1];
    cout << num[0] + num[1] << endl;
    return 0;
}

函数

C++
#include <iostream>
using namespace std;

int solve(int a, int b){
    return a + b;
}

int main(){
    int a, b;
    cin >> a >> b;
    cout << solve(a, b) << endl;
    return 0;
}

递归

C++
#include <iostream>
using namespace std;

int dfs(int step, int a, int b){
    if (step == a + b)
        return step;
    return dfs(step + 1, a, b);
}

int main(){
    int a, b;
    cin >> a >> b;
    cout << dfs(1, a, b) << endl;
    return 0;
}

筛法

C++
#include <iostream>
using namespace std;

const int MAXN = 2e9 + 10;
bool flag[MAXN];

int main(){
    int a, b;
    cin >> a >> b;
    int ans = 0;
    for (int i = 1; i < MAXN; i++)
        if (i != a && i != b) flag[i] = true;
    for (int i = 1; i < MAXN; i++)
        if (!flag[i]) ans += i;
    cout << ans;
    return 0;
}

队列

C++
#include <iostream>
using namespace std;

const int MAXN = 1e5 + 10;
const int MOD = 1e5;
int q[MAXN];
int l = 0, r = 0;

void push(int x){
    q[r] = x;
    r++;
    r %= MOD;
}

bool empty(){
    return l == r;
}

int front(){
    return q[l];
}

void pop(){
    l++;
    l %= MOD;
}

int main(){
    int a, b;
    cin >> a >> b;
    push(a); push(b);
    if (!empty())
        a = front();
    if (!empty())
        cout << a + front() << endl;
    return 0;
}

快速幂

C++
# include <iostream>
# define ll long long
using namespace std;

ll power(ll b, ll p, ll k){
    ll ret = 1;
    while (p){
        if (p & 1)
            ret *= b, ret %= k;
        b *= b, b %= k;
        p >>= 1;
    }
    return ret;
}

int main(){
    int a, b;
    cin >> a >> b;
    cout << power(a + b, 1, 2 << 31 - 1) << endl;
    return 0;
}

二分

C++
# include <iostream>
# define ll long long
using namespace std;

int main(){
    ll a, b;
    cin >> a >> b;
    ll l = -2e9, r = 2e9;
    ll ans;
    while (l <= r){
        ll mid = (l + r) >> 1;
        if (mid > a + b)
            r = mid - 1;
        else{
            ans = mid;
            l = mid + 1;
        }
    }
    cout << ans << endl;
    return 0;
}
原文
存档评论20 条抓取于