1 / 12

实验 11.14 + 习题课 2

实验 11.14 + 习题课 2. Lab5_Ex5. 编写一个递归函数 power ( base, exponent ) ,当函数被调用时,返回 base exponent 例如, power( 3, 4 ) = 3 * 3 * 3 * 3 。 假设指数 exponent 是一个大于或等于 0 的整数 . 递归步骤使用关系式 : base exponent = base · base exponent - 1 当指数 exponen=1 时,递归终止条件发生,因为此时 base 1 = base 。. Lab5_Ex6. 斐波那契数列

olympe
Download Presentation

实验 11.14 + 习题课 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 实验11.14 +习题课 2

  2. Lab5_Ex5 编写一个递归函数power ( base, exponent ),当函数被调用时,返回 base exponent 例如,power( 3, 4 ) = 3 * 3 * 3 * 3。 假设指数exponent是一个大于或等于0的整数. 递归步骤使用关系式: base exponent = base · base exponent - 1 当指数exponen=1时,递归终止条件发生,因为此时base1 = base。

  3. Lab5_Ex6 斐波那契数列 0, 1, 1, 2, 3, 5, 8, 13, 21, ... 从数值0和1开始,具有每一项斐波那契数列的值是前两项斐波那契数列的值之和的性质。

  4. Lab5_Ex6 double fibonacci( int n ) { double fib0 = 0; double fib1 = 1; double temp; if ( n == 0 ) // special case if n is 0 return 0; for ( int i = 2; i <= n; i++ ) { temp = fib1; // temporarily hold fib(i-1) fib1 = fib0 + fib1; // update fib1 to hold fib(i) fib0 = temp; // update fib0 to hold fib(i-1) } // end for return fib1; } // end function fibonacci 如何利用数组来存储斐波那契数列?

  5. 利用数组来存储斐波那契数列 #include <iostream> #include <iomanip> #include <vector> using namespace std; int main(){ vector<int> ivec; ivec.push_back(0); ivec.push_back(1); int n=2; cin>>n; for (int i=1;i<n-1;i++) { ivec.push_back(ivec[i]+ivec[i-1]); } for(int i=0;i<ivec.size();i++) cout<<ivec[i]<<endl; }

  6. Lab5_Ex7 • (中文)(递归法求最大公约数)整数x和y的最大公约数是能同时被x和y整除的最大整数。编写一个函数gcd(),返回x和y的最大公约数,定义递归如下:假如y=0,gcd( x, y )=x;否则,gcd( x, y )= gcd( y, x % y ),这里的%是求模运算符。[注:对于此算法,x必须大于y。]

  7. Lab5_Hom2 (1)计算机在教育领域的作用越来越大,编写一个程序,帮助小学生学习乘法。用rand函数产生两个一位正整数,然后输入下列问题: How much is 6 times 7? 然后学生输入答案,程序检查学生的答案。 如果正确,则打印(随机选一): Very good! Excellent! Nice work! Keep up the good work! 然后提出另一个乘法问题。 如果不正确,则打印: No. Please try again. Wrong.Try once more. Don‘t give up! No. Keep trying 然后让学生重复回答这个问题,直到答对。 用随机数产生器选择1到4的数,由此选择相应评语。用switch结构发出响应。 统计学生答对和答错的比例(每题仅给一次回答机会)。学生答完10题后,程序计算其答对率。如果比例不到75%,则程序打印"Please ask your instructor for extra help",否则输出“Nice work!”.然后终止。

  8. 编写一个猜数字游戏的程序,程序随机选择一个l到1000的数编写一个猜数字游戏的程序,程序随机选择一个l到1000的数 I have a number between 1 and 1000. Can you guess my number? Please type your first guess. 然后游戏者输人第一个结果。程序响应如下: • Excellent! You guessed the number! Would you like to play again (y or n)? • Too low. Try again. • Too high. Try again. 如果游戏考猜错,则程序进行循环.直到猜对。 这个程序中采用的查找技术称为二分查找。

  9. 汉诺塔 • towers( n, start, end , temp)= • towers( n - 1, start, temp, end ) • move last disk from start to end • 3. towers(n - 1, temp, end, start );

  10. 冒泡法排序 • 将相邻的两个数进行比较,较小的数冒到数组的顶部,较大的数沉到数组的底部。例如: • 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 • 2, 4, 6, 8, 10, 12, 89, 68, 45, 37 • 2, 4, 6, 8, 10, 12, 68, 89, 45, 37 • 2, 4, 6, 8, 10, 12, 68, 45, 89, 37 • 2, 4, 6, 8, 10, 12, 68, 45, 37,89 • 2, 4, 6, 8, 10, 12, 45, 68, 37,89 • 2, 4, 6, 8, 10, 12, 45, 37,68, 89 • 2, 4, 6, 8, 10, 12, 37,45, 68, 89

  11. #include <iostream> #include <iomanip> using name space std; int main(){ const int arraySize = 10; // size of array a int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; int hold; // temporary location used to swap array elements cout << "Data items in original order\n"; for ( int i = 0; i < arraySize; i++ ) cout << setw( 4 ) << a[ i ]; // bubble sort for ( int pass = 0; pass < arraySize - 1; pass++ ) { for ( int j = 0; j < arraySize - 1; j++ ) { if ( a[ j ] > a[ j + 1 ] ) { hold = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } // end if } // end for } // end for cout << "\nData items in ascending order\n"; for ( int k = 0; k < arraySize; k++ ) cout << setw( 4 ) << a[ k ]; cout << endl; return 0; // indicates successful termination } // end main

  12. #include <iostream> #include <iomanip> using name space std; int main(){ const int arraySize = 10; // size of array int a[arraySize ] = { 6, 4, 2, 8, 10, 12, 37, 45, 68, 89 }; int hold; // temporary variable used for swapping bool swapCheck = true; // was a swap made cout << "Data items in original order\n"; for ( int i = 0; i < arraySize ; ++i ) cout << setw( 4 ) << a[ i ]; for ( int pass = 1; pass < arraySize - 1 && swapCheck == true; pass++ ) { swapCheck = false; for (int j = 0; j < arraySize - pass; comp++ ) { if ( a[ j ] > a[ j + 1 ] ) { hold = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; swapCheck = true; // a swap has been made } // end if } // end inner for } // end outer for cout << "\nData items in ascending order\n"; for ( int q = 0; q < arraySize ; q++ ) cout << setw( 4 ) << a[ q ]; return 0; // indicates successful termination } // end main

More Related