午餐时游戏的代码挑战问题

发布于 2024-12-12 10:40:59 字数 185 浏览 0 评论 0原文

所以,我将举办一场午餐会,并且我正在寻找一项有趣的活动。我记得看过一个谷歌技术谈话视频,一开始有一系列用 C 或 C++ 编写的“这段代码会做什么”的谜题,我认为做类似的事情是个好主意。

你知道那个视频是哪个吗,因为我找不到它了?你知道有什么网站可以让我得到类似的谜题吗?如果它们是 C/C++ 语言,我会更喜欢,

谢谢:)

So, I will be hosting a lunch and I am looking for an interesting activity. I remember having seen a google tech talk video that, in the beginning, had a series of "what will this code do" puzzles in c or c++, and I thought it would be a good idea to do something similar.

Do you know which that video was, as I cannot find it again? Do you know of any site from which I could get some puzzles like that? I would prefer if they were in C/C++

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静若繁花 2024-12-19 10:40:59

也许不完全是你想要的,但仍然:

您可以使用所有 ASCII 符号,字母 a...z、A...Z 和数字 0...9 除外;
另外,还可以使用符号2.构造一个最短的C表达式,其值为100。

例如:22*(2+2)+2*(2+2+2)是long-气喘吁吁; (222-22)/2 很短(10 个符号),但不是最短的。

或者,相同,但您应该使用 0 而不是 2;该解决方案有点长,但它存在:)我很高兴解决这个问题。

Maybe not exactly what you want, but still:

You can use all ASCII symbols, except letters a...z, A...Z and numbers 0...9;
in addition, you can use the symbol 2. Construct a shortest C expression whose value is 100.

For example: 22*(2+2)+2*(2+2+2) is long-winded; (222-22)/2 is short (10 symbols) but not shortest.

Or, the same, but you should use 0 instead of 2; the solution is a bit longer but it exists :) I had fun solving this stuff.

南风几经秋 2024-12-19 10:40:59

弄清楚这将打印什么:

#include <stdio.h>

char *c[]=
{
        "ENTER",
        "NEW",
        "POINT",
        "FIRST"
};

char **cp[]={c+3,c+2,c+1,c};

char ***cpp=cp;

int main(void)
{
        printf("%s",**++cpp);
        printf("%s ",*--*++cpp+3);
        printf("%s",*cpp[-2]+3);
        printf("%s\n",cpp[-1][-1]+1);
        return 0;
}

另外,请参阅这本书

Figure out what this will print:

#include <stdio.h>

char *c[]=
{
        "ENTER",
        "NEW",
        "POINT",
        "FIRST"
};

char **cp[]={c+3,c+2,c+1,c};

char ***cpp=cp;

int main(void)
{
        printf("%s",**++cpp);
        printf("%s ",*--*++cpp+3);
        printf("%s",*cpp[-2]+3);
        printf("%s\n",cpp[-1][-1]+1);
        return 0;
}

Also, see this book.

永不分离 2024-12-19 10:40:59

找到一组逻辑问题供人们推理。其中一些可能有封闭的解决方案,另一些可能开放讨论:

您受雇从农场购买三匹马。当你到达时,你会看到这里有 25 匹马,而且它们有一条跑道,你可以让它们互相竞争,但可惜的是,你忘了带秒表,农场里没有秒表。您需要进行多少场比赛才能选出三匹跑得最快的马? (假设马不累)

封闭的房间里有三个灯泡,房间外面有三个开关。每个开关作用于其中一个灯泡,但没有标记。你能想出一种方法来识别每个灯泡只进入房间一次的开关吗?

在一个早已被遗忘的王国里,有 100 名智者被关在死牢里。在国王生日那天,他决定为智者提供获得自由的机会,因此他决定进行以下游戏:所有囚犯都站成一队,国王将戴上一顶帽子,帽子可以是白色的,也可以是白色的。黑色,在每个囚犯的头顶上。每种颜色的帽子数量未知。然后,国王将从队列中的最后一个囚犯开始询问每个囚犯,如果他猜自己会被释放,那么他的帽子是什么颜色。智者唯一能说的就是颜色:白色或黑色。智者们在夜间进行讨论并制定了总体最佳策略(即能够拯救最多囚犯的策略)。策略是什么?有多少智者能保证获得自由?

两个智者被关在一座普通监狱的高塔里,监狱里还有 10 到 13 名其他囚犯。智者的一扇窗户朝北,另一扇朝南。监狱有两个操场,中午时分,部分普通犯人到北操场练习运动,其余的犯人(除智者外)则到南操场。智者不能互相交谈,但他们知道自己是智者。监狱每天都会要求他们猜测囚犯总数。如果其中一个智者猜对了,两人都将在日落前获得自由,但如果猜错了,两人都将无法活着看到新的日出。第三天,一位智者猜出了这个数字,然后两人就回家了。监狱里有多少囚犯?智者怎么知道的?

我的老阿姨必须吃两种药,A和B,每种药每天一粒,她必须非常小心,不要在同一天吃两种相同类型的药物或一天不吃一种药物或整个治疗就会失败。不幸的是,这两种药物几乎没有区别,因此医生告诉她在处理药物时要非常小心。昨天,她没有注意到,从瓶子里取出了三粒药。通过数瓶子中剩余的药丸,她知道桌子上有 2 颗 A 药丸和 1 颗 B 药丸。她是如何设法继续那一天和接下来的几天的治疗的?

Find a set of logic problems for people to reason about. Some of them might have closed solutions, others might be open for discussion:

You are hired to buy three horses from a farm. When you arrive you see that there are 25 horses and that they have a track where you can have them compete against each other, but alas, you forgot to bring the stop-watch and there is none in the farm. How many races do you need to conduct to select the three fastest horses? (assuming that horses don't get tired)

There are three light bulbs in a closed room, and three switches outside of the room. Each switch acts on one of the bulbs, but they are not labeled. Can you figure out a way to identify what switch each bulb entering the room only once?

There are 100 wise men in the death row of a long forgotten kingdom. The day of the king's birthdate he decides to provide the wise men with the opportunity of gaining freedom, and for that reason he decides on the following game: all prisoners will stand in a queue, and the king will place a hat, either white or black, on top of the head of each prisoner. The number of hats of each color is unknown. Then the king will ask each prisoner starting from the last one in the queue what the color of his hat is, if he guesses he'll be free. The only thing that the wise men can say is a color: white or black. The wise men discuss it during the night and devise an optimal strategy overall (i.e. an strategy that will save the most number of prisoners). What is the strategy, and how many wise men are guaranteed to gain freedom?

Two wise men are prisoners in a high tower in a common prison in which there are 10 or 13 other prisoners. One of the wise men's window faces north, the other south. The prison has two fields and at noon, some of the common prisoners goes out to the north field to practice sports, while the rest of the prisoners (except the wise men) go to the south field. The wise men cannot talk with each other, but they know that they are wise. The ward will ask them each day to guess the total number of prisoners. If either of the wise men guesses, both will be free before sunset, but if the guess is wrong both will not live to see a new sunrise. On the third day one of the wise men guesses the number and both go home. How many prisoners are there in the prison? How did the wise men know?

My old aunt has to take two medicines, A and B, one pill each day of each medicine and she must be very careful not to have two of the same type in the same day or skipping one kind for a day or the whole treatment will fail. Unfortunately, the two medicines are virtually indistinguishable, so the doctor told her to be very careful in handling the medicine. Yesterday, she did not notice and pulled three pills out of the bottles. By counting the remaining pills in the bottles she knows that there are 2 A and 1 B pills on the table. How did she manage to continue with her treatment for that and the following days?

风蛊 2024-12-19 10:40:59

我不知道有什么网站,但这是我不久前为一场比赛写的一个网站。
假设 biguint 是一个任意精度的无符号整数类,

biguint poorly_named_function(unsigned long long input)
{
    if (Solve==1) return biguint(1);
    register biguint Even = 0;
    register biguint Odd = 1;
    register unsigned long long Loops = input/16+1;
    switch (input%16/2) {
        do {
    case 0: Even+=Odd; Odd+=Even;
    case 7: Even+=Odd; Odd+=Even;
    case 6: Even+=Odd; Odd+=Even;
    case 5: Even+=Odd; Odd+=Even;
    case 4: Even+=Odd; Odd+=Even;
    case 3: Even+=Odd; Odd+=Even;
    case 2: Even+=Odd; Odd+=Even;
    case 1: Even+=Odd; Odd+=Even;
        }while(--Loops);
    };
    return (input&1 ? Odd+Even : Odd);
}

它会找到斐波那契数 input,其中输入 1-10 的结果为 [1,1,2,3,5,8,13,21,34,55 ]
即使使用相对愚蠢的编译器,它也非常非常快。

I don't know any sites, but here's one I wrote for a constest a while back.
Assume biguint is an arbitrary-precision unsigned integer class

biguint poorly_named_function(unsigned long long input)
{
    if (Solve==1) return biguint(1);
    register biguint Even = 0;
    register biguint Odd = 1;
    register unsigned long long Loops = input/16+1;
    switch (input%16/2) {
        do {
    case 0: Even+=Odd; Odd+=Even;
    case 7: Even+=Odd; Odd+=Even;
    case 6: Even+=Odd; Odd+=Even;
    case 5: Even+=Odd; Odd+=Even;
    case 4: Even+=Odd; Odd+=Even;
    case 3: Even+=Odd; Odd+=Even;
    case 2: Even+=Odd; Odd+=Even;
    case 1: Even+=Odd; Odd+=Even;
        }while(--Loops);
    };
    return (input&1 ? Odd+Even : Odd);
}

It's finds the Fibonacci number input where inputs 1-10 result in [1,1,2,3,5,8,13,21,34,55]
It's also very very fast, even with a relatively stupid compiler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文