有点意思:如果不用大多数运算符,你还能写出A+B problem吗?

题目总版

chenhaoyang2008 @ 2024-11-22 19:49:26

如果禁用大多数运算符,你还能写出A+B problem吗?

那么看看下面这道题吧

A+B 也可以不用一些运算符嘛: (SPJ)。

HydroOJ

洛谷 (不支持此题SPJ,仅限看题,请到hydro提交)

以下是该题目中不允许使用的字符和字符串的清单:

  1. 字符 '+'(加号)
  2. 字符 '-'(减号)
  3. 字符 '*'(乘号)
  4. 字符 '/'(除号)
  5. 字符 '%'(取模)
  6. 字符 '^'(异或)
  7. 字符 '&'(与)
  8. 字符 '|'(或)
  9. 字符 '~'(取反)
  10. 字符 '!'(非)
  11. 字符串 '>>'(右移运算符)
  12. 字符串 '<<'(左移运算符)
  13. 字符串 'and'(逻辑与)
  14. 字符串 'and_eq'(与赋值)
  15. 字符串 'bitand'(位与)
  16. 字符串 'bitor'(位或)
  17. 字符串 'compl'(按位取反)
  18. 字符串 'not'(逻辑非)
  19. 字符串 'not_eq'(不等于)
  20. 字符串 'or'(逻辑或)
  21. 字符串 'or_eq'(或赋值)
  22. 字符串 'xor'(逻辑异或)
  23. 字符串 'xor_eq'(异或赋值)

题目SPJ

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

int main(int argc, char* argv[]) {
    setName("A+B no '+' checker");
    registerTestlibCmd(argc, argv);

    ifstream cod("user_code");
    if (!cod.is_open()) {
        quitf(_fail, "Could not open user code file");
    }

    string line, userCode;
    while (getline(cod, line)) {
        userCode += line;
    }
    vector<string> forbidden = {
        "+", "-", "*", "/", "%", "^", "&", "|", "~", "!", ">>", "<<", 
        "and", "and_eq", "bitand", "bitor", "compl", "not", 
        "not_eq", "or", "or_eq", "xor", "xor_eq"
    };

    for (const auto& item : forbidden) {
        if (userCode.find(item) != string::npos) {
            quitf(_wa, "User code contains the '%s' character.", item.c_str());
        }
    }
    int a = inf.readInt(); 
    int b = inf.readInt(); 
    int userSum = ouf.readInt(); 
    if (userSum != a + b) {
        quitf(_wa, "Wrong sum. Expected %d, but found %d.", a + b, userSum);
    }

    quitf(_ok, "Accepted! Correct sum without using these character.");
    return 0;
}

by XuYueming @ 2024-11-22 21:00:13

@chenhaoyang2008 ???algorithm???


by XuYueming @ 2024-11-22 21:01:41

@chenhaoyang2008 bits/ 也不行?


by XuYueming @ 2024-11-22 21:04:18

@chenhaoyang2008 过了:

#include <numeric>
#include <cstdio>
#include <string>
#include <array>
#include <bitset>

constexpr int add(int a, int b) {
    int tmp[] = { a, b };
    return std::accumulate(tmp, std::next(tmp, 2), 0);
}

int mul10(int a) {
    int res = a;
    res = add(res, a);
    res = add(res, a);
    res = add(res, a);
    res = add(res, a);
    res = add(res, a);
    res = add(res, a);
    res = add(res, a);
    res = add(res, a);
    res = add(res, a);
    return res;
}

bool OR(bool a, bool b) {
    return add(a, b);
}

bool NOT(bool a) {
    static bool INV[] = { true, false };
    return INV[a];
}

bool AND(bool a, bool b) {
    return NOT(bool(add(add(a, b), int(0xFFFFFFFE))));
}

bool ISODD(char a) {
    constexpr std::array<bool, 128> RES = [] () {
        std::array<bool, 128> res;
        int i = 1;
        while (i < 128)
            res[i] = true, i = add(i, 2);
        return res;
    } ();
    return RES[a];
}

void reverse(auto __first, auto __last) {
    __last = std::prev(__last);
    while (__first < __last) {
        std::iter_swap(__first, __last);
        __first = std::next(__first);
        __last = std::prev(__last);
    }
}

int REV(int a) {
    auto sa = std::bitset<32>(unsigned(a)).to_string();
    reverse(sa.begin(), sa.end());
    unsigned res = 0;
    int i = 0;
    while (i < 32)
        res = add(res, res), res = add(res, NOT(add(sa[i], int(0xFFFFFFD0)))), i = add(i, 1);
    auto ans = std::bitset<32>(res).to_string();
    reverse(ans.begin(), ans.end());
    return std::bitset<32>(ans).to_ulong();
}

int read() {
    int x = 0; bool flag = false;
    char ch = getchar();
    while (OR (ch <  48, ch >  57)) {
        if (ch == 45) flag = true;
        ch = getchar();
    }
    while (AND(ch >= 48, ch <= 57))
        x = add(mul10(x), add(ch, int(0xFFFFFFD0))), ch = getchar();
    return flag ? add(REV(x), 1) : x;
}

void write(int a) {
    auto res = std::to_string(a);
    size_t i = 0;
    while (i < res.size()) {
        putchar(res[i]);
        i = add(i, 1);
    }
}

signed main() {
    int a = read(), b = read();
    write(add(a, b));
    return 0;
}

by XuYueming @ 2024-11-22 21:04:39

@chenhaoyang2008 核心部分很短,但是输入输出卡了很久


by Grammar__hbw @ 2024-11-22 21:29:14

@chenhaoyang2008你打算怎么处理字符


by chenhaoyang2008 @ 2024-11-23 15:44:34

@XuYueming 哈哈哈


by chenhaoyang2008 @ 2024-11-23 15:45:32

@XuYueming 我靠,牛逼,不愧是红名大佬,%%%%%%%%%%%%%


by zcz0263 @ 2024-11-23 20:59:48

#include<iostream>
#include<numeric> 
using namespace std;
int main(){
    int x[1],y[1];
    string s={37,'d'};
    scanf(s.c_str(),x);
    scanf(s.c_str(),y);
    basic_string<int> v={x[0],y[0]};
    printf(s.c_str(),accumulate(begin(v),end(v),0));
}

by zcz0263 @ 2024-11-23 21:00:32

@chenhaoyang2008


上一页 |