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

@XuYueming

好家伙,大写


by XuYueming @ 2024-11-22 20:03:54

@chenhaoyang2008 过了:

#include <cstdio>
#include <tuple>

#define AND b##ita##nd
#define XOR x##o##r
#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;
}

const char INTFMT[3] = { 37, 'd', '\0' };

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

by XuYueming @ 2024-11-22 20:04:13

@chenhaoyang2008 这是我的提交记录:链接。


by chenhaoyang2008 @ 2024-11-22 20:04:22

@XuYueming

WA了


by XuYueming @ 2024-11-22 20:05:54

@chenhaoyang2008 现在过了


by chenhaoyang2008 @ 2024-11-22 20:06:10

@chenhaoyang2008 好吧,恭喜AC,下次加正则


by chenhaoyang2008 @ 2024-11-22 20:12:41

@XuYueming 我以为会有人用奇怪的STL过,结果是这么过的


by XuYueming @ 2024-11-22 20:13:30

@chenhaoyang2008 那我也会奇怪的 STL。


by XuYueming @ 2024-11-22 20:38:09

@chenhaoyang2008 不是,连 for 都不让用?


by XuYueming @ 2024-11-22 20:39:08

@chenhaoyang2008 只能手写 while


上一页 | 下一页