请把从1到1000的数打印出来,但你不能使用任何的循环语句或是条件语句。
来源:互联网 发布:淘宝手机详情页加链接 编辑:程序博客网 时间:2024/05/21 08:36
转载自:酷壳,http://coolshell.cn/articles/3445.html#more-3445
有这样一个面试题——请把从1到1000的数打印出来,但你不能使用任何的循环语句或是条件语句。更不能写1000个printf或是cout。用C/C++语言。
我相信,大多数人一开始你可能想到的是递归算法:
1
2
3
4
5
6
7
void
f(
int
n){
printf
(
"%d\n"
,n);
(1000-n) ? f(n+1) :
exit
(0) ;
}
int
main(){
f(1);
}
当然,题目中说了不能使用条件语句,所以,上面那种解法的不符合题意的,因为还是变向地使用了条件表达式。不过,我们可以用别的方法来让这个递归终止,比如:
除以零,当程序crash,呵呵。
1
2
3
4
5
void
f(
int
n){
printf
(
"%d\n"
,n);
n/(1000-n);
f(n+1);
}
还有这样退出递归的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void
yesprint(
int
i);
void
noprint(
int
i);
typedef
void
(*fnPtr)(
int
);
fnPtr dispatch[] = { yesprint, noprint };
void
yesprint(
int
i) {
printf
(
"%d\n"
, i);
dispatch[i / 1000](i + 1);
}
void
noprint(
int
i) {
/* do nothing. */
}
int
main() {
yesprint(1);
}
还有下面这些各种各样的解法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
/* prints number i */
void
print1(
int
i) {
printf
(
"%d\n"
,i);
}
/* prints 10 numbers starting from i */
void
print10(
int
i) {
print1(i);
print1(i+1);
print1(i+2);
print1(i+3);
print1(i+4);
print1(i+5);
print1(i+6);
print1(i+7);
print1(i+8);
print1(i+9);
}
/* prints 100 numbers starting from i */
void
print100(
int
i) {
print10(i);
print10(i+10);
print10(i+20);
print10(i+30);
print10(i+40);
print10(i+50);
print10(i+60);
print10(i+70);
print10(i+80);
print10(i+90);
}
/* prints 1000 numbers starting from i */
void
print1000(
int
i) {
print100(i);
print100(i+100);
print100(i+200);
print100(i+300);
print100(i+400);
print100(i+500);
print100(i+600);
print100(i+700);
print100(i+800);
print100(i+900);
}
int
main() {
print1000(1);
return
0;
}
不过,print用得多了一些。我们可以用宏嘛。
1
2
3
4
5
6
7
8
9
#include<stdio.h>
#define Out(i) printf("%d\n", i++);
#define REP(N) N N N N N N N N N N
#define Out1000(i) REP(REP(REP(Out(i))));
void
main()
{
int
i = 1;
Out1000(i);
}
不过,我们应该使用C++的一些特性,比如:
使用构造函数
1
2
3
4
5
6
7
8
9
10
11
class
Printer
{
public
:
Printer() {
static
unsigned i=1; cout << i++ << endl;; }
};
int
main()
{
Printer p[1000];
}
或是更为NB的Template:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template
<
int
N>
struct
NumberGeneration{
static
void
out(std::ostream& os)
{
NumberGeneration<N-1>::out(os);
os << N << std::endl;
}
};
template
<>
struct
NumberGeneration<1>{
static
void
out(std::ostream& os)
{
os << 1 << std::endl;
}
};
int
main(){
NumberGeneration<1000>::out(std::cout);
}
最后来个BT一点的:
1
2
3
4
void
main(
int
j) {
printf
(
"%d\n"
, j);
(main + (
exit
- main)*(j/1000))(j+1);
}
0 0
- 请把从1到1000的数打印出来,但你不能使用任何的循环语句或是条件语句。
- 【C语言】把从1到1000的数打印出来,但你不能使用任何的循环语句或是条件语句。
- 不使用任何的循环语句或是条件语句把从1到1000的数打印出来
- [转]输出从1到1000的数,不能使用循环和条件语句
- 从1到1000的数打印出来
- 打印 1--1000(不能使用条件运算符. 不能使用循环语句)(转自csdn论坛)
- 计算从1加到100不使用循环和条件语句。
- 不用条件和循环语句打印出1~1000
- 从条件语句和循环语句的条件入手,优化程序
- C#不允许使用循环语句、条件语句,在控制台中打印出1-1000这1000个数
- 不允许使用循环语句、条件语句,在控制台中打印出1-200这200个数
- shell脚本中用到的条件和循环语句
- 条件语句、循环语句中创建的变无法在语句外使用
- 从自然数1到1000中随机抽取900个不重复的数并打印出来
- 【c语言】不能使用循环语句和判断语句,求1-100所有整数的和
- 循环语句的使用
- 循环语句的使用
- 循环语句的使用
- UIImageView 转动动画
- poj 1860 Currency Exchange (最短路变形-spfa)
- C#中使用MAPI发送E-Mail
- TD-LTE学习笔记1
- 嵌入式LINUX网卡设置工作模式之Ethtool
- 请把从1到1000的数打印出来,但你不能使用任何的循环语句或是条件语句。
- POJ-1163
- HttpServletResponse简介
- 安卓手机出现数据丢失怎么办
- 投标方案编写
- MS SQL SERVER2005 DAC链接
- SQL Server中的Char 、varchar 、Nvarchar数据类型
- Android图片缩放总结及比较
- ASIHTTPRequest系列(一):同步和异步请求