有点意思:如果不用大多数运算符,你还能写出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 litjohn @ 2024-11-22 19:53:28

@chenhaoyang2008 你这样读入都很困难吧。


by litjohn @ 2024-11-22 19:54:07

@chenhaoyang2008我好像知道了:用文言。


by Grammar__hbw @ 2024-11-22 19:54:23

@chenhaoyang2008 &和<<至少留一个才能输入


by Grammar__hbw @ 2024-11-22 19:54:36

@litjohn 正确的


by qazsedcrfvgyhnujijn @ 2024-11-22 19:55:33

没法 IO 啊


by XuYueming @ 2024-11-22 19:55:56

@chenhaoyang2008 首先你这个 checker 就不对:

#include <cstdio>
#include <tuple>

#define AND b##itand
#define XOR x##or
#define LSHIFT <##<

int a, b;

int add(int a, int b) {
    int x = a AND b, y = a XOR b;
    while (x) {
        std::tie(x, y) = std::make_tuple(y AND x LSHIFT 1, y XOR x LSHIFT 1);
    }
    return y;
}

signed main() {
    scanf("%d%d", &a, &b);
    printf("%d", add(a, b));
    return 0;
}

by XuYueming @ 2024-11-22 19:56:36

@XuYueming 哦哦哦,忘记处理位与了


by XuYueming @ 2024-11-22 19:57:29

@chenhaoyang2008 这样:

#include <cstdio>
#include <tuple>

#define AND b##itand
#define XOR x##or
#define LSHIFT <##<

int a, b;

int add(int a, int b) {
    int x = a AND b, y = a XOR b;
    while (x) {
        std::tie(x, y) = std::make_tuple(y AND x LSHIFT 1, y XOR x LSHIFT 1);
    }
    return y;
}

signed main() {
    scanf("%d%d", AND a, AND b);
    printf("%d", add(a, b));
    return 0;
}

by XuYueming @ 2024-11-22 20:01:01

@chenhaoyang2008 不是哥们,字符串里的 % 也管?那得处理一下了。


by chenhaoyang2008 @ 2024-11-22 20:01:19

@Grammar__hbw getchar putchar


| 下一页