按不同精度求圆周率。这个C++程序出了什么问题,题目是PI/4=1-1/3+1/5-1/7...求PI的

代码如下
#include<iostream>
using namespace std;
int MI(int j){
int y=1;
for(int i=1;i<=j+1;j++)
y=y*(-1);
return (y);
}
int main(){
double P1;
float n=1.0;int j=1;int m,y;
cin>>m;
for(j=1;j<=m;j++)
{MI(j);cout<<j;
n+=float(y/(2j-1));
}
cout<<n;
P1=4*(n-1);
cout<<"when m,P1 is"<<P1<<endl;
system("pause");
return 0;

}

第1个回答  2011-10-26
#include<iostream>
using namespace std;
int MI(int j){
int y=1;
for(int i=1;i<=j+1;j++)
y=y*(-1);
return (y);
}
int main()
{
double P1;
float n=1.0;
int j=1;
int m,y;
cin>>m;
for(j=1;j<=m;j++)
{
y=MI(j);
cout<<j;
n+=float(y/(2*j-1));
}
cout<<n;
P1=4*(n-1);
cout<<"when m,P1 is"<<P1<<endl;
system("pause");
return 0;
}追问

你运行了么。我运行了不对头呢

追答

#include
using namespace std;
float MI(int j){//注意返回值
float y=1.0;
for(int i=1;i>m;
for(j=1;j<=m;j++)
{
y=MI(j);
cout<<j;
n+=(y/(2*j-1));//你的原写法是将除的结果整数0变为浮点数0.0
}
cout<<n;
P1=4*(n-1);
cout<<"when m,P1 is"<<P1<<endl;
system("pause");
return 0;
}

本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网