代码如下
#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;
}
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;
}