17.求圆周率PI: PI/4 = 1- 1/3 + 1/5 -1/7 +.....
#include<iostream> using namespace std; int YuanZhou(int); int main() { int n; double temp,sum=0; cout<<"please input an number: "<<endl; cin>>n; for(int i=1;i<=n;i++) { if(i%2==0) { temp=(-1.0/YuanZhou(i));//两个int型相除得到的结果会自动变成int型 }else { temp=(1.0/YuanZhou(i));//隐式类型转换 } sum+=temp; } cout<<4*sum<<endl; return 0; } int YuanZhou(int index) { if(index<0) { return -1; }else { return (2*index-1); } }