搞笑一帖
2006-03-12 14:02 Jeff 阅读(258) 评论(0) 编辑 收藏 举报
今天在CSDN上灌水,看到一个搞笑帖….
楼主:求1 2 3 4 5 6......n求和。java编程谢谢啦
一楼:
public int sum(int x)
![](/Images/OutliningIndicators/None.gif)
{
int result=0;
for (int i=0;i<=x;i++)
{
result+=i;
}
return result;
}
![](/Images/OutliningIndicators/None.gif)
这年头什么简单问题都在问啊。。这个都不会。楼主能找到工作吗?
二楼:
用一个简单的递归不就行了...
public static int sum(int n) {
![](/Images/OutliningIndicators/InBlock.gif)
if(n<1) return 0;
![](/Images/OutliningIndicators/InBlock.gif)
return n + sum(n-1);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
楼主:求1 2 3 4 5 6......n求和。java编程谢谢啦
一楼:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
二楼:
用一个简单的递归不就行了...
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
三楼:
晕,答案一个比一个离谱
n*(n+1)/2呀