C++ Programming for Teens
Final Examination on July 22, 2016
Summer 2016
Department of Computer Science & Information Engineering
National Taiwan University
Problem 1 (20 points) 撰寫一個程式加總輸入的正整數的每一位數,並將其結
果輸出在螢幕上。例如:
1 Enter a positive integer ? 12345
2 Sum = 15
Problem 2 (20 points) 撰寫一個函數計算輸入值的正平方根,並且執行方式如
下:
1 >> mySqrt 10
2 sqrt ( 1 0 ) = 3. 1 6 2 2 7 7 6 6 0 1
3 >> mySqrt 0. 1
4 sqrt ( 1 0 ) = 0. 3 1 6 2 2 7 7 6 6 0
注意,程式不得使用sqrt之類的數學函數。提示:利用Bisection method。
1 #include <iostream>
2
3 d ouble mySqrt ( d ouble x ) ;
4
5 i n t main ( i n t argc , ch a r ∗∗ argv ) {
6 /∗ your work ∗/
7 r e t u r n 1 ;
8 }
9
10 d ouble mySqrt ( d ouble x ) {
11 /∗ your work ∗/
12 }
Problem 3 (20 points)
(1) 撰寫一個程式計算x
n的結果,其中x為任意實數,n為任意整數。
(2) 此程式的時間複雜度為? (在螢幕上最後一行輸出O(g(n))即可,其中g(n)是
你認為你的程式可以被歸類於此的簡單函數。)
1
1 #include <iostream>
2
3 i n t myPow ( d ouble , i n t ) ;
4
5 i n t main ( ) {
6 std : : cout << myPow ( 2 , −10) ;
7 r e t u r n 1 ;
8 }
9
10 i n t myPow ( d ouble x , i n t n ) {
11 /∗ your work ∗/
12 }
Problem 4 (20 points) 撰寫一個程式展示bubble sort的演算法。第一,先產生
一個亂數陣列,假設長度為10且成員皆為整數。第二,透過指標(pointer)將一維
陣列(array)從main function傳給函數bubbleSort。最後,在螢幕上顯示排序前後
的結果。
1 #include <iostream>
2
3 /∗ your work ∗/
4
5 i n t main ( ) {
6 /∗ your work ∗/
7 r e t u r n 1 ;
8 }
9
10 /∗ your work ∗/
Problem 5 (20 points) 撰寫一個程式隨機產生一組四個相異正整數,皆介
於0, 1, 2, . . . , 9之間。執行方法如下:
1 >> randomSeq
2 5 7 1 2
3 >> randomSeq
4 0 4 7 3
Problem 6 (20 points) 填寫問卷者,可獲得額外的20分。連結:https://
docs.google.com/forms/d/e/1FAIpQLSfUJWQJw-oRUhNR4l-iwTWhdgJnj6IgpYAKg_
hiHIGMaCbdxA/viewform
SUBMISSION
1. Pack source codes into a zip or rar file with your full name.
2
2. Do not attach the executable files.
3. Send the zip/rar file to [email protected].
4. If you have any suggestion for improving this class, feel free to leave your
feedback in the e-mail.
Congratulations!!!
“To start, you don’t have to be good;
to be good, you have to start.”
– Slogan of NTU Toastmasters |