上機考試參考 說明: input 表示執行程式時輸入的畫面, output 表示輸出畫面, 其中 input 中沒有粗體+底線的字為提示字串, 粗體+底線為 user 輸入的資料 C: 30 C: 為提示字串, 30 為 user 輸入的資料 1. Write a program that converts centigrade temperatures to Fahrenheit temperatures. The formula is F = 9/5C + 32 where F is the Fahrenheit temperature and C is the centigrade temperature. (to round off the F, for example, F = 80.3 F = 80, F = 80.6 F = 81) input C: 30 C: 27 output F = 86 F = 81 2. Write a program that can be used as a math tutor for a young student. The teacher can enter two integers n1 and n2, and the student can enter the result of n1+n2. This program should display the correct solution if the student enters a incorrect answer. input output Teacher: n1: 30 n2: 40 Student: 30 + 40 = ? 70 Teacher: n1: 30 n2: 40 Student: 30 + 40 = ? 80 Correct Wrong 30 + 40 = 70 3. Write a program that asks the user to enter a number within the range of 1 through 10. Use a switch statement to display the Roman numeral version of that number. input output Input a number (1~10): 4 Input a number (1~10): 8 IV XIII 4. A company sells a package that retails for $100. Quantity discounts are given according to the following table. Quantity Discount 10-19 20% 20-49 30% 50-99 40% 100+ 50% 5. Write a program that asks for the number of units sold and computes the total cost of the purchase. input output Units: 4 units: 55 Cost: $400 Cost: $3300 6. The following table shows the approximate speed of sound, measured in feet per second, in air, water, and steel. Medium Speed (feet/sec) Air 1,100 Water 4,900 Steel 16,400 7. Write a program that displays a menu allowing the user to select air, water, or steel. After the user has made a selection, the number of feet a sound wave will travel in the selected medium should be enter. The program will then display the amount of time it will take. input output Select a medium: 1. Air 2. Water Select a medium: 4. Air 5. Water 3. Steel Your choice: ? 1 Enter the feet:? 10000 6. Steel Your choice: ? 3 Enter the feet:? 10000 Travel Time: 9.09091 sec Travel Time: 0.609756 sec 8. Write a program that asks the user for the speed of vehicle (in km per hour) and how many hours it has traveled. It should then use a loop to display the total distance traveled at the end of each hour of that time period. input output Speed of the vehicle? 40 Hours that it traveled? 3 Speed of the vehicle? 60 Hours that it traveled? 4 Time: 1 hr/Distance: 40 km Time: 2 hr/Distance: 80 km Time: 3 hr/Distance: 120 km Time: 1 hr/Distance: 60 km Time: 2 hr/Distance: 120 km Time: 3 hr/Distance: 180 km Time: 4 hr/Distance: 240 km 9. Write a program with a loop that user enter a series of integers, followed by -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered. input output enter a series of integers (enter -99 to break): 20 40 10 30 -99 Largest number: 40 Smallest number: 10 10. Write a program to solve the Towers of Hanoi problem. Use a recursive function with four parameters: Move(int n, char peg1, char peg2, char peg3), n: the number of disks to be moved, peg1: the peg on which these disks are initially threaded, peg2: the peg to which this stack of disks is to be moved, peg3: the peg to be used as a temporary holding area. input output enter the number of disks: 3 A C (this means move one disk from peg A to peg C) AB CB AC BA BC AC 11. The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd(int x, int y) that returns the greatest common divisor of x and y. (if x = 0, then gcd(x,y) is y; if y = 0, then gcd(x,y) is x). input output enter the two integers: 8 12 enter the two integers: 24 18 gcd(8,12) = 4 gcd(24,18) = 6 12. Use recursive to write a program that asks the user to enter an integer n, displays the following pattern on the screen. (cannot have any for, while, do-while in program) input output n: 3 n: 4 * ** *** * ** *** **** 13. Use for-statement to write a program that asks the user to enter an integer n, displays the following pattern on the screen. input output n: 3 n: 4 * ** *** * ** *** **** 14. Write a function Distance(int x1, int y1, int x2, int y2) that calculates the distance between points (x1, y1) and (x2, y2). The return values should be the type double. input output enter the two points (x1,y1), (x2,y2): 10 15 20 25 Distance of (10,15) and (20,25): 14.1421 15. Write a function Uppercase() that capitalizes the English letter of the string. input output enter a string: hello world ! HELLO WORLD !