---
# System prepended metadata

title: 課程實作題
tags: [ZeroJudge & Leetcode, 課程內容, 實作]

---

課程實作題
===

**目錄**
[TOC]

# 計算機技術
## 2023.09.18
### 1
**題目**
![111646](https://hackmd.io/_uploads/rJ980db2a.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	char name[11], department;
	int test1, test2;
	printf("Please input your name....");
	scanf("%s%*c", name);
	printf("Please select your department....(a:資工 b:電機 c:電子)");
	scanf("%c%*c", &department);
	printf("Please input your score on test 1....");
	scanf("%d%*c", &test1);
	printf("Please input your score on test 2....");
	scanf("%d%*c", &test2);
	printf("|-------------------------------------------|\n");
	printf("|   Name   |Department|Test 1|Test 2|Average|\n");
	printf("|----------+----------+------+------+-------|\n");
	printf("|%-10s|%-10c|%6d|%6d|%7.2f|\n", name, department, test1, test2, (test1+test2)/2.00);
	printf("|-------------------------------------------|\n");
	system("pause"); 
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <iomanip>  //控制輸出格式
#include <fstream>  //開通文件流
#define _USE_MATH_DEFINES   //定義數學常數
#include <cmath>    //數學函式庫
#include <ctime>    //時間函式庫
#include <cstdlib>  //C語言中的基本函式庫(繼承)
#include <cctype>   //辨別資料型態
#include <string>
#include <cstring>  //string資料型態的設定
using namespace std;

int main()
{
    int test1, test2;
    float avg;
    char department;
    string name;
    cout<<"Please input your name....";
    cin>>name;
    cout<<"Please select your department....<a:資工 b:電機 c:電子>";
    cin>>department;
    cout<<"Please input your score on test 1....";
    cin>>test1;
    cout<<"Please input your score on test 2....";
    cin>>test2;
    avg=(float)(test1+test2)/2.0;
    cout<<"|-------------------------------------------|"<<endl;
    cout<<"|   Name   |Department|Test 1|Test 2|Average|"<<endl;
    cout<<"|----------+----------+------+------+-------|"<<endl;
    cout<<"|"<<setiosflags(ios::left)<<setw(10)<<setfill(' ')<<name<<"|"<<setw(10)<<department<<"|"<<setw(6)<<setfill(' ')<<test1<<"|"<<setw(6)<<setfill(' ')<<test2<<"|";
    printf("%7.2f|\n",avg);
    cout<<"|-------------------------------------------|"<<endl;
    return 0;
}
```
#### Python
```Python==

```
**執行結果**

### 2
**題目**
![111647](https://hackmd.io/_uploads/S1nw0uZha.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h> 

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int num, a, b;
	printf("Please input a positive integer:");
	scanf("%d",&num);
	if(num<=0){
		printf("%d is not a positive integer!!", num);
	}
	else if(num<10){
		printf("%d is a small integer.", num);
	}
	else if(num<100){
		a = num/10;
		b = num%10;
		printf("%d 的十位數為%d\n%d 的個位數為%d", num, a, num, b);
	}
	else{
		printf("%d is a large integer.\n", num);
		if(num%5 == 0){
			printf("%d is divisible by 5.", num);
		}
		else if(num%5 == 1){
			a = num+5;
			printf("%d+5=%d", num, a);
		}
		else if(num%5 == 2){
			a = num-5;
			printf("%d-5=%d", num, a);
		}
		else if(num%5 == 3){
			a = num*5;
			printf("%d*5=%d", num, a);
		}
		if(num%5 == 4){
			float c;
			c = num/5.00;
			printf("%d/5=%-.2f", num, c);
		}
	}
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int num;
    cout<<"Please input a positive integer: ";
    cin>>num;
    if(num<=0){
        cout<<num<<" is  not a positive number!!\n";
    }
    else if(num<10){
        cout<<num<<" is a small integer.\n";
    }
    else if(num<100){
        cout<<num<<" 的十位數為 "<<num/10<<endl;
        cout<<num<<" 的個位數為 "<<num%10<<endl;
    }
    else{
        cout<<num<<" is a large integer.\n";
        if(num%5==0){
            cout<<num<<" is divisible by 5.\n";
        }
        else if(num%5==1){
            cout<<num<<"+5="<<num+5<<endl;
        }
        else if(num%5==2){
            cout<<num<<"-5="<<num-5<<endl;
        }
        else if(num%5==3){
            cout<<num<<"*5="<<num*5<<endl;
        }
        else{
            float avg=(float)(num+5)/5.0;
            cout<<num<<"/5=";
            printf("%.2f\n",avg);
        }
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**

## 2023.09.25
### 1
**題目**
![111648](https://hackmd.io/_uploads/ByBuAOZha.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int num_1, num_2, sum, i;
	char choice = 'Y';
	while( num_1>num_2||choice=='Y' || choice=='y'){
		printf("Number 1:");
		scanf("%d",&num_1);
		printf("Number 2:");
		scanf("%d",&num_2);
		if (num_1>num_2){
			printf("Error!!Number 1 > Number 2!!\n");
			continue;
		}
		sum = 0;
		for( i=num_1 ; i<=num_2 ; i++ ){
			sum += i;
		}
		printf("Sum = %d\n",sum);
		printf("Again(Y/N)??");
		scanf("%*c%c%*c",&choice);
	}
	system("pause");
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <iomanip>  //控制輸出格式
#include <fstream>  //開通文件流
#define _USE_MATH_DEFINES   //定義數學常數
#include <cmath>    //數學函式庫
#include <ctime>    //時間函式庫
#include <cstdlib>  //C語言中的基本函式庫(繼承)
#include <cctype>   //辨別資料型態
#include <string>
#include <cstring>  //string資料型態的設定
using namespace std;

int main()
{
    int n1, n2, sum;
    char check;

    do{
        cout<<"Number 1: ";
        cin>>n1;
        cout<<"Number 2: ";
        cin>>n2;
        sum=0;
        if(n1<=n2){
            for(int i=n1;i<=n2;i++)sum+=i;
            cout<<sum<<endl;
        }
        else{
            cout<<"Error!! Number 1 >Number 2!!\n";
            continue;
        }
        cout<<"Again(Y/N)??";
        cin>>check;
    }while(check=='Y'||check=='y'||n1>n2);
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/S1YTb1WhT.png)

### 2
**題目**
![111649](https://hackmd.io/_uploads/HJyFAubnT.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int num, i, a=0, b=1, sum;
	printf("Please input a number:");
	scanf("%d",&num);
	printf("%d %d ",a,b);
	for( i=0 ; i<num-2 ; i++ ){
		sum = a+b;
		printf("%d ",sum);
		a = b;
		b = sum;
	}
	system("pause");
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int num;
    cout<<"Please input a number: ";
    cin>>num;
    int a=1, b=0, temp, ans[num]={0,1};
    for(int i=1;i<num;i++){
        temp=a+b;
        ans[i+1]=temp;
        b=a;
        a=temp;
    }
    for(int i=0;i<num;i++){
        cout<<ans[i];
        if((i+1)==num)cout<<endl;
        else cout<<" ";
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/rJisNJ-3p.png)

### 3
**題目**
![111650](https://hackmd.io/_uploads/BJ6YRub26.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int num, i, j, k, l, sum=0, total=0;
	printf("N = ");
	scanf("%d",&num);
	for( i=1 ; i<=num ; i++){ 
		for( j=i+1 ; j<=num ; j++){
			for( k=j+1 ; k<=num ; k++){
				for( l=k+1 ; l<=num ; l++){
					sum = i+j+k+l;
					if(sum==num){
						total ++;
					}
				}
			}
		}
	}
	printf("Total is %d",total);
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int N, total=0, i, j, k, l;
    cout<<"Please input a number: ";
    cin>>N;
    for(i=1;4*i<N;i++){
        for(j=i+1;3*j<N-i;j++){
            for(k=j+1;2*k<N-i-j;k++){
                for(l=k+1;l<N-i-j-k;l++){
                    //cout<<"("<<i<<", "<<j<<", "<<k<<", "<<l<<") ";
                }
                if((i+j+k+l)==N)total++;
            }
        }
    }
    cout<<"Total is  "<<total<<endl;
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/ByWXh1-nT.png)
![image](https://hackmd.io/_uploads/SyK8nJZn6.png)

### 4
**題目**
![111651](https://hackmd.io/_uploads/BJGj0d-ha.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char argv[]){
	int num, i, j, k, l;
	printf("Please input a number:");
	scanf("%d",&num);
	for( i=1 ; i<=num ; i++){
		for( j=0 ; j<num-i ; j++){
			printf(" ");
		}
		for( k=1 ; k<=i ; k++){
			printf("%d",k%10);
		}
		for( l=i-1 ; l>0 ; l-- ){
			printf("%d",l%10);
		}
		printf("\n");
	}
	return 0;
	
} 
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int num;
    cout<<"Please input a number: ";
    cin>>num;
    for(int i=1;i<=num;i++){
        for(int j=num-i;j>0;j--) cout<<" ";
        for(int j=1;j<=i;j++)cout<<j%10;
        for(int j=i-1;j>0;j--)cout<<j%10;
        cout<<endl;
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/B11PRkb2p.png)

## 2023.10.02
### 1
**題目**
![111652](https://hackmd.io/_uploads/BkZhCubha.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int num, i, j, total=1;
	printf("Please input a number:");
	scanf("%d",&num);
	printf("%d ",2);
	for( i=2 ; i<=num-1 ; i++ ){
		for( j=2 ; j*j<=i ; j++ ){
			if( i%j==0 ){
				break;
			}
		}
		if(i%j!=0){
				printf("%d ",i);
				total++;	
			}
	}
	printf("\nTotal is %d",total);
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int num, total=0;
    cout<<"Please input a number: ";
    cin>>num;
    int ans[num]={};
    for(int i=2;i<=num;i++){
        int check=1;
        for(int j=2;j*j<=i;j++){
            if(i%j==0){
                check=0;
                break;
            }
        }
        if(check){
            ans[total]=i;
            total++;
        }
    }
    for(int i=0;i<total;i++){
        cout<<ans[i];
        if(i==total-1)cout<<endl;
        else cout<<", ";
    }
    cout<<"Total is "<<total<<endl;
    return 0;
}
```
#### Python
```Python==

```
**執行解果**
![image](https://hackmd.io/_uploads/HJgUmx-2p.png)

### 2
**題目**
![111653](https://hackmd.io/_uploads/SkhhAdW26.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int a, b, c, d;
	printf("Please input two integers:");
	scanf("%d %d",&a,&b);
	do{
		if(a>b){
			d = a%b;
		} 
		else {
			c = b;
			b = a;
			a = c;
			d = a%b;
		}
		a = b;
		b = d;
	}while(d!=0);
	printf("GCD is %d",a);
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int a, b, temp;
    cout<<"Please input teo numbers: ";
    cin>>a>>b;
    while(a!=0){
        if(b>a){
            temp=a;
            a=b;
            b=temp;
        }
        a%=b;
    }
    cout<<"GCD is "<<b<<endl;
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/BkPFrgbnp.png)

### 3
**題目**
![111654](https://hackmd.io/_uploads/HJn6CuWhp.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int sum, legs,chickens,rabbits;
	
	do{
		printf("總數量+總腳數:");
		scanf("%d+%d",&sum,&legs);
		rabbits = (legs/2)-sum;
		chickens = sum - rabbits;
		if( legs%2==1||rabbits<0||chickens<0){
			printf("無解\n");
			continue;
		}
		else if(sum==0&&legs==0){
			break;
		}
		else{
			printf("雞 %d 隻 兔子 %d 隻\n",chickens,rabbits);
		}
	}while(sum!=0||legs!=0);
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int chicken, rabbit, A, B;
    cout<<"總數量+總腳數: ";
    scanf("%d+%d",&A,&B);
    while(A!=0||B!=0){
        rabbit=(B-2*A)/2.0;
        chicken=2*A-B/2;
        if((chicken+rabbit)==A&&B%2==0){
            cout<<"雞 "<<chicken<<" 隻\t兔子 "<<rabbit<<"隻\n";
        }
        else cout<<"無解\n";
        cout<<"總數量+總腳數: ";
        scanf("%d+%d",&A,&B);
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/SJ_9alW3p.png)

### 4
**題目**
![111655](https://hackmd.io/_uploads/BJBAAOZnT.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int num, i, j, k, time=0;
	printf("Please input a number:");
	scanf("%d",&num);
	for( i=10 ; i<num ; i++ ){
		if(i>10&&i<100&&i==(i/10+i%10)*(i/10+i%10)){
			printf("%d,",i);
			time++;
		}
		else if(i>1000&&i<10000&&i==(i/100+i%100)*(i/100+i%100)){
			printf("%d,",i);
			time++;
		}
		else if(i>100000&&i<1000000&&i==(i/1000+i%1000)*(i/1000+i%1000)){
			printf("%d,",i);
			time++;
		}
	}
	printf("\nTotal is %d",time);
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int num, temp, total=0;
    cout<<"Please input a number: ";
    cin>>num;
    for(int i=1;i<=num;i++){
        if(i>=10&&i<100){
            temp=i/10+i%10;
            if(temp*temp==i){
                cout<<i<<", ";
                total++;
            }
        }
        else if(i>=1000&&i<10000){
            temp=i/100+i%100;
            if(temp*temp==i){
                cout<<i<<", ";
                total++;
            }
        }
        else if(i>=100000&&i<1000000){
            temp=i/1000+i%1000;
            if(temp*temp==i){
                cout<<i<<", ";
                total++;
            }
        }
    }
    cout<<"\nTotal is "<<total<<endl;
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/SkkbEbb3p.png)

## 2023.10.16
### 1
**題目**
![112127](https://hackmd.io/_uploads/HyObQvm26.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int length, width, num, A1, A2, i;
	printf("正方形長*寬:");
	scanf("%d*%d",&length,&width);
	A1 = length*width;
	while(length!=0&&width!=0){
		if(length>=width){
			length = length%width;
			width = width-length;
		}
		else{
			width = width%length;
			length = length-width;
		}
	}
	for( i=1 ; i<=width ; i++){
		if(width%i==0){
			A2 = i*i;
			num = A1/A2;
			printf("正方形邊長:%d 個數:%d\n",i,num);
		}
	}
	return 0;
}

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int m,n;
    cout<<"長方形長*寬: ";
    scanf("%d*%d",&m,&n);
    if(n>m){
        int temp=m;
        m=n;
        n=temp;
    }
    for(int i=1;i<=n;i++){
        if(n%i==0&&m%i==0){
            cout<<"長方形邊長為: "<<i<<" 個數: "<<(m/i)*(n/i)<<endl;
        }
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/Bkp7gRz2a.png)

### 2
**題目**
![112128](https://hackmd.io/_uploads/Bkof7vQhp.jpg)

**解答**
#### C
**第一種方法**
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int num, i, j, k, a1=2, a2, time, total=0;
	printf("Please input a number:");
	scanf("%d",&num);
	for( i=2 ; i<=num ; i++ ){
		for( j=2 ; j*j<=i ; j++ ){
			if(i%j==0){
				break;
			}
		}
		if(j*j<=i){
			continue;
		}
		a2 = i ;
		if(a2-a1>=5){
			time = a2-a1-4;
			for(k = 0 ; k<time ; k++ ){
				printf("(%d,%d,%d,%d)",a1+k+1,a1+k+2,a1+k+3,a1+k+4);
				total++;
			}
		}
		a1 = a2;
	}
	printf("\nTotal is %d",total);
	return 0;
}
```
**第二種方法**
```C==
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int N, i, j, k, check, total=0;
    scanf("%d",&N);
    i=2;
    int ans[N-1];
    while(i<=N){
    	check=1;
		j=2;
		while(j*j<=i&&check){
			if(i%j==0) check=0;
			j++;
		}
		ans[i-2]=!check;
//		printf("%d ",ans[i-2]);
		i++;
	}
	i=0;
	while(i<N-3){
		if(ans[i]&&ans[i+1]&&ans[i+2]&&ans[i+3]){
			printf("(%d,%d,%d,%d)",i+2,i+3,i+4,i+5);
			total++;
		}
		i++;
	}
	printf("\nTotal is %d\n",total);
    return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int n, i=3;
    cout<<"Please input a number: ";
    cin>>n;
    int ans[n-2]={1};
    while(i<=n){
        for(int j=2;j<i;j++){
            if(i%j==0){
                ans[i-2]=1;
                cout<<"("<<i<<","<<i-2<<","<<ans[i-2]<<")";
                break;
            }
        }
        i++;
    }
    cout<<endl;
    i=0;
    int total=0;
    while(i<n-5){
        if(ans[i]&&ans[i+1]&&ans[i+2]&&ans[i+3]){
            cout<<"("<<i+2<<","<<i+3<<","<<i+4<<","<<i+5<<")";
            total++;
        }
        i++;
    }
    cout<<"\nTotal is "<<total<<endl;
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/HkyvgJXhp.png)

### 3
**題目**
![112129](https://hackmd.io/_uploads/S17XXwQh6.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int i, num, x, sum=1,total=0, j;
	printf("please input a number:");
	scanf("%d",&num);
	for( x=2 ; x<=num ; x++){
			for( i=2 ; i*i<=x ; i++ ){
				if(x%i==0){
					sum +=i;
					j = x/i;
					if(i!=j){
						sum+=j;
					}
				}
		}
		if(sum==x){
			printf("%d,",x);
			total++;
		}
		sum = 1;
	}
	printf("\nThere are %d perfect numbers!!",total);
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int n, i=2, total=0;
    cout<<"Please input a number: ";
    cin>>n;
    while(i<=n){
        int sum=1;
        for(int j=2;j<i;j++){
            if(i%j==0){
                sum+=j;
            }
        }
        if(sum==i){
            cout<<i<<", ";
            total++;
        }
        i++;
    }
    if(total==1){
        cout<<"\nThere are "<<total<<" perfect number!!\n";
    }
    else if(total>0){
        cout<<"\nThere are "<<total<<" perfect numbers!!\n";
    }
    else{
        cout<<"\nThere are no perfect numbers!!\n";
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/Sy09mGXna.png)

### 4
**題目**
![112130](https://hackmd.io/_uploads/HypXQvm3a.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int num[3], ans[3], result, total=0,A,B, i, j, a, k, b;
	do{
		srand(time(NULL));
		ans[0]=rand()%10;
		ans[1]=rand()%10;
		ans[2]=rand()%10;
		ans[3]=rand()%10;
	}while(ans[0]==ans[1]||ans[0]==ans[2]||ans[0]==ans[3]||ans[1]==ans[2]||ans[1]==ans[3]||ans[2]==ans[3]);
	printf("%d%d%d%d\n",ans[0],ans[1],ans[2],ans[3]);
	do{
		A=0;
		B=0;
		printf("Please input four digits(0~9):");
		scanf("%d",&b);
		for( k=3 ; k>=0 ; k--){
			num[k]=b%10;
			b/=10;
		}
		printf("%d%d%d%d\n",num[0],num[1],num[2],num[3]);
		for( i=0 ; i<4 ; i++){
			for( j=0 ; j<4 ; j++ ){
				if(i==j&&ans[i]==num[j]){
					A++;
				}
				else if(ans[i]==num[j]){
					B++;
				}
			}
		}
		printf("The result is %dA %dB\n",A,B);
		total++;
	}while(A!=4);
	printf("You got it!! %d times!!",total);
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <fstream>  //開通文件流
#define _USE_MATH_DEFINES   //定義數學常數
#include <cmath>    //數學函式庫
#include <ctime>    //時間函式庫
#include <cstdlib>  //C語言中的基本函式庫(繼承)
#include <cctype>   //辨別資料型態
#include <cstring>  //string資料型態的設定
using namespace std;

int main()
{
    int prize[4], guess[4], A, B, total=0;
    char temp[4];
    srand(time(NULL));
    do{
        prize[0]=rand()%10;
        prize[1]=rand()%10;
        prize[2]=rand()%10;
        prize[3]=rand()%10;
    }while(prize[0]==prize[1]||prize[0]==prize[2]||prize[0]==prize[3]||prize[1]==prize[2]||prize[1]==prize[3]||prize[2]==prize[3]);
    cout<<prize[0]<<prize[1]<<prize[2]<<prize[3]<<endl;
    do{
        total++;
        A=0;
        B=0;
        cout<<"Please input four digits (0~9): ";
        cin>>temp;
        for(int i=0 ; i<4 ; i++){
            guess[i]=(int)temp[i]-48;
        }
        for(int i=0 ; i<4 ; i++){
            for(int j=0 ; j<4 ; j++){
                if(guess[i]==prize[j]&&i==j)A++;
                else if(guess[i]==prize[j])B++;
            }
        }
        if(A==4){
            if(total==1){
                cout<<"You got it!! "<<total<<" time!!\n";
            }
            else{
                cout<<"You got it!! "<<total<<" times!!\n";
            }
            break;
        }
        cout<<A<<"A"<<B<<"B\n";
    }while(A!=4);
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/Hk0PBz7np.png)

## 2023.10.23
### 1
**題目**
![112131](https://hackmd.io/_uploads/BJuEmDQ2a.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int N, i, j, k, sum, a;
	printf("Please input the size of matrix:");
	scanf("%d",&N);
	int matrix1[N][N], matrix2[N][N];
	printf("Please input matrix1:\n");
	for( i=0 ; i<N ; i++ ){
		for( j=0 ; j<N ; j++ ){
			scanf("%d",&matrix1[i][j]);
		}	
	}
	printf("Please input matrix2:\n");
	for( i=0 ; i<N ; i++ ){
		for( j=0 ; j<N ; j++ ){
			scanf("%d",&matrix2[i][j]);
		}
	}
	printf("matrix1*matrix2 = \n");
	for( i=0 ; i<N ; i++ ){
		
		for( j=0 ; j<N ; j++ ){
			sum = 0;
			for( k=0 ; k<N ; k++ ){
				a = matrix1[i][k]*matrix2[k][j];
				sum+=a;
			}
			printf("%d ",sum);	
		}
			printf("\n");
	}
	return 0;
}

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int n, i, j, k;
    cout<<"Please input the size if matrix: ";
    cin>>n;
    int matrix1[n][n], matrix2[n][n], ans[n][n];
    cout<<"Please input matrix1:\n";
    i=0;
    while(i<n){
        j=0;
        while(j<n){
            cin>>matrix1[i][j];
            j++;
        }
        i++;
    }
    cout<<"Please input matrix2:\n";
    i=0;
    while(i<n){
        j=0;
        while(j<n){
            cin>>matrix2[i][j];
            j++;
        }
        i++;
    }
    cout<<"matrix1 * matrix2:\n";
    i=0;
    int sum;
    while(i<n){
        j=0;
        while(j<n){
            sum=0;
            for(k=0;k<n;k++){
                sum+=matrix1[i][k]*matrix2[k][j];
            }
            cout<<sum<<" ";
            j++;
        }
        cout<<endl;
        i++;
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/SJlY5G73T.png)

### 2
**題目**
![112132](https://hackmd.io/_uploads/HyzSmvQ2T.jpg)

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int num1,num2, i, j, code[26]={10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33}, Y=0,num[11],k,num3[11];
	char a[11];
	char first;
	printf("請輸入身份證字號:");
	//scanf("%c%d",&first,&num2);
	scanf("%s",&a);
	k=a[0]-'A';
	//k=num[0];
	for( i=1 ; i<10 ; i++ ){
		//a[i] = num2%10;
		//num2/=10;
		//printf("%d",a[i]);
		num3[i+1]=(int)a[i]-48;
	}
	///num1 = (int)first-65;
	//printf("\n%d\n",code[num1]); 
	num3[0] = code[k]/10;
	num3[1] = (int)code[k]%10;
	//printf("%d %d\n",a[0],a[1]);
	Y = num3[0];
	for( i=1 ; i<=9 ; i++ ){
		Y+=num3[i]*(10-i);
	}
	printf("%d\n",Y);
	Y =(10-(Y% 10))%10;
	//printf("%d\n",Y);
	//Y = 10-Y; 
	//printf("%d\n",Y);
	if(Y==num3[10]){
		printf("身份證字號正確!!\n");
	}
	else{
		printf("身份證字號錯誤!!\n");
	}
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    int Y, turn[11], i, j, k;
    char alpha[26]={'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','X','Y','W','Z','I','O'}, input[10];
    cout<<"請輸入身份證字號:";
    cin>>input;
    i=0;
    while(i<10){
        if(i==0){
            for(j=0;j<26;j++){
                if(input[0]==alpha[j]){
                    turn[0]=(j+10)/10;
                    turn[1]=(j+10)%10;
                    break;
                }
            }
        }
        else{
            turn[i+1]=(int)input[i]-48;
        }
        i++;
    }
    Y=turn[0];
    i=1;
    while(i<10){
        Y+=(turn[i]*(10-i));
        i++;
    }
    if((10-Y%10)==turn[10]||(Y%10)==0){
        cout<<"身分證字號正確!!\n";
    }
    else{
        cout<<"身分證字號錯誤!!\n";
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/H1NeGP72T.png)

## 2023.10.30
### 1
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
using namespace std;

int main()
{
    int length, i, j, k, n, check=0, total=0;
    string input;
    cout<<"請輸入一個字串: ";
    cin>>input;
    cout<<"最長迴文為: ";
    length=input.length();
    n=length;
    while(n>=1){
        i=0;
        while(i<length-n+1){
            if(input[i]==input[i+n-1]){
                j=1;
                check=1;
                while(2*j<=n){
                    if(input[i+j]!=input[i+n-1-j]){
                        check=0;
                        break;
                    }
                    j++;
                }
                if(check){
                    j=0;
                    while(j<n){
                        cout<<input[i+j];
                        j++;
                    }
                    cout<<", ";
                    total++;
                }
            }
            i++;
        }
        if(total){
            break;
        }
        n--;
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**

### 2
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
using namespace std;

int main()
{
    int length1, length2, i, j, k, total=0;
    string str1, str2;
    cout<<"Please input the string 1: ";
    cin>>str1;
    cout<<"Please input the string 2: ";
    cin>>str2;
    length1=str1.length();
    length2=str2.length();
    if(length1>=length2){
        i=0;
        while(i<length1-length2+1){
            if(str1[i]==str2[0]){
                j=1;
                while(j<length2){
                    if(str1[i+j]!=str2[j])
                        break;
                    j++;
                }
                if(j==length2){
                    total++;
                }
            }
            i++;
        }
    }
    if(total){
        if(total==1){
            cout<<"Repeat 1 time\n";
        }
        else{
            cout<<"Repeat "<<total<<" times\n";
        }
    }
    else{
        cout<<"String 2 is not a substring of string 1\n";
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/SkPOzYVna.png)

### 3
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <ctime>
using namespace std;

int main()
{
    int sum=0, N, a, b;
    srand(unsigned(time(NULL)));
    N=rand()%31+10;
    cout<<"N="<<N<<endl;
    do{
        if((N-sum)%4==0){
            a=3;
        }
        else if((N-sum)%4==1){
            a=rand()%3+1;
        }
        else if((N-sum)%4==2){
            a=1;
        }
        else{
            a=2;
        }
        cout<<"Sum="<<sum<<", Computer add "<<a<<endl;
        sum+=a;
        if(sum>=N){
            cout<<"Computer lose!!\n";
            break;
        }
        cout<<"Sum="<<sum<<", You add ";
        cin>>a;
        sum+=a;
        if(sum>=N){
            cout<<"You lose!!\n";
            break;
        }
    }while(sum<N);
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/H1o6YKV2p.png)

## 2023.11.06
### 1
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
using namespace std;

int main()
{
    int i, j, k, length, key[3][3], sum, n;
    string str1, str2;
    cout<<"Please input a 3x3 key matrix (9 integers):";    //6 24 1 13 16 10 20 17 15
    i=0;
    while(i<3){
        j=0;
        while(j<3){
            cin>>key[i][j];
            j++;
        }
        i++;
    }
    cout<<"Please input a string: ";    //SUBSTITUTION
    cin>>str1;
    length=str1.length();
    int a[3][length/3], nea[3][length/3];
    j=0;
    n=0;
    while(j<length/3){
        i=0;
        while(i<3){
            a[i][j]=(int)str1[n++]-65;
            //cout<<a[i][j]<<" ";
            i++;
        }
        //cout<<endl;
        j++;
    }
    //cout<<endl;
    j=0;
    while(j<length/3){
        i=0;
        while(i<3){
            k=0;
            sum=0;
            while(k<3){
                sum+=key[i][k]*a[k][j];
                k++;
            }
            nea[i][j]=sum%26+65;
            //cout<<nea[i][j]<<" ";
            i++;
        }
        //cout<<endl;
        j++;
    }
    cout<<"The cipher is ";
    j=0;
    n=0;
    while(j<length/3){
        i=0;
        while(i<3){
            str2[n++]=(char)(nea[i][j]);
            cout<<str2[n-1];
            i++;
        }
        j++;
    }
    cout<<endl;
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/rJmd8i4n6.png)

### 2
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int i, j, k;
	unsigned int num, ans[20]={}, sum=0, total=0, length;
	string input;
	cout<<"Please input a decimal number: ";
	cin>>num;
	do{
		cout<<num<<" ";
		ans[total]=num%2;
		total++;
		num/=2;
	}while(num!=0);
	i=total-1;
	while(i>=0){
		cout<<ans[i];
		i--;
	}
	cout<<"\nPlease input a binary number: ";
	cin>>input;
	length=input.length();
	i=length-1;
	while(i>=0){
		k=(unsigned int)input[i]-48;
		j=1;
		while(j<length-i){
			k*=2;
			j++;
		}
		cout<<k<<"("<<j-1<<")"<<" ";
		sum+=k;
		i--;
	}
	cout<<endl<<sum<<endl;
	return 0;
}
```
#### Python
```Python==

```
**執行結果**

### 3
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
using namespace std;

int main()
{
    int n, i, j, k, total=0, week, y;
    cout<<"Please input the centery: ";
    cin>>n;
    y=(n-1)*100;
    i=1;
    while(i<=100){
        week=((y+i)+(int)((y+i-1)/4)-(int)((y+i-1)/400))%7;
        j=1;
        while(j<=12){
            if(j==1||j==3||j==5||j==7||j==8||j==10||j==12){
                week=(week+31)%7;
            }
            else if(j==4||j==6||j==9||j==11){
                week=(week+30)%7;
            }
            else{
                if(((y+i)%4==0&&(y+i)%100!=0)||((y+i)%400==0)){
                    week=(week+29)%7;
                }
                else{
                    week=(week+28)%7;
                }
            }
            if(week==5){
                total++;
            }
            j++;
        }
        i++;
    }
    cout<<total<<" times!!\n";
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/rJVwuKDhT.png)

## 2023.11.13
### 1
**題目**

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

static total=0;

void towers(int n, char from, char aux, char to){
	if(n==1){
		printf("ring %d:%c => %c\n",n,from,to);
		total++;
		
	}
	else{
		towers(n-1,from,to,aux);
		//towers(1,from,aux,to);
		printf("ring %d:%c => %c\n",n,from,to);
		towers(n-1,aux,from,to);
		
	}
	
}

int main(int argc, char *argv[]) {
	int num, ans;
	printf("請輸入A柱中的環個數:");
	scanf("%d",&num);
	towers(num,'A','B','C');
	printf("共需 %d 步驟\n",(2<<(num-1))-1);
	return 0;
}
```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

void towers(int n, char from, char aux, char to);

static int total=0;

int main()
{
    int n;
    cout<<"請輸入 A 柱中的環個數: ";
    cin>>n;
    towers(n,'A','B','C');
    cout<<"共需 "<<total<<" 個步驟\n";
    return 0;
}

void towers(int n, char from, char aux, char to){
    if(n==1){
        cout<<"ring 1:"<<from<<" => "<<to<<endl;
        total++;
    }
    else{
        towers(n-1,from,to,aux);
        cout<<"ring "<<n<<":"<<from<<" => "<<to<<endl;
        total++;
        towers(n-1,aux,from,to);
    }
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/Bk9WwQ_h6.png)

### 2
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int fact(int n);

int main(int argc, char** argv) {
	int length, i, j, k, ans=0, sum=0;
	string input;
	cout<<"Please input a string: ";
	cin>>input;
	length=input.length();
	int a[length][2];
	ans=fact(length);
	i=0;
	while(i<length){
		a[i][0]=0;
		a[i][1]=0;
		int check=1;
		j=0;
		while(j<sum){
			if(a[j][0]==(int)input[i]){
				a[j][1]++;
				check=0;
				break;
			}
			j++;
		}
		if(check){
			a[sum][0]=(int)input[i];
			a[sum][1]=1;
			sum++;
		}
		i++;
	}
	i=0;
	while(i<sum){
		if(a[i][1]>1){
			ans/=fact(a[i][1]);
		}
//		cout<<a[i][1]<<" ";
		i++;
	}
	cout<<"The number of permutations is "<<ans<<endl;
	return 0;
}

int fact(int n){
	if(n==1){
		return 1;
	}
	else{
		return n*fact(n-1);
	}
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/BJUWwJMp6.png)
![image](https://hackmd.io/_uploads/ryk7P1GaT.png)

### 3
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
using namespace std;

int main()
{
    unsigned int y1, y2, m1, m2, d1, d2, total=0, i=0;
    printf("請輸入西元開始日期:(yyyy/mm/dd)為...");
    scanf("%d/%d/%d",&y1,&m1,&d1);
    printf("請輸入西元結束日期:(yyyy/mm/dd)為...");
    scanf("%d/%d/%d",&y2,&m2,&d2);
    unsigned int range1=(y1/400)*97+((y1%400)/100)*24+y1%100/4;
    unsigned int range2=(y2/400)*97+((y2%400)/100)*24+y2%100/4;
    total=range2-range1;
    if(((y1%4==0&&y1%100!=0)||y1%400==0)&&m1<=2) total++;
    if(((y2%4==0&&y2%100!=0)||y2%400==0)&&(m2<=2&&d2<29)) total--;
    cout<<"此期間的 2 月 29 日共有 "<<total<<"次\n";
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/r195gZq3p.png)

## 2023.11.20
### 1
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

void sort1(int *arr, int n);

int main()
{
    int num, i;
    cout<<"請輸入整數的個數: ";
    cin>>num;
    int arr[num];
    i=0;
    while(i<num){
        cout<<"請輸入第 "<<i+1<<" 個數: ";
        cin>>arr[i];
        i++;
    }
    sort1(arr,num);
    cout<<"排序結果: ";
    i=0;
    while(i<num){
        cout<<arr[i]<<" ";
        i++;
    }
    cout<<endl;
    return 0;
}

void sort1(int *arr, int n){
    int i, j, k, temp;
    i=0;
    while(i<n-1){
        j=n-2;
        while(j>=i){
           if(*(arr+j)>*(arr+j+1)){
                temp=*(arr+j);
                *(arr+j)=*(arr+j+1);
                *(arr+j+1)=temp;
           }
           j--;
        }
        i++;
    }
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/r1Ouptv3p.png)

### 2
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
using namespace std;

int ISBN10(string code);
int ISBN13(string code);

int main()
{
    int length, check=0;
    string code;
    do{
        cout<<"Please input an ISBN code: ";
        cin>>code;
        length=code.length();
        if(length==10){
            check=ISBN10(code);
        }
        else if(length==13){
            check=ISBN13(code);
        }
        else{
            cout<<"Error!\n";
            break;
        }
        if(check){
            cout<<"The ISBN code is correct!!\n";
        }
        else{
            cout<<"The ISBN code is wrong!!\n";
        }
    }while(length==10||length==13);
    return 0;
}

int ISBN10(string code){
    int sum=0, num[10]={}, i=0;
    while(i<10){
        if(i==9&&code[i]=='X') num[9]=10;
        else{
            num[i]=(int)code[i]-48;
        }
        sum+=num[i]*(10-i);
        i++;
    }
    if(sum%11==0){
        return 1;
    }
    else{
        return 0;
    }
}

int ISBN13(string code){
    int sum=0, i=0, num[13];
    while(i<13){
        num[i]=(int)code[i]-48;
        if(i!=12){
            if(i%2==0){
                sum+=num[i];
            }
            else{
                sum+=num[i]*3;
            }
        }
        else{
            if((sum%10!=0&&sum%10==num[12])||sum%10==0){
                return 1;
            }
            else{
                return 0;
            }
        }
        i++;
    }
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/SyZ5Wmu2T.png)

## 2023.11.27
### 1
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
using namespace std;

int gcd(int n1, int n2);
void sort_fraction(int (*ptr)[2], int n);

int main()
{
    int num, total=0, i, j, k, check;
    cout<<"Please input a number: ";
    cin>>num;
    i=num;
    while(i>0){
        j=1;
        while(j<i){
            check=0;
            //cout<<"\n("<<i<<","<<j<<")";
            check=gcd(j,i);
            //cout<<check;
            if(check){
                total++;
            }
            j++;
        }
        i--;
    }
    //cout<<"\n"<<total<<endl;
    int a[total][2];
    k=0;
    i=num;
    while(i>0){
        j=1;
        while(j<i){
            check=0;
            check=gcd(i,j);
            if(check){
                a[k][0]=j;
                a[k][1]=i;
                //cout<<"\n("<<a[k][1]<<","<<a[k][0]<<")";
                k++;
            }
            j++;
        }
        i--;
    }
    sort_fraction(a,total);
    return 0;
}

int gcd(int n1, int n2){
    int temp;
    do{
        if(n1<n2){
            temp=n1;
            n1=n2;
            n2=temp;
        }
        n1%=n2;
        //cout<<n1<<" "<<n2<<"|";
    }while(n1!=0);
    if(n2==1){
        return 1;
    }
    else{
        return 0;
    }
}

void sort_fraction(int (*ptr)[2], int n){
    int i=0, j, k, temp;
    double a, b;
 //   for(i=0;i<n;i++){
 //       printf("(%d/%d)",ptr[i][0],ptr[i][1]);
 //   }
    while(i<n){
        j=n-2;
        while(j>=i){
            a=(double)ptr[j][0]/ ptr[j][1];
            b=(double)ptr[j+1][0]/ ptr[j+1][1];
            //printf("%f,%f\n",a,b);
            if(a>b){
                temp=ptr[j][0];
                ptr[j][0]=ptr[j+1][0];
                ptr[j+1][0]=temp;

                temp=ptr[j][1];
                ptr[j][1]=ptr[j+1][1];
                ptr[j+1][1]=temp;
            }
            j--;
        }
        i++;
    }
    cout<<"0, ";
    i=0;
    while(i<n){
        cout<<ptr[i][0]<<"/"<<ptr[i][1]<<", ";
        i++;
    }
    cout<<"1 (total numbers: "<<n+2<<" )\n";
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/BJsJannhT.png)

### 2
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
using namespace std;

int gcd(int n1, int n2);
void sort_fraction(int (*ptr)[2], int n);

int main()
{
    int ans[26][2]={{},{}}, i, j, k, length1, length2, num, check=1;
    char str1[51], str2[51];
    printf("Please input string #1: "); //O, Draconian devil!
    scanf("%[^\n]",str1);
    printf("Please input string #2: "); //Leonardo da Vinci
    scanf("%*c%[^\n]",str2);
    length1=strlen(str1);
    length2=strlen(str2);
    i=0;
    while(i<length1){
        if(isupper(str1[i])){
            ans[(int)str1[i]-65][0]++;
        }
        else if(islower(str1[i])){
            ans[(int)str1[i]-97][0]++;
        }
        i++;
    }
    i=0;
    while(i<length2){
        if(isupper(str2[i])){
            ans[(int)str2[i]-65][1]++;
        }
        else if(islower(str2[i])){
            ans[(int)str2[i]-97][1]++;
        }
        i++;
    }
    check=1;
    i=0;
    while(i<26){
        if(ans[i][0]!=ans[i][1]){
            check=0;
            break;
        }
        i++;
    }
    if(check){
        printf("Yes\n");
    }
    else{
        printf("No\n");
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/Sk1q2hh3T.png)

### 3
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int num, i, j, k, l, ans[5]={0,0,0,0,INT_MAX};
	cout<<"請輸入乘客總人數 : ";
	cin>>num;
//	cout<<i<<" "<<j<<" "<<k<<" "<<l<<endl;
	i=0;
	while(i<(num/4+1)){
		j=0;
		while(j<(num/7+1)){
			k=0;
			while(k<(num/13+1)){
				l=0;
				while(l<(num/16+1)){
					if((i*2500+j*3800+k*6400+l*7500)<ans[4]&&(4*i+7*j+13*k+16*l)>=num){
//						cout<<"("<<i<<","<<j<<","<<k<<","<<l<<","<<i*2500+j*3800+k*6400+l*7500<<")";
						ans[0]=i;
						ans[1]=j;
						ans[2]=k;
						ans[3]=l;
						ans[4]=i*2500+j*3800+k*6400+l*7500;
						break;
					}
					else if(4*i+7*j+13*k+16*l>num){
						break;
					}
					l++;
				}
				k++; 
			}
			j++;
		}
		i++;
	}
//	cout<<endl<<i<<" "<<j<<" "<<k<<" "<<l<<endl;
	cout<<"最少需要 "<<ans[4]<<" 元\n";
	cout<<"四門房車 "<<ans[0]<<"台\n";
	cout<<"商務休旅車 "<<ans[1]<<"台\n";
	cout<<"小型巴士 "<<ans[2]<<"台\n";
	cout<<"大型巴士 "<<ans[3]<<"台\n";
	return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/r1xR0Rb6T.png)

![image](https://hackmd.io/_uploads/BJmVRA-T6.png)

## 2023.12.04
### 1
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
#include <climits>  //引用極限值
#include <fstream>  //引入檔案輸入輸出流
using namespace std;

int main()
{
    int length, i, j, k, ans[5]={};
    string word;
    ifstream in("t1.txt");
    ofstream out("t2.txt");
    getline(in,word);
    length=word.length();
    i=0;
    while(i<length){
        switch(word[i]){
            case 'A':
            case 'a':{
                ans[0]++;
                break;
            }
            case 'E':
            case 'e':{
                ans[1]++;
                break;
            }
            case 'I':
            case 'i':{
                ans[2]++;
                break;
            }
            case 'O':
            case 'o':{
                ans[3]++;
                break;
            }
            case 'U':
            case 'u':{
                ans[4]++;
                break;
            }
        }
        i++;
    }
    out<<"A\tE\tI\tO\tU\n";
    i=0;
    while(i<5){
        out<<ans[i]<<"\t";
        i++;
    }
    out<<endl;
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
* t1.txt
```
Leonardo da Vinci (April 15, 1452 – May 2, 1519) was an Italian Renaissance polymath: painter, sculptor, architect, musician, mathematician, engineer, inventor, anatomist, geologist, cartographer, botanist, and writer. His genius, perhaps more than that of any other figure, epitomized the Renaissance humanist ideal. Leonardo has often been described as the archetype of the Renaissance Man, a man of "unquenchable curiosity" and "feverishly inventive imagination". He is widely considered to be one of the greatest painters of all time and perhaps the most diversely talented person ever to have lived. According to art historian Helen Gardner, the scope and depth of his interests were without precedent and "his mind and personality seem to us superhuman, the man himself mysterious and remote". Marco Rosci states that while there is much speculation about Leonardo, his vision of the world is essentially logical rather than mysterious, and that the empirical methods he employed were unusual for his time.


```
* t2.txt
```
A	E	I	O	U
76	106	69	53	20	
```
### 2
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <cstring>
#include <fstream>  //引入檔案輸入輸出流
using namespace std;

int main()
{
    ifstream in("t3.txt");
    ofstream out("t4.txt");
    int total=0, i, j, k, time[100]={}, length;
    string word, table[100];
    while(in>>word){
        length=word.length();
        if(word[length-1]=='.'||word[length-1]==','){
            word.resize(length-1);
        }
        i=0;
        while(i<total){
            if(word.compare(table[i])==0){
                time[i]++;
                break;
            }
            i++;
        }
        if(i==total){
            table[total]=word;
            time[total++]=1;
        }
    }
    i=0;
    while(i<total){
        out<<table[i]<<": "<<time[i]<<endl;
        i++;
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
* t3.txt
```
MACHINE LANGUAGES AND ASSEMBLY LANGUAGES ARE LOW LEVEL LANGUAGES. C LANGUAGES, C++ LANGUAGES, AND JAVA LANGUAGES ARE HIGH LEVEL LANGUAGES. LOW LEVEL LANGUAGES ARE CLOSER TO THE HARDWARE THAN ARE HIGH LEVEL LANGUAGES. LOW LEVEL LANGUAGES CAN BE CONVERTED TO MACHINE CODE WITHOUT USING A COMPILER OR INTERPRETER, AND THE RESULTING CODE RUNS DIRECTLY ON THE PROCESSOR. LOW LEVEL LANGUAGES ARE SIMPLE, BUT ARE DIFFICULT TO USE.
```
* t4.txt
```
MACHINE: 2
LANGUAGES: 11
AND: 3
ASSEMBLY: 1
ARE: 6
LOW: 4
LEVEL: 6
C: 1
C++: 1
JAVA: 1
HIGH: 2
CLOSER: 1
TO: 3
THE: 3
HARDWARE: 1
THAN: 1
CAN: 1
BE: 1
CONVERTED: 1
CODE: 2
WITHOUT: 1
USING: 1
A: 1
COMPILER: 1
OR: 1
INTERPRETER: 1
RESULTING: 1
RUNS: 1
DIRECTLY: 1
ON: 1
PROCESSOR: 1
SIMPLE: 1
BUT: 1
DIFFICULT: 1
USE: 1
```
### 3
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
using namespace std;

int main()
{
    unsigned int n, i, j, k, temp;
    cout<<"Please input a number n (1<n<1000000000): ";
    cin>>n;
    cout<<n<<" = ";
    temp=n;
    i=2;
    while(i<=temp){
        int check=1;
        j=2;
        while(j*j<=i){
            if(i%j==0){
                check=0;
                break;
            }
            j++;
        }
        if(check){
            while(temp%i==0){
                if(temp/i==1){
                    cout<<i<<endl;
                }
                else{
                    cout<<i<<" * ";
                }
                temp/=i;
            }
        }
        //cout<<"( "<<temp<<","<<i<<" ) ";
        i++;
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/ryBQfkAha.png)

## 2023.12.11
### 1
**題目**

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int search( FILE *din,char context[2000][20]);

int main(int argc, char *argv[]) {
	int sum=0, i, j, k, l, atotal=0, btotal=0, count=0, check=1, ans[2000][3]={}, ltotal=0;	
	//sum:A句在B文出現的次數, atotal:A總文字, btotal:B總文字, count:A重複的單字, check:acontext[i]是否在B文出現, ans[2000][3]:起始、末端、長度, ltotal:總共出現的重複句 
	float num;
	//num:相似度 
	char acontext[2000][20], bcontext[2000][20];
	//A文單字全、B文單字全、 
	FILE *da = fopen("t10.txt","r");
	FILE *db = fopen("t11.txt","r");
	if(da!=NULL&&db!=NULL){
		btotal = search(db,bcontext);
		atotal = search(da,acontext);
		for( i=0 ; i<atotal ; i++ ){
			check=0;
			sum=0;
			for( j=0 ; j<btotal ; j++ ){
				if(strcmp(acontext[i],bcontext[j])==0){
					k=0;
					while(strcmp(acontext[i+k],bcontext[j+k])==0){
						k++;
					}
					if(k>=7){
						if(sum==0){
							check=1;
							ans[ltotal][0]=i;
							ans[ltotal][1]=i+k-1;
							ans[ltotal][2]=k;
							ltotal++;
							j=j+k-1;
						}
						else if(k>ans[ltotal][2]){
							sum++;
							if(ans[ltotal][0]==i){
								ans[ltotal-1][1]=i+k-1;
								ans[ltotal-1][2]=k;
							}
							else if(ans[ltotal][1]==j){
								ans[ltotal-1][1]=i+k-1;
								ans[ltotal-1][2]=k;
							}
							else{
								ans[ltotal][0]=i;
								ans[ltotal][1]=i+k-1;
								ans[ltotal][2]=k;
							}
							j=j+k-1;
						}
					}
				}			
			}
			if(check){
				i = ans[ltotal-1][1];
			}
		}
	}
	for( i=0 ; i<ltotal ; i++ ){
		count+=ans[i][2];
	}
	num = (float)count/atotal;
	for( i=0 ; i<ltotal ; i++ ){
		for( j=ans[i][0] ; j<=ans[i][1] ; j++ ){
			printf("%s ",acontext[j]);
		}
		printf("\n");
	}
	printf("%d/%d = %-.2f%c\n",count,atotal,num*100,'%');
	fclose(da);
	fclose(db);
	return 0;
}

int search(FILE *din, char context[2000][20]){
	int length, i=0, j, k, total=0;
	char word[20];
	while(fscanf(din,"%s",context[i])!=EOF){
		length = strlen(context[i]);
		if(context[i][length-1]==' '||context[i][length-1]==','||context[i][length-1]=='.'){
			context[i][length-1]='\0';
		}
		for( j=0 ; j<length ; j++ ){
			if(isalpha(context[i][j])==1){
				k = (int)context[i][j]+32;
				context[i][j] = (char)k;
			}
		}
		i++;
	}
	return i;
}
```
#### C++
```C++==
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <iomanip>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	ifstream ain ("t10.txt");
	ifstream bin ("t11.txt");
	int i, j, k, repeat=0, atotal=0, btotal=0, length, ans[2000][3]={{},{}}, sum=0;
	double avg;
	string temp, atable[2000], btable[2000];
	while(ain>>atable[atotal]){
		length=atable[atotal].length();
		if(isupper(atable[atotal][0])){
			atable[atotal][0]=tolower(atable[atotal][0]);
		}
		if(atable[atotal][length-1]==','||atable[atotal][length-1]=='.'){
			atable[atotal].resize(length-1);
		}
//		cout<<atable[atotal]<<" ";
		atotal++;
	}
//	cout<<"\n"<<atotal<<endl;
	while(bin>>btable[btotal]){
		length=btable[btotal].length();
		if(isupper(btable[btotal][0])){
			btable[btotal][0]=tolower(btable[btotal][0]);
		}
		if(btable[btotal][length-1]==','||btable[btotal][length-1]=='.'){
			btable[btotal].resize(length-1);
		}
//		cout<<btable[btotal]<<" ";
		btotal++;
	}
//	cout<<"\n"<<btotal<<endl;
	i=0;
	while(i<atotal){
		j=0;
		while(j<btotal){
			if(atable[i]==btable[j]){
				k=0;
				do{
					k++;
				}while(atable[i+k]==btable[j+k]);
				if(k>=7&&k>=ans[sum][2]){
					ans[sum][0]=i;
					ans[sum][1]=i+k-1;
					ans[sum][2]=k;
				}
			}
			j++;
		}
		if(ans[sum][2]){
			i+=ans[sum][2];
			sum++;
			
		}
		else{
			i++;
		}
	}
	i=0;
	while(i<sum){
		repeat+=ans[i][2];
		i++;
	}
	avg=(double)repeat/atotal;
	cout<<setiosflags(ios::fixed);
	cout<<repeat<<" / "<<atotal<<" = "<<setprecision(2)<<avg*100<<"%"<<endl;
	return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/ry3Co-ChT.png)

### 2
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <fstream>
#include <cstring>
#include <cassert>
#include <cctype>
using namespace std;

int main()
{
    ifstream in("t5.txt");
    ofstream out("t6.txt");
    int total[100]={}, i, j, k, length, sum=0;
    string word[100], temp;
    while(!in.eof()){
        in>>temp;
        length=temp.length();
        if(temp[0]<=91&&temp[0]>=65){
            temp[0]=tolower(temp[0]);
        }
        if(temp[length-1]=='.'||temp[length-1]==','){
            temp.resize(length-1);
        }
        i=0;
        while(i<sum){
            if((temp.compare(word[i]))==0){
                total[i]++;
                break;
            }
            i++;
        }
        if(sum==0||i==sum){
            word[sum]=temp;
            total[sum]=1;
            sum++;
        }
    }
    i=0;
    while(i<sum){
        j=sum-2;
        while(j>=i){
            if(word[j]>word[j+1]){
                temp=word[j];
                word[j]=word[j+1];
                word[j+1]=temp;
                k=total[j];
                total[j]=total[j+1];
                total[j+1]=k;
            }
            j--;
        }
        int check=1;
        j=0;
        while(j<sum-1){
            if(word[j]>word[j+1]){
                check=0;
                break;
            }
            j++;
        }
        if(check){
            break;
        }
        i++;
    }
    i=0;
    while(i<sum){
        out<<word[i]<<": "<<total[i]<<endl;
        i++;
    }
    return 0;
}
```
#### Python
```Python==

```
**執行結果**
* t5.txt
```
Machine languages and assembly languages are low level languages. C languages, C++ languages, and Java Languages are high level languages. Low level languages are closer to the hardware than are high level Languages. Low level languages can be converted to machine code without using a compiler or interpreter, and the resulting code runs directly on the processor. Low level languages are simple, but are difficult to use.
```
* t6.txt
```
a: 1
and: 3
are: 6
assembly: 1
be: 1
but: 1
c: 1
c++: 1
can: 1
closer: 1
code: 2
compiler: 1
converted: 1
difficult: 1
directly: 1
hardware: 1
high: 2
interpreter: 1
java: 1
languages: 11
level: 6
low: 4
machine: 2
on: 1
or: 1
processor: 1
resulting: 1
runs: 1
simple: 1
than: 1
the: 3
to: 3
use: 1
using: 1
without: 1
```
## 2023.12.18
### 1
**題目**

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int cool(char word[],int n);

int main(int argc, char *argv[]) {
	int i, j, k, length, check=1;
	char word[30];
	FILE *dp = fopen("t15.txt","r");
	if(dp!=NULL){
		while((fscanf(dp,"%s",word))!=EOF){
			length = strlen(word);
			//printf("%s\n",word);
			check=cool(word,length);
			if(check!=0){
				printf("Yes\n");
			}
			else{
				printf("No\n");
			}
		}
	}
	fclose(dp);
	return 0;
}

int cool(char word[],int n){
	int j, k, l, check=0, alpha[26]={}, ans[31]={};
	for( j=0 ; j<n ; j++ ){
		if(isalpha(word[j])==1){
			k = (int)word[j];
			alpha[k-65]++;
		}
		else if(isalpha(word[j])==2){
			k = (int)word[j];
			alpha[k-97]++;
		}
	}
	j=0;
	do{
		//printf("%d ",alpha[j]);
		j++;
	}while(j<26);
	j=0;
	while(j<26){
		if(alpha[j]!=0){
			k = alpha[j];
			ans[k]++;
		}
		j++;
	}
	//printf("\n");
	j=0;
	while(j<31){
		printf("%d ",ans[j]);
		if(ans[j]>1){
			break;
		}
		if(ans[j]==1){
			check++;
		}
		j++;
	}
	//printf("(%d,%d)\n",check,ans[j]);
	if(check<=1||j<31){
		return 0;
	}
	else{
		return 1;
	}
}
```
#### C++
```C++==
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	ifstream in ("t15.txt");
	int length, i, j, k, total=0;
	string input;
	while(in>>input){
		total=0;
		length=input.length();
		if(length==1){
			cout<<"No\n";
			continue;
		}
		int ans[length][2];
		i=0;
		while(i<length){
			if(isupper(input[i])) input[i]=tolower(input[i]);
			ans[i][0]=0;
			ans[i][1]=0;
			j=0;
			while(j<total){
				if(ans[j][0]==(int)input[i]){
					ans[j][1]++;
					break;
				}
				j++;
			}
			if(j==total){
				ans[j][0]=(int)input[i];
				ans[j][1]=1;
				total++;
			}
			i++;
		}
		i=0;
		while(i<total){
			j=total;
			while(j>=i){
				if(ans[j][1]<ans[j+1][1]){
					int temp=ans[j][0];
					ans[j][0]=ans[j+1][0];
					ans[j+1][0]=temp;
					temp=ans[j][1];
					ans[j][1]=ans[j+1][1];
					ans[j+1][1]=temp;
				}
				j--;
			}
			int check=1;
			j=0;
			while(j<total){
				if(ans[j][1]<ans[j+1][1]){
					check=0;
					break;
				}
				j++;
			}
			if(check){
				break;
			} 
			i++;
		}
		int check=1;
		i=0;
		do{
//			cout<<ans[i][1]<<" ";
			j=1;
			while(i+j<total){
				if(ans[i][1]==ans[i+j][1]){
					check=0;
					break;
				}
				j++;
			}
			if(check==0) break;
//			if(ans[0][1]-i!=ans[i][1]||total==1){
//				check=0;
//				break;
//			}
			i++;
		}while(ans[i][1]!=0);
//		cout<<"("<<total<<") ";
		if(check&&total>1){
			cout<<"Yes\n";
		}
		else{
			cout<<"No\n";
		}
	}
	return 0;
}
```
#### Python
```Python==

```
**執行結果**
* t15.txt
```
abcdeabcdAbcdabcdaBcabaeeeffg
abcDabcdabcdAbcdabcabaEeeffg
AaBaa
kkKkk
ab
CddeFaaAGgbBCFffggGGgaBbbCab
ababa
abcdefaabbCCddeefabcdabcaaba
a
aakaKaabkbbbckckcddeffkfffKf
JJyjj
aakaKaabkbbbcckckcddeffkfffKf
abcdabcdabcdabcdABCABA
Abba
abcdabcdabcdabcdabcabaBCC
abcdefaabbCCddeefabcdabcaaba
Abbbbaaa
Abcbbbbcc
aaccdadaCc
AaBaBa
deffFaaAcdFfbBcfGggaabbcgGGg
HHhHH
```
* 輸出
![image](https://hackmd.io/_uploads/BJXPfZWTT.png)

### 2
**題目**

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void read(int a[31][31], FILE *dp, int n);

int main(int argc, char *argv[]) {
	int time, i, j, k=0, l, num, a[31][31]={{},{}}, total=1, temp[num], check;
	FILE *dp = fopen("t9.txt","r");
	if(dp!=NULL){
		if((fscanf(dp,"%d",&time))!=EOF){
			i=time;
			read(a,dp,time);
			while(i>0){
				check=0;
				for( j=0 ; j<31 ; j++ ){
					for( k=j ; k<31 ; k++ ){
						if(j!=k&&a[j][k]==i){
							printf("(%d,%d) %d 次\n",j,k,i);
							check=1;
						}
					}
				}
				if(check)total++;
				i--;
				if(total>2){
					break;
				}
			}
		}
	}
	fclose(dp);
	return 0;
}

void read(int a[31][31], FILE *dp, int n){
	int i, j, k=0, num, temp[num], row, line;
	while(k<n){
		fscanf(dp,"%d",&num);
		int temp[num];
		for( i=0 ; i<num ; i++ ){
			fscanf(dp,"%d",&temp[i]);
		}
		for( i=0 ; i<num-1 ; i++ ){
			for( j=i+1 ; j<num ; j++ ){
				row = temp[i];
				line = temp[j];
				a[row][line]++;
			}
		}
		k++;
	}
}
```
#### C++
```C++==
#include <iostream>
#include <fstream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	ifstream in ("t9.txt");
	int time, total, i, j, k, ans[30][30]={{},{}}, max;
	in>>time;
	max=time;
	i=0;
	while(i<time){
		in>>total;
		int temp[total];
		j=0;
		while(j<total){
			in>>temp[j++];
		}
		j=0;
		while(j<total-1){
			k=1;
			while(j+k<total){
				ans[temp[j]-1][temp[j+k]-1]++;
				k++;
			}
			j++;
		}
		i++;
	}
	total=0;
	do{
		i=0;
		while(i<30){
			j=i+1;
			while(j<30){
				if(ans[i][j]==max){
					total++;
					cout<<"("<<i+1<<","<<j+1<<") ";
				}
				j++;
			}
			i++;
		}
		if(total){
			cout<<total<<" 次\n";
			break;
		}
		max--;
	}while(total==0);
	return 0;
}
```
#### Python
```Python==

```
**執行結果**
* t9.txt
```
20
30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
5 1 3 5 7 9
3 2 3 4
2 5 6
3 6 8 9
3 1 3 4
3 4 5 7
4 3 4 8 9
10 1 3 5 7 9 11 13 15 17 19
10 2 4 6 8 10 11 13 15 17 19
7 1 3 5 11 14 16 19
7 1 2 3 4 5 6 7
4 2 3 4 5
4 6 7 8 19
8 4 5 6 7 8 9 10 11
6 2 3 4 6 8 10
4 5 8 9 10
3 8 9 10
6 2 3 5 7 8 9
5 1 3 7 8 19
```
* 輸出
![image](https://hackmd.io/_uploads/Skm_v3x6p.png)

### 3
**題目**

**解答**
#### C
```C==
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	long long int i, j, k, num, check=0;
	long long int sum1, sum2;
	FILE *dp = fopen("t16.txt","r");
	if(dp!=NULL){
		while(fscanf(dp,"%lld",&num)==1){
		//	printf("%d\n",num);
			i=2;
			check=0;
			sum2 = num*(num+1);
			while(i<num){
					sum1 = 2*i*i;
					
					if(sum1<sum2){
						i++;
					}
					else if(sum1==sum2){
						check=1;
						break;
					}
					else if(sum1>sum2){
						break;
					}
			}
			if(check){
				printf("%lld\n",i);
			}
			else{
				printf("null\n");
			}
		}
	}
	fclose(dp);
	return 0;
}
```
#### C++
```C++==
#include <iostream>
#include <fstream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	ifstream in ("t16.txt");
	long long int n, i;
	while(in>>n){
		i=n-1;
		do{
			i--;
		}while(2*i*i>n*n+n);
		//cout<<"("<<2*i*i<<","<<n*n+n<<")"<<endl;
		if(2*i*i==n*n+n){
			cout<<i<<endl;
		}
		else{
			cout<<"null"<<endl;
		}
	}
	return 0;
}
```
#### Python
```Python==

```
**執行結果**
* t16.txt
```
9800
8
129
288
48547
6732
1681
57121
7893
49
14569
```
* 輸出
![image](https://hackmd.io/_uploads/HJHlMhxTT.png)

## 2023.12.25
### 1
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream>
#include <fstream>
#include <cstring>
#include <cassert>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	ifstream in ("t7.txt");
	ofstream out ("t8.txt");
	int length, i, j, k, n, total=0;
	string input;
	while(!in.eof()){
		in>>input;
		//cout<<input<<endl;
		length=input.length();
		total=0;
		n=length;
		while(n>0&&total==0){
			i=0;
			while(i+n<=length){
				if(input[i]==input[i+n-1]){
					j=0;
					do{
						j++;
					}while(input[i+j]==input[i+n-1-j]&&2*j<=n-1);
					if(2*j>n-1){
						total++;
					}
				}
				i++;
			}
			if(total){
				
				out<<n<<","<<total<<endl;
				//cout<<n<<","<<total<<endl;
				break;
			}
			n--;
		}
		//cout<<n<<endl;
	}
	return 0;
}
```
#### Python
```Python==

```
**執行結果**
* t7.txt
```
98984A4AkAk7K7979d9d4d4K4Ks
AaBbCcbBaA
baaBcBaaaBcAcBaBcAbAcBcAbC
K11122ddddd2211122dddd22122dddd2dddd2211122dddd11
abcdddcbcdddddcbaaabcdcbaadaabcwcbaaabcwcbaabaabcdd
aa5bb5cc5dd5ee5ff5gg5hh5ii5jj
aBcBdfgWWWjhktYtoi575QqQedkckqwlkPPp
aaBBBaaBBBaAAaBBBaAaBBaAaBBaBB
A5555555bbbbb5bbb5b5bbAAAbbbAA5AAbAA5BB
aabbcc123456ddeeff
```
* t8.txt
```
3,15
1,10
7,6
15,5
17,1
4,8
3,6
12,2
7,9
2,6
```
### 2
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream>
#include <fstream>
#include <cstring>
#include <cassert>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	ifstream in ("t12.txt");
	ofstream out ("t13.txt");
	int length, i, j, k, point=0, wrap;
	string word;
	cout<<"The width of word wrap: ";
	cin>>wrap;
//	i=0;
//	while(i++<wrap) cout<<"1";
//	cout<<endl;
	while(in>>word){
		length=word.length();
//		cout<<word<<"("<<point<<","<<length<<")"<<endl;
		if(point+length>wrap&&point>1){
			point=0;
			out<<endl;
		}
		if(point+length==wrap-1){
			out<<word<<" "<<endl;
			point=0;
		}
		else if(point+length==wrap){
			out<<word<<endl;
			out<<" ";
			point=1;
		}
		else if(length>wrap){
			i=0;
			while(i<=length){
				if(point+i==wrap){
					out<<endl;
				}
				if(i==length){
					out<<" ";
					break;
				}
				out<<word[i++];
			}
			point=length%wrap+1;
		}
		else{
			out<<word<<" ";
			point+=length+1;
		}
	}
	return 0;
}
```
#### Python
```Python==

```
**執行結果**
* t12.txt
```
1234567 12345 12345 123 1234 1234567 123 1234 12345678901234567890 1234567 12345678901234567890 12345678 12345678901234 1234 123456789012345 1234567890123456
```
* t13.txt

The width of word wrap: 14
```
1234567 12345 
12345 123 1234
 1234567 123 
1234 
12345678901234
567890 1234567
 1234567890123
4567890 
12345678 
12345678901234
 1234 
12345678901234
5 
12345678901234
56 
```
### 3
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int scope, i=1, j, k, total=0, check, temp;
	cout<<"Please input a number: ";
	cin>>scope;
	while(total<scope){
		check=1;
		temp=i;
		while(temp%2==0){
			temp/=2;
		}
		while(temp%3==0){
			temp/=3;
		}
		while(temp%5==0){
			temp/=5;
		}
		if(temp==1){
			total++;
			cout<<i;
			if(total==scope){
				cout<<endl;
				break;
			}
			else{
				cout<<", ";
			}
		}
		i++;
	}
	return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/BkzBFjgT6.png)

## 2024.01.08
### 1
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	long long int a, b, i, j, k, ans[10]={}, temp;
	cout<<"請輸入 A:B......";
	scanf("%lld:%lld",&a,&b);
	i=a;
	while(i<=b){
//		cout<<i<<"(";
		temp=i;
		do{
			ans[temp%10]++;
//			cout<<temp/10<<", ";
			temp/=10;
		}while(temp!=0);
//		cout<<")\n";
		i++;
	}
	cout<<"0~9 數字的出現次數為......"; 
	i=0;
	while(i<10){
		cout<<ans[i];
		if(i!=9) cout<<", ";
		i++;
	}
	return 0;
}
```
#### Python
```Python==

```
**執行結果**
![image](https://hackmd.io/_uploads/Hkr7WAcaa.png)

### 2
**題目**

**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int i, num, temp, total=0;
	char roman[7]={'I','V','X','L','C','D','M'}, ans[20], display[20];
	cout<<"請輸入介於 1~3999 之間的阿拉伯數字: ";
	cin>>num;
	i=0;
	while(num!=0){
		temp=num%10;
//		cout<<temp<<endl;
		switch(temp){
			case 0:{
				break;
			}
			case 9:{
				ans[total++]=roman[i+2];
				ans[total++]=roman[i];
				break;
			}
			case 4:{
				ans[total++]=roman[i+1];
				ans[total++]=roman[i];
				break;
			}
			default:{
				while(temp!=5&&temp!=0){
					ans[total++]=roman[i];
					temp--;
				}
				if(temp==5) ans[total++]=roman[i+1];
				break;
			}
		}
//		cout<<"("<<ans<<","<<num<<")"<<endl;
		num/=10;
		i+=2;
	}
	i=0;
	while(i<total){
		display[i]=ans[total-i-1];
		i++;
	}
	display[i]='\0';
	cout<<display<<endl;
	return 0;
}
```
#### Python
```Python==

```
**執行結果**

### 3
**題目**

**解答**
#### C
第一種方法: 先將文字轉數字，再轉文字，前後比較文字是否有差別
```C==

```
#### C++
第二種方法: 查表法
```C++==
#include <iostream>
#include <cstring>
#include <string>
#include <string.h>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void conduct(char table[3999][20]);

int main(int argc, char** argv) {
	int i, check=0;
	char table[3999][20], input[20];
	conduct(table);
	while(cin>>input){
//		cout<<input<<endl;
		check=0;
		i=0;
		while(i<3999&&check==0){
			if(strcmp(input,table[i])==0) check=1;
//			if(i==793) cout<<"("<<table[i]<<","<<strcmp(input,table[i])<<")"<<endl;
			i++;
		}
		if(check) cout<<"轉為阿拉伯數字為 "<<i<<endl;
		else cout<<"羅馬數字輸入錯誤!!\n"; 
	}
	
	
	return 0;
}

void conduct(char table[3999][20]){
	int i, number=1, num, temp, total=0;
	char roman[7]={'I','V','X','L','C','D','M'}, ans[20], display[20];
	while(number<4000){
		total=0;
		num=number;
		i=0;
		while(num!=0){
			temp=num%10;
			switch(temp){
				case 0:{
					break;
				}
				case 9:{
					ans[total++]=roman[i+2];
					ans[total++]=roman[i];
					break;
				}
				case 4:{
					ans[total++]=roman[i+1];
					ans[total++]=roman[i];
					break;
				}
				default:{
					while(temp!=5&&temp!=0){
						ans[total++]=roman[i];
						temp--;
					}
					if(temp==5) ans[total++]=roman[i+1];
					break;
				}
			}
			num/=10;
			i+=2;
		}
		i=0;
		while(i<total){
			table[number-1][i]=ans[total-i-1];
			i++;
		}
		table[number-1][i]='\0';
//		if(number>900&&number<2985) cout<<table[number-1]<<"("<<number<<")"<<endl;
		number++;
	}
}
```
#### Python
```Python==

```
**執行結果**

我還在努力啦~٩(๑>◡<๑)۶