---
# System prepended metadata

title: 計算機程式設計檢討

---

# 計算機程式設計檢討
## selection problems
### 1
![](https://i.imgur.com/5cSG6Tn.png)
Except 0 , when adding ! before any number, it's result will be always zero.![](https://i.imgur.com/M6RZEI0.png)
### 2
The answer is D
Since we got 123456789 in f, we can pass it into that function.
Then g gets 12345678, and it call f before printing out.
a: 12345678 f : 1234567 print(a % 10)
a: 123456 f : 12345 print(a % 10)
a: 1234 f: 123 print(a % 10)
a: 12 f: 1 print(a % 10)
After exercuting the passing, g will use its variable a to operate print function.
![](https://i.imgur.com/8eWqKzG.png)

## CPE
### Printer
![](https://i.imgur.com/N16utPL.png)

```
#include<stdio.h>
#include<string.h>
#include<limits.h>

int main(){
	int X,Y,num,backup,cheap;
	int i,j,k;
	int cost[2][36+1];
	scanf("%d",&X);
	for(i=1;i<=X;++i){
		printf("Case %d:\n",i);
		for(j=1;j<=36;++j)
			scanf("%d",&cost[0][j]);
		scanf("%d",&Y);
		for(j=0;j<Y;++j){
			scanf("%d",&num);
			backup=num;
			memset(cost[1],0,sizeof(cost[1]));
			cheap=INT_MAX;
			for(k=2;k<=36;++k){
				num=backup;
				while(num){
					cost[1][k]+=cost[0][num%k+1];
					num/=k;
				}
				if(cost[1][k]<cheap)
					cheap=cost[1][k];
			}
			printf("Cheapest base(s) for number %d: ",backup);
			for(k=2;k<=36;++k)
				if(cost[1][k]==cheap)
					printf("%d ",k);
			printf("\n");
		}
		printf("\n");
	}
	return 0;
}
```
### Hotel
![](https://i.imgur.com/ReGGrTa.png)

```
#include<stdio.h>

int main(){
	long long int S,D;
	while(scanf("%lld%lld",&S,&D)!=EOF){
		while(D>0)
			D-=S++;
		printf("%lld\n",S-1);
	}
	return 0;
}
```
### Encyption
![](https://i.imgur.com/OCnDVRk.png)
![](https://i.imgur.com/7LKgHwa.png)

Since hexadecimal just only consists of 4 digits.
We can directly count when it forms 1 after divided by 2
```
#include<stdio.h>

int main(){
	int T,N,b1,b2;
	int i,backup,ext;
	scanf("%d",&T);
	for(i=1;i<=T;++i){
		b1=b2=0;
		scanf("%d",&N);
		backup=N;
		while(N){
			if(N%2)
				++b1;
			N/=2;
		}
		N=backup;
		while(N){
			ext=N%10;
			while(ext){
				printf("%d\n", ext);
				if(ext%2)
					++b2;
				ext/=2;
			}
			N/=10;
		}
		printf("%d %d\n",b1,b2);
	}
	return 0;
}


```
### Largest Square
![](https://i.imgur.com/XmV5h28.png)

```
#include<stdio.h>

#define MAXSIZE 100 

int main(){
	char grid[MAXSIZE+1][MAXSIZE+1];
	char center;
	int T,M,N,Q;
	int i,j,k,l,m,r,c,max,flag;
	scanf("%d",&T);
	for(i=1;i<=T;++i){
		scanf("%d%d%d",&M,&N,&Q);
		printf("%d %d %d\n",M,N,Q);
		for(j=0;j<M;++j)
			scanf("%s",grid[j]);
		for(j=1;j<=Q;++j){
			max=1;
			scanf("%d%d",&r,&c);
			center=grid[r][c];
			for(k=flag=1;flag;++k){
				for(l=k*-1;l<=k&&flag;++l)
					for(m=k*-1;m<=k&&flag;++m)
						if(r+l<0||r+l>=M||c+m<0||c+m>=N||grid[r+l][c+m]!=center)
							flag=0;
				if(flag)
					max+=2;
			}
			printf("%d\n",max);
		} 
	}
	return 0;
} 
```
### Parity
![](https://i.imgur.com/TyJpsuh.png)
```
#include<stdio.h>
#include<string.h>

#define MAXSIZE 32

int f(int); 
int main(){
	int n,count,binary[MAXSIZE]={},i,j;
	while(scanf("%d",&n)&&n!=0){
		memset(binary,0,sizeof(binary));
		count=i=0;
		while(n){
			binary[i]=n%2;
			++i;
			if(n%2)
				++count;
			n/=2;
			
		}
		printf("The parity of ");
		for(j=i-1;j>=0;--j)
			printf("%d",binary[j]);
		printf(" is %d (mod 2).\n",count);
	}
	return 0;
}
```
### Summing Digits
```
#include<stdio.h>

int f(int); 
int main(){
	int n;
	while(scanf("%d",&n)&&n!=0)
		printf("%d\n",f(n));
	return 0;
}
int f(int n){
	int sum=0;
	while(n){
		sum+=n%10;
		n/=10;
	}
	if(sum>=10)
		return f(sum);
	else
		return sum;
}
```
![](https://i.imgur.com/SWorRxQ.png)

### rotating Sentences
![](https://i.imgur.com/yr7qQAZ.png)

```
#include<stdio.h>
#include<string.h>

#define MAXSIZE 100

int main(){
	char str[MAXSIZE+1][MAXSIZE+1]={};
	int i=0,j,k,maxLength=0;
	while(gets(str[i])!=NULL){
		if(strlen(str[i])>maxLength)
			maxLength=strlen(str[i]);
		++i;
	}
	for(j=0;j<maxLength;++j){
		for(k=i-1;k>=0;--k){
			if(j>=strlen(str[k]))
				printf(" ");
			else 
				printf("%c",str[k][j]);
		}
		printf("\n");
	}
	return 0;
}
```
### Examining the country
![](https://i.imgur.com/a1vwNsm.png)

```
#include<stdio.h>
#include<string.h>

#define MAXNAME 75
#define MAXSIZE 2000

int main(){
	char countries[MAXSIZE][MAXNAME+1]={},country[MAXNAME]={};
	char str[MAXSIZE];
	int count[MAXSIZE]={0};
	int n,i,j,c=0,tmp;
	scanf("%d",&n);
	getchar();
	for(i=0;i<n;++i){
		scanf("%s",country);
		gets(str);
		for(j=0;j<c;++j){
			if(strcmp(country,countries[j])==0){
				++count[j];
				break;
			}
		}
		if(j==c){
			++count[c];
			strcpy(countries[c],country);
			++c;
		}
	}
	for(i=c-1;i>=0;--i){
		for(j=0;j<i;++j){
			if(strcmp(countries[j],countries[j+1])>0){
				strcpy(str,countries[j]);
				strcpy(countries[j],countries[j+1]);
				strcpy(countries[j+1],str);
				tmp=count[j];
				count[j]=count[j+1];
				count[j+1]=tmp;
			}
		}
	}
	for(i=0;i<c;++i)
		printf("%s %d\n",countries[i],count[i]);
	return 0;
}
```
### Symmetric Matrix
![](https://i.imgur.com/wiWeJzK.png)

```
#include<stdio.h>
#include<string.h>

#define MAXSIZE 100

int main(){
	int T,n;
	long long int matrix[MAXSIZE][MAXSIZE]={};
	int i,j,k,flag,l;
	scanf("%d",&T);
	for(i=flag=1;i<=T;++i,flag=1){
		getchar();getchar();getchar();getchar();getchar();
		scanf("%d",&n);
		for(j=0;j<n;++j)
			for(k=0;k<n;++k){
				scanf("%lld",&matrix[j][k]);
				if(matrix[j][k]<0)
					flag=0;
			}
		for(j=0;j<=n/2&&flag;++j)
			for(k=0;k<n&&flag;++k)
				if(matrix[j][k]!=matrix[n-j-1][n-k-1])
					flag=0;
		printf("Test #%d: ",i);
		if(flag)
			printf("Symmetric.\n");
		else
			printf("Non-symmetric.\n");
	}
	return 0;
}


```
### Die Game
![](https://i.imgur.com/qa13YA3.png)
```
#include<stdio.h>
#include<string.h>

#define SUM 7

/*
  北
西頂東
  南 
  背 
*/ 

int main(){
	char cmd[6]={};
	int top,north,west,tmp;
	int n,i;
	while(scanf("%d",&n)&&n!=0){
		top=1;
		north=2;
		west=3;
		for(i=1;i<=n;++i){
			scanf("%s",cmd);
			if(strcmp(cmd,"north")==0){
				tmp=top;
				top=SUM-north;
				// The summation of the top and the buttom will be 7
				// If we turn it into north, we will find that the south side is shown
				north=tmp;
			}
			else if(strcmp(cmd,"west")==0){
				tmp=top;
				top=SUM-west;
				west=tmp;
			}
			else if(strcmp(cmd,"south")==0){
				tmp=top;
				top=north;
				north=SUM-tmp;
			}
			else if(strcmp(cmd,"east")==0){
				tmp=top;
				top=west;
				west=SUM-tmp;
			}
			
		printf("top: %d\n",top);
		}
		printf("%d\n",top);
	}
	return 0;
}
```
### Fibonacci bits
![](https://i.imgur.com/QE1qxtF.png)
![](https://i.imgur.com/Fhlegxh.png)
```
#include<stdio.h>

int main(){
	int fibo[50]={1,2};
	int term[50];
	int num;
	int i,j,k,N,flag;
	for(i=2;;++i){
		fibo[i]=fibo[i-1]+fibo[i-2];
		if(fibo[i]>100000000)
			break;
	}
	scanf("%d",&N);
	for(j=1,flag=1;j<=N;++j,flag=1){
		for(k=0;k<i;++k)
			term[k]=0;
		scanf("%d",&num);
		printf("%d = ",num);
		for(k=i-1;k>=0;--k)
			if(num>=fibo[k]){
				term[k]=1;
				num-=fibo[k];
			}
		for(k=i-1;k>=0;--k){
			if(term[k]==0&&flag);
			else{
				flag=0;
				printf("%d",term[k]);
			}
		}
		printf(" (fib)\n");
	}
	return 0;
}
```
## File operating
### File compression
![](https://i.imgur.com/LFZWDqZ.png)
```
#include<stdio.h>

#define MAXSIZE 20

int main(){
	char filename_in[MAXSIZE]={};
	const char filename_out[]={"out.txt"};
	FILE *fpin,*fpout;
	char ch,rec=NULL;
	int count=0;
	gets(filename_in);
	fpin=fopen(filename_in,"r");
	fpout=fopen(filename_out,"w");
	while((ch=getc(fpin))!=EOF){
		if(rec!=ch){
			if(count)
				fprintf(fpout,"%d%c",count,rec);
			rec=ch;
			count=0;
		}
		count++;
	}
	fprintf(fpout,"%d%c",count,rec);
	fclose(fpin);
	fclose(fpout);
	
	FILE *FFFFFFILE;
	char chaaaaaar;
	FFFFFFILE=fopen("out.txt","r");
	while((chaaaaaar=getc(FFFFFFILE))!=EOF)
		putchar(chaaaaaar);
	fclose(FFFFFFILE);
	
	return 0;
} 
```
### Converting txt file into csv file
![](https://i.imgur.com/D2JsypZ.png)
![](https://i.imgur.com/pFdbxwG.png)
![](https://i.imgur.com/pTMYS24.png)
```
#include<stdio.h>

#define MAXSIZE 20

int main(){
	char filename_in[MAXSIZE]={},str[MAXSIZE];
	const char filename_out[]={"out.csv"};
	FILE *fpin,*fpout;
	char name[MAXSIZE]={};
	int score[3];
	int rank;
	float total;
	gets(filename_in);
	fpin=fopen(filename_in,"r");
	fpout=fopen(filename_out,"w");
	fprintf(fpout,"Rank,Name,Score1,Score2,Score3,Total\n");
	fscanf(fpin,"%s%s%s%s%s%s",str,str,str,str,str,str);
	while(fscanf(fpin,"%d %s%d%d%d%f",&rank,name,&score[0],&score[1],&score[2],&total)!=EOF)
		fprintf(fpout,"%d,%s,%d,%d,%d,%.1f\n",rank,name,score[0],score[1],score[2],total);
	fclose(fpin);
	fclose(fpout);
	
	FILE *FFFFFFILE;
	char chaaaaaar;
	FFFFFFILE=fopen("out.csv","r");
	while((chaaaaaar=getc(FFFFFFILE))!=EOF)
		putchar(chaaaaaar);
	fclose(FFFFFFILE);
	
	return 0;
}
```
### Arraging report cards
![](https://i.imgur.com/rJuu6AE.png)
![](https://i.imgur.com/zF829iA.png)
![](https://i.imgur.com/eizgggQ.png)
```
#include<stdio.h>

#define MAXSIZE 20

int main(){
	char filename_in[MAXSIZE]={};
	const char filename_out[]={"out.txt"};
	FILE *fpin,*fpout;
	char name[MAXSIZE]={};
	int score[3];
	gets(filename_in);
	fpin=fopen(filename_in,"r");
	fpout=fopen(filename_out,"w");
	fprintf(fpout,"Name       Score1 Score2 Score3\n");
	//Input its specific content.
	while(fscanf(fpin,"%s%d%d%d",name,&score[0],&score[1],&score[2])!=EOF)
		fprintf(fpout,"%-10s %6d %6d %6d\n",name,score[0],score[1],score[2]);
	fclose(fpin);
	fclose(fpout);
	
	FILE *FFFFFFILE;
	char chaaaaaar;
	FFFFFFILE=fopen("out.txt","r");
	while((chaaaaaar=getc(FFFFFFILE))!=EOF)
		putchar(chaaaaaar);
	fclose(FFFFFFILE);
	
	return 0;
}
```
### Backing up the file
![](https://i.imgur.com/9KVaR1N.png)
```
#include<stdio.h>

#define MAXSIZE 20

int main(){
	char filename_in[MAXSIZE]={};
	const char filename_out[]={"out.txt"};
	FILE *fpin,*fpout;
	char ch;
	gets(filename_in);
	fpin=fopen(filename_in,"r");
	fpout=fopen(filename_out,"w");
	while((ch=getc(fpin))!=EOF)
		putc(ch,fpout);//Use to input the content of the original file 
	fclose(fpin);
	fclose(fpout);
	
	FILE *FFFFFFILE;
	char chaaaaaar;
	FFFFFFILE=fopen("out.txt","r");
	while((chaaaaaar=getc(FFFFFFILE))!=EOF)
		putchar(chaaaaaar);
	fclose(FFFFFFILE);
	
	return 0;
}
```
## Problem solving
### Enum
![](https://i.imgur.com/xUQHCpu.png)
![](https://i.imgur.com/KKB16iW.png)
```
#include<stdio.h>
#include<stdlib.h>

enum suits{
	spade=1,
	heart,
	diamond,
	club
}suit;
enum nums{
	two=1,
	A,
	K,
	Q,
	J,
	ten,
	nine,
	eight,
	seven,
	six,
	five,
	four,
	three,
}num;

int main(){
	scanf("%d%d",&suit,&num);
	if(num<two||num>three){
		printf("wrong number");
		return 0;
	}
	switch(suit){
		case spade:
			printf("S");
			break;
		case heart:
			printf("H");
			break;
		case diamond:
			printf("D");
			break;
		case club:
			printf("C");
			break;
		default:
			printf("wrong suit");
			return 0;
	}
	if(num>=nine&&num<=three)
		printf("%d",16-num);
	switch(num){
		case two:
			printf("2");
			break;
		case A:
			printf("A");
			break;
		case J:
			printf("J");
			break;
		case Q:
			printf("Q");
			break;
		case K:
			printf("K");
			break;
	}
	return 0;
}
```
### Position problem

![](https://i.imgur.com/8QPhqvW.png)
```
#include <stdio.h>
#define MAXSIZE 54
int main(){
	int pos[MAXSIZE]={0};
	int n,lose=0,num=0,count;
	int i;
	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%d",&count);
		num+=count;
	}
	num/=n;
	i=0;
	while(lose!=n-1){
		count=0;
		printf("Lose: %d\n", lose);
		while(1){
			if(pos[i]==0)
				++count;
			if(count==num)
				break;
			i=++i%n;
			printf("i : %d\n", i);
		}
		pos[i]=1;
		for(int j = 0; j < n; ++j) printf("%d ", pos[j]); 
		printf("\n");
		i=++i%n;
                //We have to pass the current position lest
		//we will identify the wrong position in the next loop
		++lose;
	}
	for(i=0;i<n;++i){
		if(pos[i]==0){
			printf("%d",i+1);
			break;
		}
	}
	return 0;
}

```
### Big letter and small letter are mixed in an array

![](https://i.imgur.com/PWmUBVx.png)
```

#include<stdio.h>
#include<string.h>

#define MAXSIZE 10000 

int main(){
	char str[MAXSIZE]={0};
	int k;
	int i,j,rec=-1,recLen=0,count,UL,len;
	char in;
	scanf("%d",&k);
	scanf("%s",str);
	for(i=0;i<strlen(str);++i){
		count=k;len=0;
		UL=str[i]/('Z'+1);//0 = upper 1 = lower
		printf("str: %s\n", str + i);
		printf("UL: %d\n", UL);
		for(j=i;j<strlen(str);++j){
			printf("UL in loop:%d\n", UL);
			if(UL==str[j]/('Z'+1))
			/*
			Since using the division will 
                       let those less than that number be zero,
			those big letters will be smaller than 
                       'Z' + 1 and small letters will be larger
                       than 'Z' + 1
			*/
				--count, printf("STR[j] %d\n", str[j]);
			else{
				printf("break\n");
				break;
			}
			if(!count){
				len+=k;
				UL=!UL;//This is for the big letters
				//Since len will be 0 every time it iterate
                               , we will keep adding
				//If it is in the process of small letter 
                               to big letter
				count=k;
				printf("LEN: %d, k %d\n", len, k);
			}
		}
		if(len>recLen){
			rec=i;recLen=len;
		}
		if(j==strlen(str))
			break;
		else if(len==0)
			i+=k-count-1;
		else if(len>k)
			i+=len-1;
	}
	if(rec==-1){
		printf("No answer");
	}
	else
		for(i=0,j=rec;i<recLen;++i,++j)
			printf("%c",str[j]);
	return 0;
}
```
### the power of 10
![](https://i.imgur.com/ajcuamo.png)

```
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#define max(i, j) ((i) > (j) ? (i) : (j))
int function(char str[], int arr[]){
	char str3[4] = {""};
	int tmp = 0;
	int tmp2 = 0;
	int maxi = 0;
	char *ptr = str;
	char *ptr2 = str;
	int i = 0;
	for(; i < strlen(str); ++i){
		if(str[i] == '*'){
			tmp = *ptr - '0';
		}
		else if(str[i] == '^'){
			ptr2 = str + i + 1;
		}
		else if(str[i] == '+'){
			strcpy(str3, ptr2);
			for(int j = 0; j < strlen(str3); ++j){
				if(str3[j] < '0' || str3[j] > '9'){
					str3[j] = '\0';
					tmp2 = atoi(str3);
					maxi = max(maxi, tmp2);
					arr[tmp2] = tmp;
					break;
				}
			}
			ptr = str + i + 1;
		}
	}
	strcpy(str3, ptr2);
	arr[atoi(str3)] = *ptr - '0';
	return maxi;
	
}
int main(){
	char str[101] = {""};
	int arr[101] = {0};
	int arr2[101] = {0};
	gets(str);
	int max1 = function(str, arr);
	gets(str);
	int max2 = function(str, arr2);
	for(int i = 0; i < max(max1, max2); ++i){
		arr[i] += arr2[i];
		if(arr[i] >= 10){
			arr[i + 1] = arr[i] / 10;
			arr[i] = arr[i] % 10;
		}
	}
	bool cfm = true;
	for(int i = max(max1, max2); i >= 0; --i){
		if(arr[i] > 0 && cfm){
			printf("%d*10^%d", arr[i], i);
			cfm = false;
		}
		else if(arr[i] > 0){
			printf("+%d*10^%d", arr[i], i);
		}
	}
	return 0;
}
```
### Compression
![](https://i.imgur.com/cLb3wts.png)

```
#include<stdio.h>
#include<string.h>

#define WORDSIZE 32
#define MAXWORD 2047

int main(){
	char str[MAXWORD][WORDSIZE]={0};
	char tmp[WORDSIZE]={0};
	int pos[MAXWORD]={0};
	int i=0,j,count=0,nrCount=0;//not repeating
	char in;
	while(scanf("%c",&in)!=EOF&&in!='0'){
		if((in>='a'&&in<='z')||(in>='A'&&in<='Z'))
			tmp[i++]=in;
		else if(strlen(tmp)){
			for(j=0;j<nrCount;++j)
				if(!strcmp(tmp,str[j])){
					printf("%d",count-pos[j]);//pos records every item's position
					//When we come across the string equals str[j], then we subtract 
					//the current count by that position. Because at this point, we will
					//find the element which is spotted first.
					pos[j]=count;
					break;
				}
			if(j==nrCount){
				//If we haven't find any compatible element in the loop,
				//then we can print this tmp string since it will not being replaced with a number.
				printf("%s",tmp);
				pos[j]=count;
				strcpy(str[j],tmp);
				++nrCount;
			}
			++count;
			memset(tmp,0,WORDSIZE);
			i=0;
		}
		if(!i)
			printf("%c",in);//to print those which are not alphat out 
	}
	return 0;
} 
```
### Decode
![](https://i.imgur.com/SRV6924.png)

```
#include<stdio.h>
#include<string.h>
#define MAXSIZE 1000
int main(){
	char str[MAXSIZE]={0};
	int line,count,target,flag;
	int i,j;
	scanf("%d",&line);
	getchar();
	for(i=0;i<line;++i){
		gets(str);
		for(j=0,count=target=flag=1;j<strlen(str);++j,++count){
			if(str[j]==' '){
				count=0;
				flag=1;
			}
			if(count==target&&flag){
				printf("%c",str[j]);
				flag=0;
				++target;
			}
		}
		printf("\n");
	}
	return 0;
} 
```
### The stop problem
![](https://i.imgur.com/6sVbEcp.png)
```
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<stdlib.h> 
#define min(i, j) ((i) < (j) ? (i) : (j))
int n = 0;
struct data{
	int route[101];
	int size;
}stop[101];
int g = 0;
bool cfm = false;
void cheapest(int cost, int s){
	if(n - 1 == s){
		if(cfm == false) {
			g = cost;
			cfm = true;
		}
		else{
			g = min(g, cost);
		}
		return;
	}
	int m = stop[s].size;
	for(int i = 0; m > 0; ++i){
		if(stop[s].route[i]){
			--m;
			cheapest(cost + stop[s].route[i], i);
		}
	}
}
//Use a double array to store the data
//When it has number in certain stops, put it into the array with its cost. 
//I also have to be noticed that which stops I have visited.

int main(){
	scanf("%d", &n);
	int i = 0, i2 = 0, i3 = 0;
	while(scanf("%d %d %d", &i, &i2, &i3) && (i != 0 || i2 != 0 || i3 != 0)){
		stop[i].route[i2] = i3;
		if(stop[i].route[i2]) ++stop[i].size;
	}
	cheapest(0, 0);
	printf("%d", g);
	//Use another array called visited to indicate the element visited
	return 0;
}
```

```

#include<stdio.h>
#include<string.h> 
#include<limits.h>

#define MAXSIZE 100
int main(){
	int costAdj[MAXSIZE][MAXSIZE],minCost[MAXSIZE];
	int node;
	int i,j,start,end,cost;
	for(i=0;i<MAXSIZE;++i){
		minCost[i]=INT_MAX;
		for(j=0;j<MAXSIZE;++j)
			costAdj[i][j]=-1;
	}
	scanf("%d",&node);
	while(scanf("%d %d %d",&start,&end,&cost)&&(start!=0||end!=0||cost!=0))
		costAdj[start][end]=cost;
	for(i = 0; i < node - 1; ++i){
		for(j = 0; j < node; ++j){
			printf("%2d ", costAdj[i][j]);	
		}
		printf("\n");
	}
	minCost[0]=0;
	for(i=0;i<node-1;++i)
		for(j=0;j<node;++j){
			printf("%ld\n", minCost[j]);
			if(costAdj[i][j]!=-1)
				if(minCost[i]+costAdj[i][j]<minCost[j])
					minCost[j]=minCost[i]+costAdj[i][j];
		}
	for(i = 0; i < node; ++i){
			if(minCost[i]== INT_MAX)
				printf("%2d ", 0);
			else
				printf("%2d ", minCost[i]);	
		}
	
	printf("\n%d",minCost[node-1]);
	return 0;
}
```
### Grading paper
![](https://i.imgur.com/ZYvZ1jr.png)
![](https://i.imgur.com/yjR2e44.png)
```

#include<stdio.h>
#include<math.h>
#include<string.h>
#define MAXSTUDENT 53
#define MAXNAME 100
#define MAXTEST 3

struct score{
	int score[MAXTEST];
	float total;
};
struct student{
	char name[MAXNAME+1];
	struct score sc;
};

float change(struct student st[],int);
void quick(struct student st[], int left, int right, int N){
	if(left < 0 || right > N || right - left < 1) return;
	float pi = st[left].sc.total;
	struct student swap;
	int i = left, j = right;
	while(i < j){
		while(i < j && st[j].sc.total >= pi){ --j;}
		while(i < j && st[i].sc.total <= pi){ ++i;}
		if(i < j){
			swap = st[j];
			st[j] = st[i];
			st[i] = swap;
		}
	}
	swap = st[left];
	st[left] = st[i];
	st[i] = swap;
	quick(st, left, i - 1, N);
	quick(st, i + 1, right, N);
}
int main(){
	struct student st[MAXSTUDENT];
	struct student *ptr=st;
	int N;
	float a;
	char target[MAXNAME];
	int i,j;
	scanf("%d",&N);
	getchar();
	for(i=0;i<N;++i){
		gets((ptr+i)->name);
		for(j=0;j<MAXTEST;++j){
			scanf("%d",&(ptr+i)->sc.score[j]);
			if(j==MAXTEST-1)
				(ptr+i)->sc.total+=(ptr+i)->sc.score[j]*0.4;
			else
				(ptr+i)->sc.total+=(ptr+i)->sc.score[j]*0.3;
		}
		getchar();
	}
	quick(st, 0, N - 1, N);
	a=change(st,N);	

	gets(target);
	for(i=0;i<N;++i)
		if(strcmp((ptr+i)->name,target)==0){
			if(a<60)
				if(85+(((60-85)/(a-80))*((ptr+i)->sc.total-80))>(ptr+i)->sc.total){
					if(85+(((60-85)/(a-80))*((ptr+i)->sc.total-80))>100)
						(ptr+i)->sc.total=100;
					else
						(ptr+i)->sc.total=85+(((60-85)/(a-80))*((ptr+i)->sc.total-80));
				}
			printf("%.1f ",(ptr+i)->sc.total);
			if((ptr+i)->sc.total>=60)
				printf("PASS\n");
			else
				printf("FAILED\n");
			break;
		} 
			
	if(i==N)
		printf("no student called %s",target);
	return 0;
}

float change(struct student st[],int N){
	struct student swap;

	if(N%2)
		return st[N/2].sc.total;
	else
		return (st[N/2].sc.total+st[N/2-1].sc.total)/2;
}
```

### Pokemon problem
![](https://i.imgur.com/oavl0g8.png)
![](https://i.imgur.com/BLP3uKv.png)

```
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdbool.h> 
/*
 * The outline of this program:
 * 
 * Preliminary estimation:
 * 		First of all, we use simple for loop to estimate if those Pokemons I caught
 *	 can be upgreded. 波波->比比->大比
 *	 	I will gain the most experiences if I upgrade my Pokemon for the first time.
 *	 So I determine if the remaining number of Pokemon is sufficient so that I will be 
 * 	 able to convert those into candies.
 * 
 * Following steps: 
 * 1. Recording
 *		In order to estimate the number of Pokemons, I used array to indicate each kind of Pokemon.
 * 2. The costs
 *		To realize getting the most number of experiences, I have to consider which Pokemon
 *	 I should take priority to form candies. After finding that I will earn much if I convert
 *   大比 into candies instead of 波波 or 比比 because of the costs of upgrading.  
 * 3. The experiences calculation
 *		In every sections of identifying the upgrade of each Pokemon, I also calculate the summation 
 *	 of the cumulative experiences. If the experiences exceed the threshold of upgrading, the level 
 *   of character will rise by cutting the respective number of experiences.
 */
int main(void) {
    int candy = 0;
    int ex1 = 0;
    int ex2 = 0;
    int level = 0;
    //To initialize the number of this species.
    int arr[3] = {0};//This array contains three elements
    //They respectively indicate different species.
    int bobo = 0;
    printf("請輸入波波數量(輸入 0 結束): ");
    scanf("%d", &bobo);
	//sentintal control 
    while (bobo != 0) {
    	//We use this statement to determine when we should halt the loop.
        arr[0] = bobo;
        if (bobo > 80) ex1 += 3000;
        else if (bobo > 30) ex1 += 2000;
        //First estimation
        candy = bobo * 3;
        if (candy >= 12) {
            --arr[0];
            ++arr[1];
            candy = candy - 12;
            ex1 = ex1 + 2500;
            if (candy < 50 && arr[0] + candy >= 50 && arr[1] > 0) {
                arr[0] -= 50 - candy;
                candy = 50;
            }
        }
        //Since we will earn the most when upgrading the Pokemon in advance,
        //we first identify if it fits the requirements.
        if (candy >= 50 && arr[1] > 0) {
            --arr[1];
            ++arr[2];
            candy = candy - 50;
            ex1 = ex1 + 2500;
        }
        //The remaining sections
        //If finding that there is any Pokemon which is able to upgrade,
        //we verify how many Pookemon can be upgrade.
        //As the first step of upgrading is cheaper than the second step,
        //we make sure that the first upgrade happens before the second upgrade.
        for (candy; candy >= 12; candy = candy - 12) {
            --arr[0];
            ++arr[1];
            ex1 = ex1 + 500;
        }
        for (candy; candy >= 50; candy = candy - 50) {
            --arr[1];
            ++arr[2];
            ex1 = ex1 + 500;
        }
        while (1) {
            candy = candy + arr[2];
            arr[2] = 0;
            for (candy; candy >= 12 && arr[0] > 0; candy = candy - 12) {
                --arr[0];
                ++arr[1];
                ex1 = ex1 + 500;
            }
            for (candy; candy >= 50 && arr[1] > 0 && arr[0] == 0; candy = candy - 50) {
                --arr[1];
                ++arr[2];
                ex1 = ex1 + 500;
            }
            candy = candy + arr[2];
            arr[2] = 0;
            while (candy <= 12) {
                if (arr[1] > 0) {
                    --arr[1], ++candy;
                }        
                else if (arr[0] > 0) {
                    --arr[0];
                    ++candy;
                }
                if (arr[0] == 0 && arr[1] == 0 && candy <= 12) break;
            }
            if (arr[0] == 0 && arr[1] == 0) break;
        }
        //We convert those Pokemons left into candies.
        for (int z = 0; z < 3; ++z) candy += arr[z];
        //To upgrade the level by consuming the certain number of
        //experiences.
        if (ex1 >= 500) {
            level = 1;
            ex2 = ex1 - 500;
        }
        if (ex1 >= 1500) {

            level = 2;

            ex2 = ex1 - 1500;
        }
        if (ex1 >= 3000) {
            level = 3;
            ex2 = ex1 - 3000;
        }
        if (ex1 >= 5000) {
            level = 4;
            ex2 = ex1 - 5000;
        }
        if (ex1 >= 7500) {
            level = 5;
            ex2 = ex1 - 7500;
        }
        if (ex1 >= 10500) {
            level = 6;
            ex2 = ex1 - 10500;
        }
        if (ex1 >= 14000) {
            level = 7;
            ex2 = ex1 - 14000;
        }
        if (ex1 >= 18000) {
            level = 8;
            ex2 = ex1 - 18000;
        }
        printf("剩餘糖果數量:%d，", candy);
        printf("獲取經驗值:%d，", ex1);
        printf("玩家最終等級:%d，", level);
        printf("剩餘經驗值:%d", ex2);
        printf("\n請輸入波波數量(輸入 0 結束): ");
        scanf("%d", &bobo);
        ex1 = 0;
        ex2 = 0;
        while (bobo < 0) {
            printf("輸入錯誤，請重新輸入: ");
            scanf("%d", &bobo);
        }
    }
    printf("剩餘糖果數量:0，");
    printf("獲取經驗值:0，");
    printf("玩家最終等級:0，");
    printf("剩餘經驗值:0");
    return 0;
}
```
# Knowledge library
1. puts
![](https://i.imgur.com/hfUQWmt.png)
2. strcmp(str1, str2) : compare two string
When the first string has the elements in the front several parts and it is greater regardless of size, return 1.
Otherwise return -1. However, if the two string are the same, return 0.
![](https://i.imgur.com/VzhGFff.png)
3. strncmp(str1, str2, n) : compare two string
It compares n number of characters at most
If the first n characters are the same, return 0.
If the second string has the less number of characters than n and all characters are the same before n but the first string having more, return 4. strcpy(str1, const str2) : return a pointer and the first string must have enough space for copying.
![](https://i.imgur.com/RVJJabU.png)
4. strncpy(str1, const str2, n) : copying str2's n number of characters.
5. strcat(str, const str2): return a pointer pointing to the address of the first element in string 1. 
![](https://i.imgur.com/qXsnxtW.png)
6. strncat(str, const str2, n)
7. strrev(str): reverse the every characters in the string.

![](https://i.imgur.com/ldN8KEQ.png)

![](https://i.imgur.com/mMxqLD1.png)
8. sprintf(str, "%d %x...", integer):
Convert a integer into a string.
#include<stdio.h>
![](https://i.imgur.com/xYZoJKE.png)
![](https://i.imgur.com/tDfYcCN.png)
9. strtok(char *str, const char *delim)
We could use string delim to divide the targeted string into several parts with that delim string.
![](https://i.imgur.com/dLnQ1NJ.png)

# some important points
## char *ptr = "...":
  it forms a read-only string so that it will generate error if changing its value.
## passing an two dimensional array
![](https://i.imgur.com/5KlVF4g.png)
## Enum
![](https://i.imgur.com/b6gFCvn.png)







