X大于3,且X和Y之间至少相差2


11

我试图打败一些C ++。是否可以使这种情况更短?

X > 3 & X - Y > 1

(当然,除了删除空白。)

所以,X至少4,但X >= Y + 2

XY是[0,5]间隔中的整数。

我试图找到一些按位公式,但失败了。


1
@JoeZ。对于CodeGolf?为什么?只要它能正常工作……
克里斯蒂(Cristy)2014年

4
@Cristy是的,但是(到目前为止)询问高尔夫建议的问题非常少,而大多数询问建议的问题确实只是一般的编程问题-不在主题之列。因此,我可以理解为什么人们的第一反应可能是“哦,这实际上是SO的另一个问题”,甚至没有想到这可能与打高尔夫球有关。其实我希望看到更多的这些在未来,也许会有个标签为他们的一天左右的时间,这将是清楚的,立即说,你知道如何使用这个网站。;)
Martin Ender 2014年

4
如果它们是0..5(含)之间的整数,则可以使用进行相同的操作x*x-y*y>9。它的字符数相同,但是您可以找到该方法的快捷方式/替代方法。只是另一种看待方式。
Geobits,2014年

5
使用Python:3<x>y+1
avall,2014年

2
我发现了很多具有Python运算符优先级的解决方案,例如y+3<2^x,但C的运算符优先级却不同。我敢打赌,这里有7个字符的解决方案,只需要修改我的脚本来处理C运算符优先级即可
Claudiu 2014年

Answers:


11

在将所有有用的符号组合强行压下9个字符之后,我发现没有比的解决方案小了x>3&x-y>1

为了好玩,这里有一些时髦的9字符解决方案,蛮力发现者:

-x<~y>4>x
~y+x>2>>y
x*x-y*y>9
~y>x/~3*x
-3>>y>y-x
~y+x<<y>2

蛮力是在Python中完成的,它构建了自上而下的语法树,根据C的规则,没有哪个子运算符的优先级可以低于其父运算符。为了减少可能性,我只允许使用一位数字文字,并且任何二进制运算符都不能有两个常量子代。我想不出任何具有两位数字文字的解决方案,或者使用二进制运算符构建常量的解决方案。然后,对每个表达式求值[0,5],如果匹配,则将其打印出来。


我真的很喜欢x*x-y*y>9。也许您也应该尝试使用多位数字常量?(另请加上括号)
约翰·德沃夏克

@JanDvorak我也是。它很好地表达了“ x和y之间的距离”的逻辑。我认为,如果将其绘制在图表中,它将变得更加明显。
sehe 2015年

@JanDvorak我认为括号不可能是一个较小的解决方案。较小的解决方案最多可以包含8个字符,其中2个必须为xy,并且2个必须为括号,仅保留4个字符的逻辑。我将尝试使用2位数的常数运行蛮力攻击,但我真的不认为这会带来结果。
orlp 2015年

x,y,一个常数,一对括号和两个运算符怎么样?
约翰·德沃夏克

@JanDvorak (a#b)$c是一种格式。在abc必须为x和的两个y位置中[0-9xy],要保留3个可能的位置,并且只能翻转一个xy。只有有趣的运算符+-*/&|^<>,因此有9种可能性。因此总可能性小于3 * 12 * 2 * 9 * 9 <5832.
orlp

0

在响应于由(真棒)高尔夫的orlp

正确性必须放在首位

  • 其中大多数分解为某些整数类型。这包括OP中的版本
  • 有趣的是,它们确实为之工作int16_t-因此存在假设。对于32位整数,可能需要将位移提高到+16(这些天几乎到处都是)。这使他们的角色变得更大...

唯一的“正确”书写方式是IMO (x>3) && (x > y+1),它可以打成x>3&x>y+19个字符。

(您确实需要考虑(更大)无符号类型的可能性,尤其是因为在C ++表达式中无符号性是“传染性的”。我认为“修复”适当的static_cast<>s可能会破坏目的……)

更新

用下面的测试中,我已经能够找出哪些表现实际上可靠地工作:

Live On Coliru

#define REPORT(caption, expr) do {\
    do_report(caption, [](T x, T y) -> bool { return (expr); }, #expr); } while (false)

template <typename T> struct driver {
    static void run() {
        std::cout << "\n" << __PRETTY_FUNCTION__ << "\n";

        // the only two correct implementations:
        REPORT("MASTER"  , (x>3) && (x>y+1));
        REPORT("GOLF"    , x>3&x>y+1);
        REPORT("lookup"  , "000000000000000000000000111000111100"[x*6+y]-'0');

        // failing sometimes:
        REPORT("question", (x>3)&(x-y>1));
        REPORT("orlp0"   , x>3&x-y>1);
        REPORT("orlp2"   , ~y+x>2>>y);
        REPORT("orlp3"   , x*x-y*y>9);
        REPORT("orlp4"   , ~y>x/~3*x);
        REPORT("orlp5"   , -3>>y>y-x);
        REPORT("orlp6"   , ~y+x<<y>2);

        // failing always
        REPORT("orlp1"   , -x<~y>4>x);
    }
private:
    static void do_report(std::string const& caption, bool (*f)(T,T), char const* expression) {
        std::string r;
        for (T x = 0; x < 6; ++x) for (T y = 0; y < 6; ++y) r += f(x, y)?'1':'0';
        bool const correct = "000000000000000000000000111000111100" == r;
        std::cout << (correct?"OK\t":"ERR\t") << r << "\t" << caption << "\t" << expression << "\n";
    }
};

int main() {
    driver<int8_t>::run();
    driver<int16_t>::run();
    driver<int32_t>::run();
    driver<int64_t>::run();

    driver<uint8_t>::run();
    driver<uint16_t>::run();
    driver<uint32_t>::run();
    driver<uint64_t>::run();
}

在coliru上的输出,以供参考:

static void driver<T>::run() [with T = signed char]
OK  000000000000000000000000111000111100    MASTER  (x>3) && (x>y+1)
OK  000000000000000000000000111000111100    GOLF    x>3&x>y+1
OK  000000000000000000000000111000111100    lookup  "000000000000000000000000111000111100"[x*6+y]-'0'
OK  000000000000000000000000111000111100    question    (x>3)&(x-y>1)
OK  000000000000000000000000111000111100    orlp0   x>3&x-y>1
OK  000000000000000000000000111000111100    orlp2   ~y+x>2>>y
OK  000000000000000000000000111000111100    orlp3   x*x-y*y>9
OK  000000000000000000000000111000111100    orlp4   ~y>x/~3*x
OK  000000000000000000000000111000111100    orlp5   -3>>y>y-x
OK  000000000000000000000000111000111100    orlp6   ~y+x<<y>2
ERR 000000000000000000000000000000000000    orlp1   -x<~y>4>x

static void driver<T>::run() [with T = short int]
OK  000000000000000000000000111000111100    MASTER  (x>3) && (x>y+1)
OK  000000000000000000000000111000111100    GOLF    x>3&x>y+1
OK  000000000000000000000000111000111100    lookup  "000000000000000000000000111000111100"[x*6+y]-'0'
OK  000000000000000000000000111000111100    question    (x>3)&(x-y>1)
OK  000000000000000000000000111000111100    orlp0   x>3&x-y>1
OK  000000000000000000000000111000111100    orlp2   ~y+x>2>>y
OK  000000000000000000000000111000111100    orlp3   x*x-y*y>9
OK  000000000000000000000000111000111100    orlp4   ~y>x/~3*x
OK  000000000000000000000000111000111100    orlp5   -3>>y>y-x
OK  000000000000000000000000111000111100    orlp6   ~y+x<<y>2
ERR 000000000000000000000000000000000000    orlp1   -x<~y>4>x

static void driver<T>::run() [with T = int]
OK  000000000000000000000000111000111100    MASTER  (x>3) && (x>y+1)
OK  000000000000000000000000111000111100    GOLF    x>3&x>y+1
OK  000000000000000000000000111000111100    lookup  "000000000000000000000000111000111100"[x*6+y]-'0'
OK  000000000000000000000000111000111100    question    (x>3)&(x-y>1)
OK  000000000000000000000000111000111100    orlp0   x>3&x-y>1
OK  000000000000000000000000111000111100    orlp2   ~y+x>2>>y
OK  000000000000000000000000111000111100    orlp3   x*x-y*y>9
OK  000000000000000000000000111000111100    orlp4   ~y>x/~3*x
OK  000000000000000000000000111000111100    orlp5   -3>>y>y-x
OK  000000000000000000000000111000111100    orlp6   ~y+x<<y>2
ERR 000000000000000000000000000000000000    orlp1   -x<~y>4>x

static void driver<T>::run() [with T = long int]
OK  000000000000000000000000111000111100    MASTER  (x>3) && (x>y+1)
OK  000000000000000000000000111000111100    GOLF    x>3&x>y+1
OK  000000000000000000000000111000111100    lookup  "000000000000000000000000111000111100"[x*6+y]-'0'
OK  000000000000000000000000111000111100    question    (x>3)&(x-y>1)
OK  000000000000000000000000111000111100    orlp0   x>3&x-y>1
OK  000000000000000000000000111000111100    orlp2   ~y+x>2>>y
OK  000000000000000000000000111000111100    orlp3   x*x-y*y>9
OK  000000000000000000000000111000111100    orlp4   ~y>x/~3*x
OK  000000000000000000000000111000111100    orlp5   -3>>y>y-x
OK  000000000000000000000000111000111100    orlp6   ~y+x<<y>2
ERR 000000000000000000000000000000000000    orlp1   -x<~y>4>x

static void driver<T>::run() [with T = unsigned char]
OK  000000000000000000000000111000111100    MASTER  (x>3) && (x>y+1)
OK  000000000000000000000000111000111100    GOLF    x>3&x>y+1
OK  000000000000000000000000111000111100    lookup  "000000000000000000000000111000111100"[x*6+y]-'0'
OK  000000000000000000000000111000111100    question    (x>3)&(x-y>1)
OK  000000000000000000000000111000111100    orlp0   x>3&x-y>1
OK  000000000000000000000000111000111100    orlp2   ~y+x>2>>y
OK  000000000000000000000000111000111100    orlp3   x*x-y*y>9
OK  000000000000000000000000111000111100    orlp4   ~y>x/~3*x
OK  000000000000000000000000111000111100    orlp5   -3>>y>y-x
OK  000000000000000000000000111000111100    orlp6   ~y+x<<y>2
ERR 000000000000000000000000000000000000    orlp1   -x<~y>4>x

static void driver<T>::run() [with T = short unsigned int]
OK  000000000000000000000000111000111100    MASTER  (x>3) && (x>y+1)
OK  000000000000000000000000111000111100    GOLF    x>3&x>y+1
OK  000000000000000000000000111000111100    lookup  "000000000000000000000000111000111100"[x*6+y]-'0'
OK  000000000000000000000000111000111100    question    (x>3)&(x-y>1)
OK  000000000000000000000000111000111100    orlp0   x>3&x-y>1
OK  000000000000000000000000111000111100    orlp2   ~y+x>2>>y
OK  000000000000000000000000111000111100    orlp3   x*x-y*y>9
OK  000000000000000000000000111000111100    orlp4   ~y>x/~3*x
OK  000000000000000000000000111000111100    orlp5   -3>>y>y-x
OK  000000000000000000000000111000111100    orlp6   ~y+x<<y>2
ERR 000000000000000000000000000000000000    orlp1   -x<~y>4>x

static void driver<T>::run() [with T = unsigned int]
OK  000000000000000000000000111000111100    MASTER  (x>3) && (x>y+1)
OK  000000000000000000000000111000111100    GOLF    x>3&x>y+1
OK  000000000000000000000000111000111100    lookup  "000000000000000000000000111000111100"[x*6+y]-'0'
ERR 000000000000000000000000111001111100    question    (x>3)&(x-y>1)
ERR 000000000000000000000000111001111100    orlp0   x>3&x-y>1
ERR 111111011111001111000111111011111101    orlp2   ~y+x>2>>y
ERR 011111001111000111000011111001111100    orlp3   x*x-y*y>9
ERR 111111111111111111111111111111111111    orlp4   ~y>x/~3*x
ERR 111111011111001111000111111011111101    orlp5   -3>>y>y-x
ERR 111111011111001111000111111011111101    orlp6   ~y+x<<y>2
ERR 000000000000000000000000000000000000    orlp1   -x<~y>4>x

static void driver<T>::run() [with T = long unsigned int]
OK  000000000000000000000000111000111100    MASTER  (x>3) && (x>y+1)
OK  000000000000000000000000111000111100    GOLF    x>3&x>y+1
OK  000000000000000000000000111000111100    lookup  "000000000000000000000000111000111100"[x*6+y]-'0'
ERR 000000000000000000000000111001111100    question    (x>3)&(x-y>1)
ERR 000000000000000000000000111001111100    orlp0   x>3&x-y>1
ERR 111111011111001111000111111011111101    orlp2   ~y+x>2>>y
ERR 011111001111000111000011111001111100    orlp3   x*x-y*y>9
ERR 111111111111111111111111111111111111    orlp4   ~y>x/~3*x
ERR 111111011111001111000111111011111101    orlp5   -3>>y>y-x
ERR 111111011111001111000111111011111101    orlp6   ~y+x<<y>2
ERR 000000000000000000000000000000000000    orlp1   -x<~y>4>x

摘要

由于这与重复源代码元素的“成本”有关,因此您可以使用查找表。您可以“隐藏”查找表,因此可以

 LUT[x][y]

要么

 LUT[x*6+y]

当然,您可以学究而钝化并重命名LUT

 L[x][y]

所以我的“版本”是... 7个字符。(或者使函数和L(x,y)更短)。

或者,更重要的是:正确,可测试和可维护。


增加了“真正的”高尔夫。不能少于9个字符,但第一个正确!
sehe 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.