• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

lkjy-coding

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

A+B Problem的一些离谱解法

代码均为C++

1.正解

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

2.差点TLE

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a, b;
    if (!(cin >> a >> b)) return 0;
    volatile int x = 0;
    for (int i = 0; i < 5e7; ++i) x += i * 0;
    cout << a + b << '\n';
    return 0;
}

3.差点MLE

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int a, b;
    if (!(cin >> a >> b)) return 0;
    constexpr size_t n = (1 << 20) - 64;
    vector<char> v(n);
    for (size_t i = 0; i < n; ++i) v[i] = char(i);
    cout << a + b << '\n';
    return 0;
}

4.它CE了?假的!

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    if (!(cin >> a >> b)) return 0;
    vector<int> v(1 << 20);
    cout << a + b << '\n';
    return 0;
}

5.这代码可太长了

#include <bits/stdc++.h>
using namespace std;

namespace APlusB {

template<typename T>
struct TypeTrait {
    static constexpr const char* name = "unknown";
};

template<>
struct TypeTrait<int> {
    static constexpr const char* name = "int";
};

template<>
struct TypeTrait<long long> {
    static constexpr const char* name = "long long";
};

template<typename T>
struct Input {
    static T read() {
        T x;
        std::cin >> x;
        return x;
    }
};

template<typename T>
T add(T a, T b) {
    return a + b;
}

struct Validator {
    template<typename T>
    static bool inRange(T) { return true; }
};

template<typename T>
struct Output {
    static void write(T x) {
        std::cout << x << '\n';
    }
};

template<typename T>
struct Solver {
    void solve() {
        T a = Input<T>::read();
        T b = Input<T>::read();
        if (!Validator::inRange(a) || !Validator::inRange(b)) return;
        T c = add(a, b);
        Output<T>::write(c);
    }
};

}  // namespace APlusB

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    APlusB::Solver<int> solver;
    solver.solve();
    return 0;
}

更 多 解 法

欢迎投稿!

posted on 2025-08-30 20:27  lkjy-coding  阅读(9)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3