好久没有来了,大家100个倒数相加做的怎么样了。。。
程序代码:
第一种 #include < iostream > #include < cstdlib > using namespace std; int main ( ) { const int N = 100; double sum = 0.0; int x = 1; do sum += 1.0 / x++; while ( x <= N ); cout << " The sum of the first" << N << " reciprocals is " << sum; 第二种: //迭代意指反复执行一条或多条语句,直至某个条件为真、 #include < iostream > #include < cstdlib > using namespace std; int main ( ) { const int N = 100; double sum = 0.0; int x = 1; repeat: sum += 1.0 / x++; if ( x < N ) goto repeat; cout << "The sum of the first " << N << " reciprocals is " << sum; system ( "pause" ); } 第三种: #include < iostream > #include < cstdlib > using namespace std; int main ( ) { const int N = 100; double sum = 0.0; int x = 1; while ( x <= N ) sum += 1.0 / x++; cout << "The sum of the first " << N << " reciprocals is " << sum; system ( "pause" ); return 0; } 第四种: #include < iostream > #include < cstdlib > using namespace std; int main ( ) { const int N = 100; double sum = 0.0; for ( int x = 1; x <=N; x++ ) sum += 1.0 / x; cout << "The sum of the first " << N << " reciprocal is " << sum << endl; system ( "pause" ); return 0; } 再发个漂亮的图案玩玩: #include < iostream > #include < cstdlib > using namespace std; int main ( ) { const int N = 10; for ( int i = 0; i < N; i++ ) { for ( int j = 0; j < 2 * N; j++ ) if ( j < N -i || j > N + i ) cout << " "; else cout << "*"; cout <<"\n"; } system ( "pause" ); return 0; } system ( "pause" ); return 0; }嘿嘿