---
# System prepended metadata

title: 期末考練習A

---

# 期末考練習A
## A1-找迴文
```.cpp
#include<iostream>
using namespace std;
int reverseNumber(int number){
	int renum=0;
	while(number!=0){
		renum=renum*10+number%10;
		number/=10;
	}
	return renum;
}
int main(){
	for(int i=0;i<2;i++){
		int n,sum,rec=1;
		cin >> n;
		sum=n+reverseNumber(n);
		while(sum!=reverseNumber(sum)){
			sum=sum+reverseNumber(sum);
			rec++;
		}
		cout << "Total:" << rec << endl;
		cout << "Result:" << sum << endl;
	}
}
```
範例輸入:
265 750
範例輸出:
Total:5
Result:45254
Total:3
Result:6666
## A2-出現次數最多的數
```.cpp
#include<iostream>
using namespace std;
int main(){
	int ans[10]={},n=0,max=0;
	for(int i=0;i<10;i++){
		cin >> n;
		ans[n]++;
	}
	for(int i=0;i<10;i++){
		if(ans[i]>max){
			max=ans[i];
		}
	}
	cout << "Result: ";
	for(int i=0;i<10;i++){
		if(ans[i]==max){
			cout << i << " "; 
		}
	}
}
```
## A3-不重複的一維陣列
```.cpp
#include<iostream>
using namespace std;

bool check(int ans[],int n){
	for(int i=0;i<5;i++){
		if(ans[i]==n){
			cout << "Please re - enter since the added number is repeated:" << endl;
			return true;
		}
	}
}
int main(){
	int ans[5]={},n,c;
	while(ans[4]==0){
		cin >> n;
		if(check(ans,n)==0){
			ans[c]=n;
			c++;	
		}
	}
	for(int i=0;i<5;i++){
		cout << ans[i];
	}
}
```
範例輸入:
7 1 7 6 1 3 6 8
範例輸出:
Please re - enter since the added number is repeated:
Please re - enter since the added number is repeated:
Please re - enter since the added number is repeated:
7 1 6 3 8
## A4-成績的刪減及新增
```.cpp
#include<iostream>
using namespace std;

void Forward(int arr[],int count,int f){
	for(int i=0;i<count;i++){
		if(i==f){
			arr[i]=0;
		}
		else if(i>f){
			arr[i-1]=arr[i];
			arr[i]=0;
		} 
	}
}
void Backward(int arr[],int count,int b,int score){
	for(int i=count-1;i>=b;i--){
		arr[i+1]=arr[i];
		arr[i]=0; 
	}
	arr[b]=score;
}
void Print(int arr[], int count){
	for(int i=0;i<count;i++){
		cout << arr[i] << ' ';
	}
	cout << endl;
}
int main(){
	int arr[30]={},n,f,b,score;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> arr[i];
	}
	cin >> f;
	Forward(arr,n,f);
	Print(arr, n);
	cin >> b >> score;
	Backward(arr,n,b,score);
	Print(arr, n);
}
```
範例輸入:
15
90 92 85 75 74 73 60 66 19 0 66 67 89 63 40
9
9 60
範例輸出:
90 92 85 75 74 73 60 66 19 66 67 89 63 40 0
90 92 85 75 74 73 60 66 19 60 66 67 89 63 40
## A5-函式f(n)=n的所有位數相加，重複f(n)直到結果不變(使用遞迴)
```.cpp
#include<iostream>
using namespace std;

void f(int n){
	int sum=0;
	while(n!=0){
		sum+=n%10;
		n/=10;
	}
	if(sum>9){
		f(sum);
	}
	else{
		cout << sum;
	}
}
int main(){
	int n;
	cin >> n;
	f(n);
}
```
範例輸入:
876543
範例輸出:
6
## A6-大十字
```.cpp
#include<iostream>
using namespace std;

int main(){
	int room[20][20]={},r,c,stdr,stdc;
	cin >> r >> c >> stdr >> stdc;
	for(int i=0;i<r;i++){
		for(int j=0;j<c;j++){
			if(i==stdr||j==stdc){
				room[i][j]=1;
			}
		}
	}
	room[stdr-1][stdc-1]=1;
	room[stdr-1][stdc+1]=1;
	room[stdr+1][stdc-1]=1;
	room[stdr+1][stdc+1]=1;
	room[stdr][stdc]=0;
	for(int i=0;i<r;i++){
		for(int j=0;j<c;j++){
			cout << room[i][j] << " ";
		}
		cout << endl;
	}
}
```
範例輸入1:
5 5 2 2
範例輸出1:
0 0 1 0 0
0 1 1 1 0
1 1 0 1 1
0 1 1 1 0
0 0 1 0 0
## A7-根據大樓剩餘層數，判斷刑期
```.cpp
#include<iostream>
using namespace std;

int main(){
    int Long,width,ammo;
    int place_l,place_w;
    int year=0;
    
    cin>>Long>>width;
    cin>>ammo;
    int set[Long][width]={0};
    
    for(int i=0;i<Long;i++){
        for(int j=0;j<width;j++){
            set[i][j]=101;
        }
    }
    for(int i=1;i<=ammo;i++){
        cin>>place_l>>place_w;
        
        for(int x=place_l-1;x<=place_l+1;x++){
            for(int y=place_w-1;y<=place_w+1;y++){
                if(x>=0&&y>=0&&x<Long&&y<width)
					set[x][y]-=5;
            }   
        }
    }
    for(int i=0;i<Long;i++){
        for(int j=0;j<width;j++){
            if(set[i][j]<101&&set[i][j]>=50){
				year+=87;
			}
			else if(set[i][j]<0){
				year+=6666;
			}
			else if(set[i][j]<50&&set[i][j]>0){
				year+=777;
			}
        }
    }
    cout<<year<<endl;
}
```
## A8-輸出數量為質數的英文字
```.cpp
#include<iostream>
using namespace std;

int main(){
	int n;
	char en;
	cin >> n;
	int enarr[26]={};
	for(int i=1;i<=n;i++){
		cin >> en;
		enarr[(int)en%65]++;
	}
	for(int i=0;i<26;i++){
		int num=0;
		for(int j=1;j<=enarr[i];j++){
			if(enarr[i]%j==0){
				num++;
			}
		}
		if(num==2){
			cout << (char)(i+65) << " ";
		}
	}
}
```
範例輸入:
76
N F Z H E W H Q S L H E P E P U Y H E N Q Z X V B R X A B Z Q N V P A G S H W W L H C K Y R B W H Y G L D D I Q J M W P R J W F I P C P M C G C U N Y S
範例輸出:
A B D F G H I J L M R S U V X Z
# 期末考練習B
## B1-幾A幾B
```.cpp
#include<iostream>
using namespace std;

int n;

void guessNumber(int ans, int num[])
{
	
	for (int i = 0; i < n; i++){
		int a=0,b=0;
		for(int j=1000;j!=0;j/=10){
			for(int k=1000;k!=0;k/=10){
				if(num[i]/k%10==ans/j%10){
					if(j==k){
						a++;
					}
					else{
						b++;
					}
				}
			}
		}
		cout << a << 'A' << b << 'B' << endl;
	}		
}

int main()
{
	int ans, num[100];

	cin >> ans;
	cin >> n;

	for (int i = 0; i < n; i++)
		cin >> num[i];

	guessNumber(ans, num);
}
```
範例輸入：
1536
3
1234
2345
1536
範例輸出：
2A0B
0A2B
4A0B
## B2-取平方數的百位數和十位數直到一樣
```.cpp
#include<iostream>
using namespace std;
void funA(int num){
	int numd=num*num;
	int nn=0;
	for(int i=100;i>=10;i/=10){
		nn+=numd/i%10*i/10;
	}
	if(nn!=num){
		cout << " " << nn;
		funA(nn);
	}
	else{
		cout << nn;
	}
}

int main()
{
	int num1, num2;
	cin >> num1;
	cin >> num2;
	cout << num1;
	funA(num1);
	cout << endl;
	cout << num2;
	funA(num2);
	return 0;

}
```
範例輸入1:
43
38
範例輸出1:
43 84 5 2 0 0
38 44 93 64 9 8 6 3 0 0
## B3-羅馬數字
```.cpp
#include<iostream>
#include<cmath>
using namespace std;

int main(){
	int num1,num2,n;
	cin >> num1 >> num2;
	n=abs(num1-num2);
	for(int i=1000;i!=0;i/=10){
		if(n/i%10!=0){
			switch(i){
				case 1000:
					for(int j=1;j<=n/i%10;j++){
						cout << "M";
					}
					break;
				case 100:
					if(n/i%10==9){
						cout << "CM";
					}
					else if(n/i%10==4){
						cout << "CD";
					}
					else{
						if(n/i%10<5){
							for(int j=1;j<=n/i%10;j++){
								cout << "C";
							}
						}
						if(n/i%10>=5){
							cout << "D";
							for(int j=1;j<=n/i%10-5;j++){
								cout << "C";
							}
						}
					}
					break;
				case 10:
					if(n/i%10==9){
						cout << "XC";
					}
					else if(n/i%10==4){
						cout << "XL";
					}
					else{
						if(n/i%10<5){
							for(int j=1;j<=n/i%10;j++){
								cout << "X";
							}
						}
						if(n/i%10>=5){
							cout << "L";
							for(int j=1;j<=n/i%10-5;j++){
								cout << "X";
							}
						}
					}
					break;
				case 1:
					if(n/i%10==9){
						cout << "IX";
					}
					else if(n/i%10==4){
						cout << "IV";
					}
					else{
						if(n/i%10<5){
							for(int j=1;j<=n/i%10;j++){
								cout << "I";
							}
						}
						if(n/i%10>=5){
							cout << "V";
							for(int j=1;j<=n/i%10-5;j++){
								cout << "I";
							}
						}
					}
					break;
			}
		}
	}
}
```
範例輸入：
2000 2
範例輸出：
MCMXCVIII
## B4-各班成績取中位數，再取一次中位數
```.cpp
#include <iostream>
using namespace std;

template<class T>
void sort(T stu[], int y)
{
    for(int i = 0; i < y-1; i++)
        for(int j = i+1; j < y; j++)
            if(stu[i] > stu[j])
                swap(stu[i], stu[j]);
}

int main()
{
	int cnum,midnum=0;
	float cmid[100]={};
	cin >> cnum;
	for(int i=1;i<=cnum;i++){
		int snum;
		float score[100]={};
		cin >> snum;
		for(int j=0;j<snum;j++){
			cin >> score[j];
		}
		sort(score,snum);
		if(snum%2==0){
			cmid[midnum]=(score[snum/2-1]+score[snum/2])/2;
			midnum++;
		}
		else{
			cmid[midnum]=score[snum/2];
			midnum++;
		}
	}
	sort(cmid,midnum);
	if(midnum%2==0){
		cout << (cmid[midnum/2-1]+cmid[midnum/2])/2;
	}
	else{
		cout << cmid[midnum/2];;
	}
    return 0;
}
```
範例輸入1:
3
4 60 68 61 60
5 70 72 71 68 82
5 60 60 60 60 60
範例輸出1:
60.5
# 期末考練習C
## C1-呼叫指標從特定位置輸出特定數量字元
```.cpp
#include <iostream>
#include <string>
using namespace std;

void print(char *chr, int start, int print_size)
{
    for(int i=start;i<start+print_size;i++){
    	cout << chr[i];
    }
}

int main()
{
    char n[1000]={};
    char *chr=&n[0];
    int st,psize;
    cin >> n;
    cin >> st >> psize;
    print(chr,st,psize);
    return 0;
}
```
範例輸入1:
abcdef
1 4
範例輸出1:
bcde
## C2-透過指標計算總合
```.cpp
#include <iostream>
using namespace std;

int sum(int *arr, int arr_size)
{
    int ans=0;
    for(int i=0;i<arr_size;i++){
    	ans+= *(arr+i);
    }
    return ans;
}

int main()
{
    int arr_size;
    cin >> arr_size;
    int *arr = new int[arr_size];
    for(int i=0;i<arr_size;i++){
        cin >> arr[i];
    }
    cout << sum(arr,arr_size);
}
```
範例輸入:
6
1 3 5 7 9 11
範例輸出:
36
## C3-指標中的質互換
```.cpp
#include <iostream>
using namespace std;

void swap(int* x,int* y){
	int hold;
	hold=*x;
	*x=*y;
	*y=hold;
}

void print(int a, int b, int c) {
	cout << a << "," << b << "," << c << endl;
}
 
int main() {
    int a,b,c;
    cin >> a >> b >> c;
    print(a,b,c);
    swap(a,b);
    print(a,b,c);
    swap(a,c);
    print(a,b,c);
}
```
範例輸入:
1 2 3
範例輸出:
1,2,3
2,1,3
3,1,2