问题及代码:
/*
*copyright (t) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作者:郝昱猛
*完成日期:2016年3月10日
*版本号:v1.0
*问题描述: 写出下面求解任务的程序
(1)用如下公式求π的近似值(计算直到最后一项的绝对值小于10的-5次方)
π/4=1-1/3+1/5-1/7+···
(2)
*输入描述:输入m。
*程序输出:输出运算结果。
*/
(1)
<pre name="code" class="cpp">#include <iostream>
using namespace std;
int main()
{
double Pi=0.0,temp=0.0;
double judge=0.00001,demo=1.0;
long i=0;
for(;;)
{
if((1/demo)<judge)
break;
if(i%2==0)
temp+=1/demo;
else
temp-=1/demo;
demo+=2;
i++;
}
Pi=4.0*temp;
cout<<Pi<<endl;
return 0;
}
</pre><pre name="code" class="cpp">运行结果:<img src="https://img-blog.youkuaiyun.com/20160313214153434?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
(2)
<pre name="code" class="cpp"><pre name="code" class="cpp">#include <iostream>
using namespace std;
int main()
{
long first=1;
long second=1;
long third=0;
cout<<first<<" "<<second<<" ";
for(;;)
{
third=first+second;
if(third>=10000)
break;
cout<<third<<" ";
first=second;
second=third;
}
return 0;
}
运行结果:<img src="https://img-blog.youkuaiyun.com/20160313214200469?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<p>知识点总结:</p><p>循环过程需要弄清变量之间的关系,掌握运行的步骤 。</p><p>学习心得:</p><p>一些程序的循环还不能熟练掌握,在同学的帮助下才完成的。以后还要努力学习。</p>
</pre><pre name="code" class="cpp">