输出1到1000的数
最近经常见这道题:请把从1到1000的数打印出来,但你不能使用任何的循环语句或是条件语句。更不能写1000个printf或是cout。用C/C++语言。
自己用C++试了一下,怎么也不会抛出异常,难道不对?看代码:
1 #include <windows.h>
2 #include <stdio.h>
3
4 void f(int n)
5 {
6 printf("%d\n", n);
7 n / (1000-n);
8 f(n+1);
9 }
10
11 int main()
12 {
13 f(1);
14 }
15
调试使用:Visual studio 2008 Command Prompt,cl /EHsc
后直接用cl也不行,都能打印到85721这个值。难道编译器对分母为零熟视无睹???
然后直接把代码拷贝到vs2008的编译器里面,还是如此。郁闷中,随手把第一行“#include <windows.h> 删除,竟然能打印到1000了!
why?
你干啥了,Windows.h?
另外,在不删除Windows.h的情形下,将第七行“n/(1000-1)”改为:"double a = n/(1000-1)",也能正确输出到1000的数。tricky!