diff --git a/level1/p01_runningLetter/runningletter.c b/level1/p01_runningLetter/runningletter.c new file mode 100644 index 00000000..fc3612f5 --- /dev/null +++ b/level1/p01_runningLetter/runningletter.c @@ -0,0 +1,27 @@ +#include +#include +#include +#define WIDE 110 + +int main(void){ + char s[20]; + int x=0; + int dir=1; + int i; + + scanf("%s",s); + while(1){ + system("cls"); + for(i=0;i<=x;i++){ + printf(" "); + } + puts(s); + x += dir; + if(x == WIDE-1) + dir=-1; + else if(x==0) + dir=1; + } + + return 0; +} diff --git a/level1/p02_isPrime/isprime.c b/level1/p02_isPrime/isprime.c new file mode 100644 index 00000000..4ad0b278 --- /dev/null +++ b/level1/p02_isPrime/isprime.c @@ -0,0 +1,26 @@ +#include +int main(void){ + int x; + int i; + int cnt=0; + scanf("%d",&x); + if( x == 2){ + cnt =1; + } + if(x == 1){ + cnt =0; +} + for(i=2;i +int main(void){ + double x=1; + while(1){ + if(x == x/6+x/12+x/7+5+x/2+4) + break; + x++; + } + + printf("The age of Diophantus is %f\n",x-4.0); + +} diff --git a/level1/p04_ narcissus/narcissus.c b/level1/p04_ narcissus/narcissus.c new file mode 100644 index 00000000..2a2877ff --- /dev/null +++ b/level1/p04_ narcissus/narcissus.c @@ -0,0 +1,22 @@ +#include +#include +#define MAX 1000 +#define MIN 100 +#define third(c) pow(c,3) +int main(void){ + int i; + int a,x,y,z; + + for(i=100;i<1000;i++){ + a=i/10; + z=i%10; + y=a%10; + x=i/100; + int t=third(x)+third(y)+third(z); + + if(i == t ) + printf("%d ",i); + } + printf("\n"); + return 0; +} diff --git a/level1/p05_allPrimes/allprimes.c b/level1/p05_allPrimes/allprimes.c new file mode 100644 index 00000000..cb7ad454 --- /dev/null +++ b/level1/p05_allPrimes/allprimes.c @@ -0,0 +1,38 @@ +#include +#include +#define MAX 1000 + +int isprime(int a); + +int main(void){ + clock_t t1,t2; + t1= clock(); + int i; + for(i=2;i +#define MAX 100 + +int isprime(int a); +int main(void){ + int i; + int x=0; + int a[25]={0,}; + + for(i=1;i + +void hanoi(int num,char start,char end,char temp); +void put(char a,char b); + +int main(void){ + int numOfdisk=0; + scanf("%d",&numOfdisk); + + const char s = 'A', e = 'B' ,t = 'C'; + hanoi(numOfdisk,s,e,t); + + return 0; +} + +void hanoi(int num,char start,char end,char temp){ + if(num == 1){ + put(start,end); + } + else{ + hanoi(num-1,start,temp,end); + put(start,end); + hanoi(num-1,temp,end,start); + } +} + +void put(char a,char b){ + printf("Move %c to %c\n",a,b); +} diff --git a/level1/p09_maze/maze.c b/level1/p09_maze/maze.c new file mode 100644 index 00000000..649c2dd9 --- /dev/null +++ b/level1/p09_maze/maze.c @@ -0,0 +1,121 @@ +#include +#include //kbhit的原型 +#include +#define WIDE 15 +#define LEN 17 + +struct COD{ + int x; + int y; +}; + +char maze[WIDE][LEN]; +void Initmap(void); +void BuildrBarrier(int st,int end,int row); +void BuildlBarrier(int st,int end,int line);//辅助造墙函数 +void PlayerMove(int twd,struct COD* now); +int GetPlayerCommand(void); +void Judge(struct COD* now,struct COD* New); + +int main(void){ + struct COD* now; + struct COD loc={1,1,}; + now = &loc; + + Initmap(); + while(now->y < 16){ + system("cls"); + int i,j; + for(i=0;ix][New->y] != '#'){ + temp=maze[now->x][now->y]; + maze[now->x][now->y]=maze[New->x][New->y]; + maze[New->x][New->y]=temp; + *now= *New; + } +} + +void PlayerMove(int twd,struct COD* now){ + struct COD n =*now; + struct COD *New=&n; + switch(twd){ + case -1: break; + case 0: n.y++;Judge(now,New);break; + case 1: n.x++;Judge(now,New);break; + case 2: n.y--;Judge(now,New);break; + case 3: n.x--;Judge(now,New);break; + } +} diff --git a/level1/p10_pushBoxes/pushBoxes1.3.c b/level1/p10_pushBoxes/pushBoxes1.3.c new file mode 100644 index 00000000..829b5576 --- /dev/null +++ b/level1/p10_pushBoxes/pushBoxes1.3.c @@ -0,0 +1,243 @@ +#include +#include //getch的原型 +#include +#include +#define WIDE 9 +#define LEN 10 +#define ENDJUDGE(c) MAP[end[c].x][end[c].y] != 'B' //终点判断 + +struct COD{ //指明坐标的结构 + int x; + int y; +}; +struct COD end[4]; //终点数组记录每一关卡的终点 +int cnt=1; //用于异常检测 + +char MAP[WIDE][LEN]; +void Initmap(void); //将数组全部初始化为空格 +struct COD Initmap_1(void); +struct COD Initmap_2(void); +struct COD Initmap_3(void); +void BuildrBarrier(int st,int end,int row); +void BuildlBarrier(int st,int end,int line);//辅助造墙函数 +void PlayerMove(int twd,struct COD* now); +int GetPlayerCommand(void); //读取用户命令 +void Judge(int twd,struct COD* now,struct COD* New);//判断人的移动 +void BJudge(struct COD* Bnow,struct COD* BNew);//判断箱子移动 +void BoxMove(int Btwd,struct COD* Bnow); +void Put(void);//输出二维数组的当前值 + +int main(void){ + struct COD* now; + struct COD loc=Initmap_1(); + now = &loc; + + while(ENDJUDGE(0) || ENDJUDGE(1) || ENDJUDGE(2) || ENDJUDGE(3) ){ + Put(); + PlayerMove(GetPlayerCommand(),now); + } + Put(); + printf("you win!,turn to level 2!\n"); + Sleep(1000); + + system("cls"); + loc = Initmap_2(); + now = &loc; + while(ENDJUDGE(0) || ENDJUDGE(1) || ENDJUDGE(2) ){ + Put(); + PlayerMove(GetPlayerCommand(),now); + } + Put(); + printf("you win!,turn to level 3!\n"); + Sleep(1000); + + system("cls"); + loc = Initmap_3(); + now = &loc; + while(ENDJUDGE(0) || ENDJUDGE(1) || ENDJUDGE(2) || ENDJUDGE(3) ){ + Put(); + PlayerMove(GetPlayerCommand(),now); + } + Put(); + printf("you win!,Here is your score list"); + + return 0; +} + +void Put(void){ + system("cls"); + int i,j; + for(i=0;ix][New->y] == ' ' || MAP[New->x][New->y] == 'X' ){ + MAP[New->x][New->y]='o'; + MAP[now->x][now->y]=' '; + *now = *New; + } + if(cnt){ + if(MAP[New->x][New->y] == 'B'){ + struct COD *Bnow=New; + BoxMove(twd,Bnow); + PlayerMove(twd,now); + cnt=1; + } + } + for(int i=0;i<4;i++){ //回复X的值 + if(MAP[end[i].x][end[i].y] == ' '){ + MAP[end[i].x][end[i].y] = 'X'; + } + } +} + +void PlayerMove(int twd,struct COD* now){ + struct COD n =*now; + struct COD *New=&n; + switch(twd){ + case -1: break; + case 0: n.y++;Judge(twd,now,New);break; + case 1: n.x++;Judge(twd,now,New);break; + case 2: n.y--;Judge(twd,now,New);break; + case 3: n.x--;Judge(twd,now,New);break; + } +} + +void BJudge(struct COD* Bnow,struct COD* BNew){ + char temp; + if(MAP[BNew->x][BNew->y] == 'X' || MAP[BNew->x][BNew->y] == ' '){ + MAP[BNew->x][BNew->y] = 'B'; + MAP[Bnow->x][Bnow->y] = ' '; + *Bnow = *BNew; + } + if(MAP[BNew->x][BNew->y] == '#' || MAP[BNew->x][BNew->y] == 'B'){ + cnt = 0; + } +} + +void BoxMove(int Btwd,struct COD* Bnow){ + struct COD m =*Bnow; + struct COD *BNew=&m; + switch(Btwd){ + case -1: break; + case 0: m.y++;BJudge(Bnow,BNew);break; + case 1: m.x++;BJudge(Bnow,BNew);break; + case 2: m.y--;BJudge(Bnow,BNew);break; + case 3: m.x--;BJudge(Bnow,BNew);break; + } +} + diff --git a/level1/p10_pushBoxes/pushboxes.c b/level1/p10_pushBoxes/pushboxes.c new file mode 100644 index 00000000..a433f4b0 --- /dev/null +++ b/level1/p10_pushBoxes/pushboxes.c @@ -0,0 +1,209 @@ +#include +#include +#include +#include +#define WIDE 9 +#define LEN 10 +#define Boxpush MAP +#define ENDJUDGE(c) MAP[end[c].x][end[c].y] != 'B' //终点判断 + +struct COD{ + int x; + int y; +}; +struct COD end[4]; //终点数组 + +char MAP[WIDE][LEN]; +struct COD Initmap_1(void); +struct COD Initmap_2(void); +void BuildrBarrier(int st,int end,int row); +void BuildlBarrier(int st,int end,int line);//辅助造墙函数 +void PlayerMove(int twd,struct COD* now); +int GetPlayerCommand(void); +void Judge(int twd,struct COD* now,struct COD* New); +void BJudge(struct COD* now,struct COD* New); +void BoxMove(int Btwd,struct COD* Bnow); +void Put(void); + +int main(void){ + struct COD* now; + struct COD loc=Initmap_1(); + now = &loc; + + while(ENDJUDGE(0) || ENDJUDGE(1) || ENDJUDGE(2) || ENDJUDGE(3) ){ + Put(); + PlayerMove(GetPlayerCommand(),now); + } + Put(); + printf("you win!,turn to level 2\n"); + Sleep(1000); + + system("cls"); + loc=Initmap_2(); + now = &loc; + Put(); + + while(ENDJUDGE(0) || ENDJUDGE(1) || ENDJUDGE(2) ){ + Put(); + PlayerMove(GetPlayerCommand(),now); + } + Put(); + printf("you win!,turn to level 3\n"); + Sleep(1000); + + + return 0; +} + +void Put(void){ + system("cls"); + int i,j; + for(i=0;ix][New->y] == ' ' || MAP[New->x][New->y] == 'X' ){ + temp=MAP[now->x][now->y]; + MAP[now->x][now->y]=MAP[New->x][New->y]; + MAP[New->x][New->y]=temp; + *now = *New; + } + if(MAP[New->x][New->y] == 'B'){ + struct COD *Bnow=New; + BoxMove(twd,Bnow); + PlayerMove(twd,now); + } +} + +void PlayerMove(int twd,struct COD* now){ + struct COD n =*now; + struct COD *New=&n; + switch(twd){ + case -1: break; + case 0: n.y++;Judge(twd,now,New);break; + case 1: n.x++;Judge(twd,now,New);break; + case 2: n.y--;Judge(twd,now,New);break; + case 3: n.x--;Judge(twd,now,New);break; + } +} + +void BJudge(struct COD* now,struct COD* New){ + char temp; + if(MAP[New->x][New->y] == ' '){ + temp=MAP[now->x][now->y]; + MAP[now->x][now->y]=MAP[New->x][New->y]; + MAP[New->x][New->y]=temp; + *now = *New; + } + if(MAP[New->x][New->y] == 'X'){ + MAP[New->x][New->y] = 'B'; + MAP[now->x][now->y] = ' '; + *now = *New; + } +} + +void BoxMove(int Btwd,struct COD* Bnow){ //箱子移动 + struct COD m =*Bnow; + struct COD *BNew=&m; + switch(Btwd){ + case -1: break; + case 0: m.y++;BJudge(Bnow,BNew);break; + case 1: m.x++;BJudge(Bnow,BNew);break; + case 2: m.y--;BJudge(Bnow,BNew);break; + case 3: m.x--;BJudge(Bnow,BNew);break; + } +} + diff --git a/level1/p11_linkedList/linkedList.c b/level1/p11_linkedList/linkedList.c new file mode 100644 index 00000000..c46031c5 --- /dev/null +++ b/level1/p11_linkedList/linkedList.c @@ -0,0 +1,83 @@ +#include +#include + +struct numlist{ + int value; + struct numlist* next; +}; + +struct numlist *relist(struct numlist *head); +void cleanup(struct numlist *head); + +int main(void){ + struct numlist* head=NULL; + struct numlist* prev,*now; + int cnt; //暂存value的值 + while(1){ + char c; + if ((c=getchar())=='\n'){ + break; + } + cnt=c-'0'; + while ((c=getchar())!='\n'){ + cnt=cnt*10+c-'0'; + } + now=(struct numlist* )malloc(sizeof(struct numlist)); + if( head == NULL){ + head = now; + }else{ + prev->next = now; + } + now->next = NULL; + now->value = cnt; + prev=now; + } + + struct numlist *temp=relist(head); //逆序链表 + int num=1; + int status=0; + now=temp; + while(now){ + if(now->value == 5){ + status=1; + printf("%d ",num); + } + now=now->next; + num++; //计数 + } + printf("\n"); + + if(status == 1){ + printf("Above is the order of list which's value equals 5!\n"); + }else{ + printf("-1 \n"); + } + cleanup(head); //清理malloc的内存 + return 0; +} + +struct numlist* relist(struct numlist *head){ + struct numlist *Prev,*Now,*Next; + struct numlist *rhead; + Prev=NULL; + Now=head; + Next=head->next; + while(Next){ + Now->next=Prev; + Prev=Now; + Now=Next; + Next=Next->next; + } + Now->next=Prev; + rhead=Now; + + return rhead; +} + +void cleanup(struct numlist *head){ + struct numlist *now=head; + while(now){ + free(now); + head=now->next; + } +} diff --git a/level2/GA/GA.c b/level2/GA/GA.c new file mode 100644 index 00000000..55647734 --- /dev/null +++ b/level2/GA/GA.c @@ -0,0 +1,198 @@ +#include +#include +#include +#include +#define CROSSOVER_RATE 0.7 //交叉概率 +#define MUTATE_RATE 0.001 //突变概率 +#define POP_SIZE 120 //种群大小 +#define CHROMO_SIZE 60 //染色体最大长度 +#define MAXGENERATION 1000 //最大繁衍代数 +#define MAP_LEN 21 +#define MAP_WIDE 12 + +struct path{ + int chromo[CHROMO_SIZE]; + double fiteness; +}; +struct COD{ //坐标结构 + int x; + int y; +}; +char map[MAP_WIDE][MAP_LEN]; +struct path baby[POP_SIZE+1]; +struct path baby1,baby2; + +void Initmap(void); +void coding(int *code); +double FitenessJudge(int* chromo); +void JudgeWall(struct COD* now,struct COD* New); +int SelectionParents(struct path*); +void Crossover(struct path,struct path); +void Mutate(int *code); +int choosebest(const struct path* pop); + +int main(void){ + Initmap(); + struct path pop[POP_SIZE]; + srand((unsigned int) (time(NULL))); + int i,j,k; + for(i=0;ix][New->y] == ' ' ){ + *now = *New; + } +} + +int SelectionParents(struct path *pop){ //轮盘赌选法 + int parent=0; + int i,j; + double total,ctotal=0; + for(i=0;ia){ + parent=j; + break; + } + } + return parent; +} + +void Crossover(struct path dad,struct path mum){ + double pro1=rand()/(double)(RAND_MAX); + int i,j; + if(pro1>CROSSOVER_RATE){ + baby1=dad; + baby2=mum; + return ; + } + int breakpoint=rand()%(CHROMO_SIZE-1); + int temp[CHROMO_SIZE]; + for(i=0;iMUTATE_RATE){ + code[i]=!code[i]; + } + } +} + +int choosebest(const struct path* pop){ + int i; + int best=0; + for(i=1;i