---
tags: Programming Course (C/C++)
---
# 考古題參考ans
[回講義](https://hackmd.io/@CSnoob/ComputerProgramming2)
[TOC]
## 助教課練習題
### P1 溫度轉換
``` CPP=
#include <iostream>
#include <math.h> // 使用第八行round()函式前要include的函示庫
using namespace std;
int main(){
int c;
while(cin>>c){
float f = (float(c)*9)/5+32;
cout<<round(f)<<endl; // round 用來四捨五入到個位數
}
return 0;
}
```
### p2 數學加法檢查
```CPP=
#include <iostream>
using namespace std;
int main(){
int n1, n2, ans;
while(cin>>n1>>n2>>ans){
if(n1+n2 == ans){
cout<<"Correct"<<endl;
}else{
cout<<"Wrong"<<endl<<n1<<"+"<<n2<<"="<<n1+n2<<endl;
}
}
return 0;
}
```
### p3 輸出羅馬數字
```Cpp=
#include <iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
switch (n){
case 1: cout<<"I";break;
case 2: cout<<"II";break;
case 3: cout<<"III";break;
case 4: cout<<"IV";break;
case 5: cout<<"V";break;
case 6: cout<<"VI";break;
case 7: cout<<"VII";break;
case 8: cout<<"VIII";break;
case 9: cout<<"IX";break;
case 10: cout<<"X";break;
default: break;
}
cout<<endl;
}
return 0;
}
```
### p4p5 打折
```Cpp=
#include <iostream>
using namespace std;
int main(){
int units;
while(cin>>units){
int cost=units;
switch(units){
case 0 ... 9:
cost *= 100;
break;
case 10 ... 19:
cost *= 80;
break;
case 20 ... 49:
cost *= 70;
break;
case 50 ... 99:
cost *= 60;
break;
default:
cost *= 50;
break;
}
cout<<cost<<endl;
}
return 0;
}
```
### p6p7 計算travel time
```Cpp=
#include <iostream>
using namespace std;
int main(){
int choice, feet;
while(cin>>choice>>feet){
float sec;
switch(choice){
case 1:
sec=float(feet)/1100;
break;
case 2:
sec=float(feet)/4900;
break;
case 3:
sec=float(feet)/16400;
break;
default:
break;
}
cout<<sec<<"sec"<<endl;
}
return 0;
}
```
### p8 輸出汽車跑了幾公里
```Cpp=
#inlcude <iostream>
using namespace std;
int main(){
int s, h;
while(cin>>s>>h){
int time = 1;
while(h--){
printf("Time: %d hr/Distance: %d km\n", time++, time*s);
}
}
return 0;
}
```
### p9 輸出最小最大
``` CPP=
// vector<int> name -> 宣告叫做name的vector
// (vector 就是個容器 跟陣列很像
// 陣列宣告要告訴電腦陣列大小,但vector不用)
// name.clear() -> 清空vector
// name.push_back(number) -> 把number 放到 vector 裡面
// name.size() -> vector目前的大小
// name[i] -> 第i筆資料
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<int> data;
data.clear();
int input;
while(true){
scanf("%d", &input);
if(input == -99){
break;
}
data.push_back(input);
}
sort(data.begin(),data.end());
printf("%d ", data[0]);
printf("%d ", data.back());
return 0;
}
```
### p10 河內塔
```CPP=
#include <iostream>
using namespace std;
void Move(int n, char from, char to, char tmp){
if (n == 0) {
return;
}
Move(n - 1, from, tmp, to);
cout << from<< " -> " << to << endl;
Move(n - 1, tmp, to, from);
}
int main()
{
int n;
while(cin>>n){
Move(n, 'A', 'C', 'B');
}
return 0;
}
```
### p11 recursive GCD
``` CPP=
#include <iostream>
using namespace std;
int gcd(int a, int b){
if(b==0){
return a;
}else{
return gcd(b,a%b);
}
}
int main(){
int i, j;
while(cin>>i>>j){
cout<<gcd(i,j)<<endl;
}
return 0;
}
```
### p12 reucrsive 印星星
```CPP=
#include <iostream>
using namespace std;
void recur(int sp, int st, int ln, int n){ // sp = space, ln = line, n = num
if(ln>n){
return ;
}
if(sp>0){
cout<<" ";
recur(sp-1, st, ln, n);
}else if(st!=0){
cout<<"*";
recur(0, st-1, ln, n);
}else{
cout<<endl;
recur(n-ln-1,ln+1, ln+1, n);
}
}
int main(){
int num;
while(cin>>num){
recur(num-1, 1, 1, num);
}
return 0;
}
```
### p13 for迴圈印星星
```Cpp=
#include <iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
for(int i=1; i<=n; i++){
for(int j=0; j<n-i; j++){
cout<<" ";
}
for(int j=0; j<i; j++){
cout<<"*";
}
cout<<endl;
}
}
return 0;
}
```
### p14 計算距離
``` CPP=
#include <iostream>
#include <math.h>
using namespace std;
double distance(int x1, int y1, int x2, int y2){
return sqrt(pow(abs(x1 - x2), 2)+pow(abs(y1 - y2), 2));
}
int main(){
int x1, y1, x2, y2;
while(cin>>x1>>y1>>x2>>y2){
cout<<distance(x1, y1, x2, y2)<<endl;
}
return 0;
}
```
### p15 大小寫轉換
V1 在副函示輸出
``` Cpp=
#include <iostream>
using namespace std;
void uppercase(string s){
for(int i=0; i<s.length(); i++){
if(s[i] >= 97 && s[i] <= 122){
cout<<char(int(s[i])-32);
}else{
cout<<s[i];
}
}
cout<<endl;
}
int main(){
string str;
while(true){
getline(cin, str);
uppercase(str);
}
return 0;
}
```
V2 傳遞指標,在主函式輸出
```CPP=
#include <iostream>
#include <string.h>
using namespace std;
void uppercase(char *s){
if(*s >= 97 && *s <= 122){
*s = (int(*s)-32);
}
}
int main(){
string str;
while(true){
getline(cin, str);
for(int i=0; i<str.length(); i++){
uppercase(&str[i]);
}
cout<<str;
cout<<endl;
}
return 0;
}
```
## 110程式大會考
### p12
```CPP=
#include <iostream>
#include <math.h>
using namespace std;
bool isprime(int a, int check){
if(check > sqrt(a)) return 1;
else if(a % check == 0) return 0;
else return isprime(a, check + 1);
}
void Two2N_Prime(int start, int end){
if(start > end){
return;
}else{
int ch = 2;
if(isprime(start, ch) == true){
cout<<start<<" ";
}
Two2N_Prime(start+1, end);
}
}
int main(){
int n;
while(cin>>n){
Two2N_Prime(2, n);
cout<<endl;
}
return 0;
}
```
### p2
```CPP=
#include <iostream>
using namespace std;
void swap(int *a, int *b){
int tmp = *a; //把指標a指向的變數的值存放在tmp這個變數裡面
*a = *b; //把指標a指向的變數的值,改成指標b指向的變數的值
*b = tmp; //把指標b指向的變數的值,改成tmp的值
return;
}
int main(){
int num1, num2;
scanf("a=%d\n",&num1);
scanf("b=%d",&num2);
swap(&num1,&num2);
printf("a=%d\nb=%d\n", num1, num2);
return 0;
}
```
