---
# System prepended metadata

title: 程式培訓共筆

---

# 程式培訓共筆
[toc]
# a227: 河內之塔
> 題目連結: [a227 河內之塔](https://zerojudge.tw/ShowProblem?problemid=a227)
```c=
#include <stdio.h>

void ta(int n,char start,char temp,char end) {
    if(n<1) return;
    ta(n-1,start,end,temp);
    printf("move %d from %c to %c\n",n,start,end);
    ta(n-1,temp,start,end);
}
int main() {
	int n;
    while(EOF!=scanf("%d",&n)) {
        ta(n,'A','B','C');
    }
    return 0;
}
```
# a020: 身分證檢驗
> 題目連結: [a020 身分證檢驗](https://zerojudge.tw/ShowProblem?problemid=a020)
```c=
#include <stdio.h>
#include <stdlib.h>

int check(char* id) {
    int i;
    int t[] = {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};
    int ans=0;
    int j=t[id[0]-'A'];
    ans+=j/10+(j%10)*9;
    for (i=1;i<9;i++) {
        ans+=(id[i]-'0')*(9-i);
    }
    ans+=id[9]-'0';
    return (id[1]=='1' || id[1]=='2') ? ((ans%10==0) ? 1 : 0) : 0;
}
int main() {
    char id[11];
    while(EOF!=scanf("%s",id)) {
        printf("%s\n",check(id)?"real":"fake");
    }
    return 0;
}
```
# a022: 迴文
> 題目連結: [a022 迴文](https://zerojudge.tw/ShowProblem?problemid=a022)
```c=
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char x=0;
    char in[1000]={0};
    while(EOF!=scanf("%s",in)) {
        char ni[1000]={0};
        int c=0,n=0;
        for(int i=0;i<1000;i++){
            if(in[999-i]!=x) ni[c++]=in[999-i];
        }
        for(int i=0;i<1000;i++) {
            if(in[i]!=ni[i]) {
                n=1;
                break;
            }
        }
        if(n) printf("no");
        else printf("yes");
        printf("\n");
        for(int i=0;i<1000;i++) in[i]=0;
    }
    return 0;
}
```
# d570: 神龍見首不見尾
> 題目連結: [d570 神龍見首不見尾](https://zerojudge.tw/ShowProblem?problemid=d570)
```c=
#include <stdio.h>

void SL(int n) {
	printf("%d\n",n);
	if(n/10>=1) SL(n/10);
}
int main() {
	int n;
    while(EOF!=scanf("%d",&n)) {
		SL(n);
    }
    return 0;
}
```
# a417: 螺旋矩陣
> 題目連結: [a417 螺旋矩陣](https://zerojudge.tw/ShowProblem?problemid=a417)
```c=
#include <stdio.h>

int main()
{
    int t,m,n,i,j,p,x,y,a;
    int dx[]={0,1,0,-1};
    int dy[]={1,0,-1,0};
    while(EOF!=scanf("%d",&t)){
        while(t--) {
            int R[100][100]={0};x=0;y=0;a=1;
            scanf("%d%d",&n,&m);
			for(i=n-1;i>0;i-=2) {
				for(j=0;j<4;j++) {
					for(p=0;p<i;p++) {
						R[x][y]=a++;
						x+=dx[j];
						y+=dy[j];
					}
				}
				x++;y++;
			}
			if(n%2!=0) R[n/2][n/2]=a;
			for(int y=0;y<n;y++){
                for(int x=0;x<n;x++){
                    if(m==1) printf("%5d",R[y][x]);
                    else printf("%5d",R[x][y]);
                }
                printf("\n");
            }
            printf("\n");
        }
    }
	return 0;
}
```
# a054: 電話客服中心
> 題目連結: [a054 電話客服中心](https://zerojudge.tw/ShowProblem?problemid=a054)
```c=
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int i;
  char n[9];
  while(EOF!=scanf("%s",n)) {
    int a[]={1,0,9,8,7,6,5,4,9,3,2,2,1,0,8,9,8,7,6,5,4,3,1,3,2,0};
    int d[9]={0},num=0;
    for(i=0;i<9;i++) {
      d[i]=n[i]-48;
      num+=d[i]*(8-i);
    }
    num+=d[8];
    num=(10-num%10)%10;
    for(i=0;i<26;i++) {
      if(a[i]==num) printf("%c",i+65);
    }
    printf("\n");
  }
  return 0;
}
```
# e969: 大吃大喝 (Big eater)
> 題目連結: [e969: 大吃大喝 (Big eater)](https://zerojudge.tw/ShowProblem?problemid=e969)
```c=
#include <stdio.h>
#include <stdlib.h>

void eat(int n,int m,int k,int t)
{
    n-=(k ? 55 : 32);
    if(n>0) {
        printf("%d: Wayne %s, and now he has %d %s.\n",t,k ? "drinks a Corn soup" : "eats an Apple pie",n,n==1?"dollar":"dollars");
        t+=m;
        k=(k+1)%2;
        eat(n,m,k,t);
    } else if(n==0) {
        printf("%d: Wayne %s, and now he doesn't have money.\n",t,k ? "drinks a Corn soup" : "eats an Apple pie");
    }
}

int main()
{
    int n,m,k;
    while(EOF!=scanf("%d%d%d",&n,&m,&k)) {
        if(k?n<55:n<32) {ˋㄇ
            printf("Wayne can't eat and drink.\n");
        } else {
            eat(n,m,k,0);
        }
    }
    return 0;
}
```
# a870: List Maker
> 題目連結: [a870: List Maker](https://zerojudge.tw/ShowProblem?problemid=a870)
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct XXX NODE;
struct XXX {
	char str[20];
	NODE *p;
};
void show(NODE *s) {
	while(s) {
        printf("%s ",s -> str);
		s = s -> p;
	}
	printf("\n");
}
int main() {
    NODE *head,*x,*s;
    char A[10];
    char N[20];
    head = NULL;
    while(EOF != scanf("%s",A)) {
        if(strcmp(A,"ADD") == 0) {
            x = malloc(sizeof(NODE));
            scanf("%s",x -> str);
            if(head == NULL) {
                x -> p = NULL;
                head = x;
            } else {
                s = head;
                while(s -> p != NULL) {
                    s = s -> p;
                }
                x -> p = s -> p;
                s -> p = x;
            }
        } else if(strcmp(A,"INSERT") == 0) {
            x = malloc(sizeof(NODE));
            scanf(" %s %s",x -> str,N);
            if(strcmp(head -> str,N) == 0) {
                x -> p = head;
                head = x;
                continue;
            }
            s = head;
            while(strcmp(s -> p -> str,N)) {
                s = s -> p;
            }
            x -> p = s -> p;
            s -> p = x;
        } else if (strcmp(A,"REMOVE") == 0) {
            char X[20];
            NODE *del;
            scanf(" %s",X);
            if(strcmp(head -> str,X) == 0) {
                del = head;
                head = head -> p;
                free(del);
                continue;
            }
            s = head;
            while(strcmp(s -> p -> str,X)) {
                if(s -> p == NULL) break;
                s = s -> p;
            }
            del = s -> p;
            s -> p = s -> p -> p;
            free(del);
        } else {
            show(head);
        }
    }
    return 0;
}
```
# a225: 明明愛排列
> 題目連結: [a225: List Maker](https://zerojudge.tw/ShowProblem?problemid=a225)
```c=
#include <stdio.h>
#include <stdlib.h>

int comp(int a,int b) {
    if(a%10==b%10 && a>b || a%10<b%10) return 0;
    return 1;
}
int main()
{
    int n,i,j,t,num[1000];
    while(EOF!=scanf("%d",&n)) {
        for(i=0;i<n;i++) {
            scanf("%d",&num[i]);
        }
        for(i=n-1;i>=1;i--) {
            for(j=0;j<i;j++) {
                if(comp(num[j],num[j+1])) {
                    t=num[j];
                    num[j]=num[j+1];
                    num[j+1]=t;
                }
            }
        }
        for(i=0;i<n;i++) {
            printf("%d ",num[i]);
        }
        printf("\n");
    }
    return 0;
}
```