Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6599799
  • 博文数量: 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;
}

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

chinaunix网友2008-12-18 11:37:26

罪过罪过,怎么变成比长度了? 我的本意只是为了告诉那位网友,print 缺省参数就是 $_ 罢了。 窃以为比长度没啥意义。

chinaunix网友2008-12-18 09:27:53

shell版 seq 1000

chunchengfh2008-12-17 23:22:19

Python版,也是17个字符... :) print range(1001)

ghostsaint2008-12-17 17:24:24

一时看晕了头, 原来是: Haskell

ghostsaint2008-12-17 17:22:57

呵呵, 谢谢 perl 版的老大 flw 指正, 这样只需要17个字符就可以了, 估计是最短的解决方案: map{print}0..1000 递归所有语言都支持, 一般都会用到递归. if可以用switch或三元操作符来代替. 这个perl版, 用另一种思路, 利用语言提供的功能变相的使用了循环. 另外: FP语言google上不好找啊, 一堆结果不知道是哪个, 可否给个主页的链接?