#include <iostream> using namespace std; int main( ) { const int n=9; double f; //用while循环求解 while(i<=n) {b=b*i; t=1.0/b; f=f-sign*t; sign=-sign; i++; } cout<<"(1)用while循环求解,f的值为:"<<f<<endl; return 0; } //用for循环求解 for(i=1;i<=n;i++) {b=b*i; t=1/b; f=f-a*t; a=-a; } cout<<"(2)用for循环求解,f的值为:"<<f<<endl; return 0; } //用do~while循环求 #include <iostream> using namespace std; int main( ) { const int n=9; int a=-1,i=1; double f=0,b=1.0,t; do {b=b*i; t=1/b; f=f-a*t; a=-a; i++;} while(i<=n); cout<<"(2)用for循环求解,f的值为:"<<f<<endl; return 0; } cout<<"(3)用do~while循环求解,f的值为:"<<f<<endl; cout<<"(4)三种循环殊途同归,灵活运用是关键!"<<endl; return 0; }
经验积累:
1.整形,常量之类的没分清
上机感言:编出来真不容易啊 !