不带循环或条件打印 1 到 1000
任务:在不使用任何循环或条件语句的情况下打印从 1 到 1000 的数字。不要只编写 printf()
或 cout
语句 1000 次。
您将如何使用 C 或 C++ 来做到这一点?
Task: Print numbers from 1 to 1000 without using any loop or conditional statements. Don't just write the printf()
or cout
statement 1000 times.
How would you do that using C or C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
这实际上编译为没有任何条件的程序集:
Edit: Added '&' so it will consider the address hence evading the pointer errors.
标准 C 中的上述版本,因为它不依赖于函数指针的算术:
This one actually compiles to assembly that doesn't have any conditionals:
Edit: Added '&' so it will consider the address hence evading the pointer errors.
This version of the above in standard C, since it doesn't rely on arithmetic on function pointers:
编译时递归! :P
Compile time recursion! :P
我很惊讶似乎没有人发布这个——我认为这是最明显的方式。
1000 = 5*5*5*8。
I'm surprised nobody seems to have posted this -- I thought it was the most obvious way.
1000 = 5*5*5*8.
看起来不需要使用循环
Looks like it doesn't need to use loops
这是我所知道的三种解决方案。但第二个可能有争议。
[ 编辑: (1) 和 (4) 仅可用于编译时常量,(2) 和 (3) 也可用于运行时表达式 - 结束编辑。 >]
Here are three solutions that I know. The second might be argued though.
[ Edit: (1) and (4) can be used for compile time constants only, (2) and (3) can be used for runtime expressions too — end edit. ]
我不会把 printf 语句写 1000 遍!
不客气 ;)
I'm not writing the printf statement 1000 times!
You're welcome ;)
它不会打印所有数字,但它会“打印从 1 到 1000 的数字”。含糊不清的胜利问题! :)
It doesn't print all the numbers, but it does "Print numbers from 1 to 1000." Ambiguous question for the win! :)
触发致命错误!这是文件 countup.c:
编译,然后在 shell 提示符下执行:
这确实打印了从 1 到 1000 的数字,没有任何循环或条件!
Trigger a fatal error! Here's the file, countup.c:
Compile, then execute on a shell prompt:
This does indeed print the numbers from 1 to 1000, without any loops or conditionals!
使用系统命令:
Using system commands:
未经测试,但应该是普通标准 C:
Untested, but should be vanilla standard C:
与这里的其他人相比有点无聊,但可能是他们正在寻找的东西。
A bit boring compared to others here, but probably what they're looking for.
该任务从未指定程序必须在 1000 之后终止。
(如果您运行 ./a.out 且不带额外参数,则可以缩短为此)
The task never specified that the program must terminate after 1000.
(Can be shortened to this if you run ./a.out with no extra params)
简单易行! :P
Easy as pie! :P
我们可以启动 1000 个线程,每个线程打印一个数字。安装OpenMPI,使用
mpicxx -o 1000 1000.cpp
编译并使用mpirun - 运行np 1000 ./1000
。您可能需要使用limit
或ulimit
增加描述符限制。请注意,这将相当慢,除非您有大量核心!当然,数字不一定按顺序打印,但问题并不要求它们按顺序排列。
We can launch 1000 threads, each printing one of the numbers. Install OpenMPI, compile using
mpicxx -o 1000 1000.cpp
and run usingmpirun -np 1000 ./1000
. You will probably need to increase your descriptor limit usinglimit
orulimit
. Note that this will be rather slow, unless you have loads of cores!Of course, the numbers won't necessarily be printed in order, but the question doesn't require them to be ordered.
使用纯 C:
当然,您可以对其他基数实现相同的想法(2: print2 print4 print8 ...),但这里的数字 1000 建议基数 10。您还可以减少添加中间函数的行数: < code>print2() print10() print20() print100() print200() print1000() 和其他等效替代方案。
With plain C:
Of course, you can implement the same idea for other bases (2: print2 print4 print8 ...) but the number 1000 here suggested base 10. You can also reduce a little the number of lines adding intermediate functions:
print2() print10() print20() print100() print200() print1000()
and other equivalent alternatives.只需将 std::copy() 与特殊迭代器一起使用即可。
Just use std::copy() with a special iterator.
函数指针(ab)使用。没有预处理器魔法来增加输出。 ANSI C.
Function pointer (ab)use. No preprocessor magic to increase output. ANSI C.
丑陋的 C 答案(每 10 的幂仅展开一个堆栈帧):
Ugly C answer (unrolled for only one stack frame per power of 10):
堆栈溢出:
这是针对 8MB 堆栈的。每个函数调用大约占用 32 个字节(因此是 32 * 1000)。但当我运行它时,我只得到了 804(因此是 196 * 32;也许 C 运行时堆栈中还有其他部分,您也必须扣除)。
Stack overflow:
This is for an 8MB stack. Each function invocation appears to take about 32 bytes (hence the 32 * 1000). But then when I ran it I only got to 804 (hence the 196 * 32; perhaps the C runtime has other parts in the stack that you have to deduct also).
函数指针的乐趣(不需要新奇的 TMP):
附带说明:我也禁止将条件扩展到逻辑和关系运算符。如果允许逻辑否定,递归调用可以简化为:
Fun with function pointers (none of that new-fangled TMP needed):
As a side note: I took the prohibition against conditionals to extend to logical and relational operators as well. If you allow logical negation, the recursive call can be simplified to:
我觉得这个答案会非常简单易懂。
I feel this answer will be very simple and easy to understand.
我错过了所有的乐趣,所有好的 C++ 答案都已经发布了!
这是我能想到的最奇怪的事情,不过我不敢打赌它是合法的 C99 :p
另一种,有一点作弊:
最后一个想法,同样的作弊:
I missed all the fun, all the good C++ answers have already been posted !
This is the weirdest thing I could come up with, I wouldn't bet it's legal C99 though :p
Another one, with a little cheating :
Last idea, same cheat :
简单易行:
执行方法:
规范没有说序列必须在代码内部生成:)
Easy as pie:
method of execution:
The specification does not say that the sequence must be generated inside the code :)
更多预处理器滥用:
我感觉很肮脏;我想我现在就去洗澡。
More preprocessor abuse:
I feel so dirty; I think I'll go shower now.
如果 POSIX 解决方案被接受:
If POSIX solutions are accepted:
由于对错误没有限制..
或者甚至更好(?),
Since there is no restriction on bugs..
Or even better(?),