Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6599510
  • 博文数量: 227
  • 博客积分: 10047
  • 博客等级: 上将
  • 技术积分: 6678
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-11 10:33
个人简介

网上的蜘蛛

文章分类

全部博文(227)

文章存档

2010年(19)

2009年(29)

2008年(179)

分类: Java

2008-12-14 21:53:13

今天在CSDN上面看到一个说是面试试题:
我想出了一个答案:

package print;

public class PrintNum {

   public int print(int n){
     int a = n>1?print(n-1)+1:1;
     System.out.println(a);
     return a;
   }
   public static void main(String[] args){

     PrintNum p = new PrintNum();
     p.print(1000);
   }
}

还有我看到的是用C++的构造函数来实现的:

typedef struct _test{

static int a; _test(){

printf("%d\n",_test::a); a++; }

}Test;

int Test::a = 1;

int main() {

Test tt[1000]; return 0;

}

甚至还有

int a = 1;
int main()
{
if(a < 1001) {printf("%d\t", a++); main();}
return 0;
}

没有测试过...有兴趣可以参考
阅读(12221) | 评论(14) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-12-17 14:36:30

to ghostsaint: print 后面的 $_ 可以不要的。 循环可以用递归来代替,if 可以用模式匹配来代替, 因此这个问题用 FP 语言很好解决。 Haskell: f 0 = putStrLn "0" f n = do f (n-1) putStrLn $ show n Erlang: f (0) -> io:foramt( "0~n" ); f (N) -> f(N-1), io:format( "~p~n", [N] ).

chinaunix网友2008-12-17 14:36:26

to ghostsaint: print 后面的 $_ 可以不要的。 循环可以用递归来代替,if 可以用模式匹配来代替, 因此这个问题用 FP 语言很好解决。 Haskell: f 0 = putStrLn "0" f n = do f (n-1) putStrLn $ show n Erlang: f (0) -> io:foramt( "0~n" ); f (N) -> f(N-1), io:format( "~p~n", [N] ).

ghostsaint2008-12-17 13:16:37

perl version, 只需要19个字符: map{print$_}0..1000 check out in my blog: http://shawnyan.blogspot.com

chinaunix网友2008-12-16 19:40:46

最后一个明显就有一个if