| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 520 人关注过本帖
标题:请求牛人相助!!!!
只看楼主 加入收藏
azhan
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-4-23
收藏
 问题点数:0 回复次数:0 
请求牛人相助!!!!
由于本人初学,很多不懂。哪位高人能看一下下面这道题,最好附上源码,谢谢!
1.Pi can be calculated using the following sum, which is an approximation to Pi/4 :
PiSum = 1 - 1/3 + 1/5 - 1/7 + ... + (-1)^n / (2*n + 1)
1) Write two methods that return PiSum for a specified value of n, one method
using recursion and the other using iteration to do the sum.
Hint: instead of calculating (-1)^n, you can check whether the nth term is
negative by checking whether n%2 (i.e. n modulo 2) is 1.
Assume that the methods are always called with a non-negative value of n, so you
don't need to worry about dealing with invalid parameter values.
2) Show the additional code you would add to the above methods to throw an
exception if n is less than zero (you don't need to rewrite the whole method). The
exception handler should generate an error message saying that the number of terms
in the sum must not be negative.
Write a code fragment (i.e. it doesn't need to be a complete method) that invokes
one of the methods for calculating Pi and prints out the result if the method returns
correctly, or else catches an exception and prints out the error message returned.
3) When calculating Pi using the given formula, suppose that instead of passing a
parameter specifying the number of terms n in the sum, we instead want to pass a
parameter that specifies a precision value. The sum should terminate at the first value
n for which the absolute value of the nth term in the sum, i.e. 1/(2*n+1) is less than
the specified precision. Show how this can be implemented using a do loop.
搜索更多相关主题的帖子: 请求 
2008-04-23 20:46
快速回复:请求牛人相助!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018601 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved