---
# System prepended metadata

title: 自學實作題
tags: [ZeroJudge & Leetcode, 實作, 教材題目練習]

---

自學實作題(C/C++/Python)
===

**目錄**
[TOC]

# 自學ZeroJudge
## a
### a010. Factoring
[題目連結](https://zerojudge.tw/ShowProblem?problemid=a010)
```C==
#include <stdio.h>
#include <stdlib.h>
#include <string.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, check=1,count, time=0;
	scanf("%d",&num);
	for( i=2 ; i<=num ; i++ ){
		check=1;
		for( j=2 ; j*j<=i ; j++ ){
			if(i%j==0){
				check=0;
				break;
			}
		}
		if(check){
			count=0;
			while(num%i==0){
				count++;
				num/=i;
			}
			if(count>0){
				time++;
				if(time==1){
					printf("%d",i);
				}
				else{
					printf(" * %d",i);
				}
				if( count>1){
					printf("^%d",count);
				}
			}
		}
	}
	return 0;
}
```
### a013. Roman Numerals
[題目連結](https://zerojudge.tw/ShowProblem?problemid=a013)
```C==
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

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

int judge(char arr[]);
void ansjudge(int ansnum, char ans[]);

int main(int argc, char *argv[]) {
	int  anum, bnum, ansnum;
	char a[100], b[100], ans[100];
	while( scanf("%s",a)==1){
		if( a[0]=='#'){
			break;
		}
		scanf("%*c%s",b);
		anum = judge(a);
		bnum = judge(b);
		ansnum = abs(anum-bnum);
		//printf("%d\n",ansnum);
		ansjudge(ansnum,ans);
	}
	return 0;
}

int judge(char arr[]){
	int i=0, j, k, total=0, length;
	length = strlen(arr);
	while(i<length&&(arr[i]!='\0'||arr[i]!='\n')){
		switch(arr[i]){
			case 'I':{
				if(arr[i+1]=='V'){
					total+=4;
					i+=2;
				}
				else if(arr[i+1]=='X'){
					total+=9;
					i+=2;
				}
				else{
					total++;
					i++;
				}
				break;
			}
			case 'V':{
				total+=5;
				i++;
				break;
			}
			case 'X':{
				if(arr[i+1]=='L'){
					total+=40;
					i+=2;
				}
				else if(arr[i+1]=='C'){
					total+=90;
					i+=2;
				}
				else{
					total+=10;
					i++;
				}
				break;
			}
			case 'L':{
				total+=50;
				i++;
				break;
			}
			case 'C':{
				if(arr[i+1]=='D'){
					total+=400;
					i+=2;
				}
				else if(arr[i+1]=='M'){
					total+=900;
					i+=2;
				}
				else{
					total+=100;
					i++;
				}
				break;
			}
			case 'D':{
				total+=500;
				i++;
				break;
			}
			case 'M':{
				total+=1000;
				i++;
				break;
			}
		}
	}
	return total;
}

void ansjudge(int ansnum, char ans[]){
	int roman_num[7] = {1,5,10,50,100,500,1000}, i=0, j=0, k, n;
	char roman_word[7] = {'I','V','X','L','C','D','M'};
	if(ansnum==0){
		printf("ZERO\n");
		return;
	}
	while(ansnum!=0){
		n = ansnum%10;
		ansnum/=10;
		switch(n){
			case 0:{
				j--;
				break;
			}
			case 4:{
				ans[j]=roman_word[i+1];
				j++;
				ans[j]=roman_word[i];
				//printf("%c%c ",ans[j-1],ans[j]);
				break;
			}
			case 9:{
				ans[j]=roman_word[i+2];
				j++;
				ans[j]=roman_word[i];
				//printf("%c%c ",ans[j-1],ans[j]);
				break;
			}
			case 5:{
				ans[j]=roman_word[i+1];
				//printf("%c ",ans[j]);
				break;
			}
			default:{
				do{
					ans[j]=roman_word[i];
					//printf("%c",ans[j]);
					j++;
					n--;
				}while(n!=5&&n!=0);
				if( n==5){
					ans[j]=roman_word[i+1];
					//printf("%c ",ans[j]);
				}
				else{
					//printf(" ");
					j--;
				}
			} 
			 
		}
		i+=2;
		j++;
	}
	//printf("\n");
	for( k=j-1 ; k>=0 ; k-- ){
		printf("%c",ans[k]);
	}
	printf("\n");
}
```
### a015. Matrix Flip
[題目連結](https://zerojudge.tw/ShowProblem?problemid=a015)
```C==
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

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

int judge(char arr[]);
void ansjudge(int ansnum, char ans[]);

int main(int argc, char *argv[]) {
	int i, j, k, m, n;
	while(scanf("%d %d",&m,&n)==2){
		int num[m][n];
		for( i=0 ; i<m ; i++ ){
			for( j=0 ; j<n ; j++ ){
				scanf("%d",&num[i][j]); 
			}
		}
		for( j=0 ; j<n ; j++ ){
			for( i=0 ; i<m ; i++ ){
				printf("%d ",num[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
}
```


### a020. ID card check
[題目連結](https://zerojudge.tw/ShowProblem?problemid=a020)
```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, j, k, check[11], total=0;
	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];
	scanf("%s",input);
	for( i=1 ; i<10 ; i++ ){
		check[i+1]=(int)input[i]-48;
	}
	for(i=0 ; i<26 ; i++){
		if(input[0]==alpha[i]){
			check[0]=i/10+1;
			check[1]=i%10;
			break;
		}
	}
	total=check[0]+check[10];
	for(i=1 ; i<10 ; i++){
		total+=check[i]*(10-i);
	}
	if(total%10==0){
		printf("real\n");
	}
	else{
		printf("fake\n");
	}
	return 0;
}
```
### a022. Palindrome
[題目連結](https://zerojudge.tw/ShowProblem?problemid=a022)
```C==
#include <stdio.h>
#include <stdlib.h>
#include <string.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, j, k, length;
	char input[1000];
	scanf("%s",input);
	length = strlen(input);
	for(i=0 ; 2*i<=length ; i++ ){
		if(input[i]!=input[length-1-i]){
			break;
		}
	}
	if(2*i>length)printf("yes\n");
	else printf("no\n");
	return 0;
}
```
### a024. GCD
[題目連結](https://zerojudge.tw/ShowProblem?problemid=a024)
```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 a, b, temp;
	scanf("%lld %lld",&a,&b);
	while(a!=0){
		if(a<b){
			temp=a;
			a=b;
			b=temp;
		}
		a%=b;
	}
	printf("%lld\n",b);
	return 0;
}
```
### a040. Armstrong number
[題目連結](https://zerojudge.tw/ShowProblem?problemid=a040)
```C==
#include <stdio.h>
#include <stdlib.h>
#include <math.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, m, i, j, k, total=0, x, num, ans=0, a[7]={};
	scanf("%d %d",&n,&m);
	for( i=n ; i<=m ; i++ ){
		num=i;
		ans=0;
		j=0;
		while(num!=0){
			a[j++]=num%10;
			num/=10;
		}
		for(k=0 ; k<j ; k++){
			ans+=pow(a[k],j);
		}
		if(ans==i){
			printf("%d ",i);
			total++;
		}
	}
	if(total==0){
		printf("none\n");
	}
	return 0;
}
```
### a042. Flat round cutting
[題目連結](https://zerojudge.tw/ShowProblem?problemid=a042)
```C==
#include <stdio.h>
#include <stdlib.h>
#include <math.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, ans;
	while(scanf("%d",&num)==1){
		ans = pow(num,2)-num+2;
		printf("%d\n",ans);
	}
	return 0;
}
```
### a.044 Space translation
[題目連結](https://zerojudge.tw/ShowProblem?problemid=a044)
```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;
	while(scanf("%d",&a)!=EOF){
		printf("%d\n",(a*a*a+5*a+6)/6);
	}
	return 0;
}
```
:::info
補充
===
![image](https://hackmd.io/_uploads/Sk3iX0m-R.png)
:::
### a.045 

## b
## c
## d

# 教材實作題
C教材:
C\++教材:C/C++技研手冊、C\++程式設計入門(第二版)
Python教材:
## Easy
### 1
**題目**
某補習班優待第一位報名者為20000元，以後每位遞增100元。設計程式讓使用者輸入三位報名者姓名，然後顯示資料如下圖。

**執行結果**
![1](https://hackmd.io/_uploads/rJ-620Fc6.png)

**解答**
#### 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;
	char name1[20], name2[20], name3[20];
	char temp[20];
	cout<<"請輸入第一位報名者的姓名:";
	cin.getline(name1,20);
	cout<<"請輸入第二位報名者的姓名:";
	cin.getline(name2,20);
	cout<<"請輸入第三位報名者的姓名:";
	cin.getline(name3,20);
	cout<<"姓名\t編號\t費用\n";
	for( i=0 ; i<3 ; i++ ){
		if(i==0)cout<<name1;
		else if(i==1)cout<<name2;
		else if(i==2)cout<<name3;
		cout<<"\t00"<<i+1<<"\t"<<20000+i*100<<"\n";
	}
	system("pause"); 
	return 0;
}
```
#### Python
```Python==

```
### 2
**題目**
顯示本機作業系統各種資料型別、變數及運算式所佔記憶體的大小。

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

**解答**
#### 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 a, b;
	char c;
	short d;
	cout<<"char\t記憶體大小為\t"<<sizeof(char)<<endl;
	cout<<"short\t記憶體大小為\t"<<sizeof(short)<<endl;
	cout<<"int\t記憶體大小為\t"<<sizeof(int)<<endl;
	cout<<"long\t記憶體大小為\t"<<sizeof(long)<<endl;
	cout<<"float\t記憶體大小為\t"<<sizeof(float)<<endl;
	cout<<"double\t記憶體大小為\t"<<sizeof(double)<<endl;
	cout<<"string\t記憶體大小為\t"<<sizeof(string)<<endl;
	cout<<"變數 a\t記憶體大小為\t"<<sizeof(a)<<endl;
	cout<<"運算式 a+b 記憶體大小為\t"<<sizeof(a+b)<<endl;
	system("pause"); 
	return 0;
}
```
#### Python
```Python==

```
### 3
**題目**
請使用者輸入三個任意數，程式會顯示三數中的最大數。

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

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

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

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

#define Max 0x3f3f3f3f

int main(int argc, char** argv) {
	int num[3], i, j, k, ans=INT_MIN;
	cout<<"請輸入第一個數:";
	cin>>num[0];
	cout<<"請輸入第二個數:";
	cin>>num[1];
	cout<<"請輸入第三個數:";
	cin>>num[2];
	for(i=0 ; i<3 ; i++ )
		ans = num[i]>ans?num[i]:ans;
	cout<<"輸出三個數中最大的數為:"<<ans<<"\n"; 
	system("pause"); 
	return 0;
}
```
#### Python
```Python==

```
### 4
**題目**
請設計一個程式計算圓面積與圓周長，依輸入的半徑計算圓面積與圓周長(取四捨五入小數後兩位)。

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

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

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

int main(int argc, char *argv[]) {
	double r, x=M_PI, cir, area;
	printf("請輸入半徑: ");
	scanf("%lf",&r);
	cir=round(2*r*x*100)/100;
	area=round(r*r*x*100)/100;
	printf("圓周長為: %lf\n",cir);
	printf("圓周長為: %lf\n",area);
	return 0;
}
```
#### C++
```C++==
#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
using namespace std;

int main()
{
    double r;
    cout<<"請輸入半徑: ";
    cin>>r;
    cout<<"圓周長為: "<< round(2*r*M_PI*100)/100<<"\n";
    cout<<"圓體積為: "<< round(r*r*M_PI*100)/100<<"\n";
    return 0;
}
```
#### Python
```Python==

```

### 5
**題目**
依照所輸入的本金利率將定存一到三年間的本利率與利息和以複利方式進行計算。

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

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

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

int main()
{
    double A, r, money;
    cout<<"請輸入本金: ";
    cin>>A;
    cout<<"請輸入年利率: ";
    cin>>r;
    money=A*(1+r/100);
    cout<<"第一年本利和為: "<< money<<"\n";
    money*=(1+r/100);
    cout<<"第二年本利和為: "<< money<<"\n";
    money*=(1+r/100);
    cout<<"第三年本利和為: "<< money<<"\n";
    return 0;
}
```
#### Python
```Python==

```
### 6
**題目**
![110404](https://hackmd.io/_uploads/BJbYG7C9a.jpg)
< 資料來源 : C++ 程式設計入門 P.4-14 >

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

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

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

int main()
{
    long long int money, pay=0;
    cout<<"請輸入所得淨額: ";
    cin>>money;
    switch(money){
        case 1 ... 500000:{
            cout<<"所得稅為"<<money*0.05<<"\n";
            break;
        }
        case 500001 ... 1000000:{
            cout<<"所得稅為"<<money*0.1-500000*(0.1-0.05)<<"\n";
            break;
        }
        case 1000001 ... 2000000:{
            cout<<"所得稅為"<<money*0.2-1000000*0.1-500000*(0.1-0.05)<<"\n";
            break;
        }
        case 2000001 ... 4000000:{
            cout<<"所得稅為"<<money*0.3-2000000*0.1-500000*(-0.05)<<"\n";
            break;
        }
        default:{

        }
    }
    return 0;
}
```
#### Python
```Python==

```
### 7
**題目**
**執行結果**
![image](https://hackmd.io/_uploads/rJJ38xZip.png)

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

```
```C++==
#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
#include <ctime>
using namespace std;

int main()
{
    int dice;
    srand(time(NULL));
    do{
        dice=rand()%6+1;
        cout<<"擲骰子點數為: "<<dice<<"\n";
        if(dice==6)break;
    }while(dice!=6);
    return 0;
}
```
#### Python
```Python==

```
### 8
**題目**
請製作一個樂透開獎程式，請隨機產生六個介於 1 到 48 之間的號碼，且號碼不能重複則表示若有重複，則再產生一個號碼，直到不重複為止。

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

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

```
#### C++
```C++==
#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    int prize[6], check=1, i, j;
    srand(time(NULL));
	for(i=0 ; i<6 ; i++){
        check=0;
        do{
			prize[i]=rand()%48+1;
            for(j=0 ; j<i ; j++){
                if(prize[i]==prize[j]){
                    check=1;
                    break;
                }
            }
        }while(check);
        cout<<"中獎號碼: "<<prize[i]<<endl;
    }
    return 0;
}
```
#### Python
```Python==

```
### 9
**題目**
寫一個程式允許輸入角度，求該角度的Sin、Cos與Tan值。

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


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

```
#### C++
```C++==
#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <cstring>
using namespace std;

int main()
{
    double angle, x=M_PI, path;
    cout<<"請輸入角度: ";
    cin>>angle;
    path=angle*x/180;
    cout<<"Sin("<<angle<<")= "<<sin(path)<<"\n";
    cout<<"Cos("<<angle<<")= "<<cos(path)<<"\n";
    cout<<"Tan("<<angle<<")= "<<tan(path)<<"\n";
    return 0;
}
```
#### Python
```Python==

```
### 10
**題目**
寫一個老師帶的班級學生結構，其包含老師姓名、學生個資(名字、座號、國英數成績)。
**解答**
#### C
```C==

```
#### C++
```C++==
#include <iostream> //固定輸入與輸出流為鍵盤與終端機
#include <fstream>  //開通文件流
#define _USE_MATH_DEFINES   //定義數學常數
#include <cmath>    //數學函式庫
#include <ctime>    //時間函式庫
#include <cstdlib>  //C語言中的基本函式庫(繼承)
#include <cctype>   //辨別資料型態
#include <string>
#include <cstring>  //string資料型態的設定
using namespace std;

typedef struct stu_{
    string name;
    int seat;
    int score_c;
    int score_e;
    int score_m;
}stu;

typedef struct tea_{
    string name;
    stu student[2];
}tea;

tea* in(tea *);
void check(tea *);

int main()
{
    tea  *point, teacher;
    //cout<<"請輸入學生1的座位: ";
    //cin>>teacher.student[0].seat;
    //point=&teacher;
    //cout<<&teacher<<endl;
    //cout<<point<<endl;
    //cout<<"請輸入學生1的座位: ";
    //cin>>point->student[0].seat;
    point=in(&teacher);
    check(point);
    return 0;
}

tea* in(tea *class1){
    //cout<<class1<<endl;
    cout<<"請輸入老師的名字: ";
    getline(cin, class1->name);
    cout<<class1->name<<endl;
    cout<<"請輸入學生1的名字: ";
    getline(cin,class1->student[0].name);
    cout<<class1->student[0].name<<endl;
    cout<<"請輸入學生1的座位: ";
    cin>>class1->student[0].seat;
    cout<<class1->student[0].seat<<endl;
    cout<<"請輸入學生1的國文成績: ";
    cin>>class1->student[0].score_c;
    cout<<"請輸入學生1的英文成績: ";
    cin>>class1->student[0].score_e;
    cout<<"請輸入學生1的數學成績: ";
    cin>>class1->student[0].score_m;
    cin.get();
    cout<<"請輸入學生2的名字: ";
    getline(cin,class1->student[1].name);
    cout<<class1->student[1].name<<endl;
    cout<<"請輸入學生2的座位: ";
    cin>>class1->student[1].seat;
    cout<<"請輸入學生2的國文成績: ";
    cin>>class1->student[1].score_c;
    cout<<"請輸入學生2的英文成績: ";
    cin>>class1->student[1].score_e;
    cout<<"請輸入學生2的數學成績: ";
    cin>>class1->student[1].score_m;
    return class1;
}

void check(tea *class1){
    cout<<class1->name<<endl;
    cout<<class1->student[0].name<<"\t"<<class1->student[0].seat<<"\t"<<class1->student[0].score_c<<"\t"<<class1->student[0].score_e<<"\t"<<class1->student[0].score_m<<endl;
    cout<<class1->student[1].name<<"\t"<<class1->student[1].seat<<"\t"<<class1->student[1].score_c<<"\t"<<class1->student[1].score_e<<"\t"<<class1->student[1].score_m<<endl;
}
```
#### Python
```Python==

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

## Medium

## Hard

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