任务:打印从1到1000的数字,不使用任何循环或条件语句。不要只写printf()或cout语句1000次。
用C或c++怎么做呢?
任务:打印从1到1000的数字,不使用任何循环或条件语句。不要只写printf()或cout语句1000次。
用C或c++怎么做呢?
当前回答
#include <stdio.h>
int main(int argc, char** argv)
{
printf("numbers from 1 to 1000\n");
}
其他回答
您可以使用System()打印1到1000(通过使用DOS命令)
include <process.h>
void main()
{
system("cmd.exe /c for /l %x in (1, 1, 1000) do echo %x" );
}
运行程序的.exe(可执行)文件,显示1到1000
注意:在WINDOWS中测试
显然需要Windows/Visual Studio…但它确实有效。
#include <stdio.h>
#include <Windows.h>
void print(int x)
{
int y;
printf("%d\n", x);
__try
{
y = 1 / (x - 1000);
print(x + 1);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
return;
}
}
void main()
{
print(1);
}
#include <stdio.h>
#include <assert.h>
void foo( int n )
{
printf("%d\n", n);
assert( n > 0 );
foo(--n);
}
int main()
{
foo( 1000 );
getchar();
}
#include <stdio.h>
int show(int i) {
printf("%d\n",i);
return( (i>=1000) || show(i+1));
}
int main(int argc,char **argv) {
return show(1);
}
||操作符使递归调用短路,以显示i为>= 1000时的情况。
应该在任何不喜欢0 / 0的机器上工作。如果需要,可以用空指针引用替换它。程序可以在打印1到1000后失败,对吧?
#include <stdio.h>
void print_1000(int i);
void print_1000(int i) {
int j;
printf("%d\n", i);
j = 1000 - i;
j = j / j;
i++;
print_1000(i);
}
int main() {
print_1000(1);
}