From 7725a63249a9ee8cbdb29656c72b0c360c6afddb Mon Sep 17 00:00:00 2001 From: CDsxy <1773844883@qq.com> Date: Sun, 11 Mar 2018 22:35:24 +0800 Subject: [PATCH 1/9] hand in homework --- level1/p01_runningLetter/runningletter.c | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 level1/p01_runningLetter/runningletter.c 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; +} From e8c2dfe4ba12c7abfe7a90e339a689d9845a5d8e Mon Sep 17 00:00:00 2001 From: CDsxy <1773844883@qq.com> Date: Mon, 12 Mar 2018 22:12:00 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BA=A4?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p02_isPrime/isprime.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 level1/p02_isPrime/isprime.c 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 Date: Sat, 17 Mar 2018 23:25:32 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E8=82=9D=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p08_hanoi/hanoi.c | 29 ++++++++++ level1/p09_maze/maze.c | 121 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 level1/p08_hanoi/hanoi.c create mode 100644 level1/p09_maze/maze.c diff --git a/level1/p08_hanoi/hanoi.c b/level1/p08_hanoi/hanoi.c new file mode 100644 index 00000000..2452b2c7 --- /dev/null +++ b/level1/p08_hanoi/hanoi.c @@ -0,0 +1,29 @@ +#include + +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; + } +} From 82566e88311f41d91ece4fbd71d3f7221c2a1b51 Mon Sep 17 00:00:00 2001 From: CDsxy <1773844883@qq.com> Date: Tue, 20 Mar 2018 08:57:34 +0800 Subject: [PATCH 4/9] Debug of pushbox --- level1/p10_pushBoxes/pushboxes.c | 209 +++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 level1/p10_pushBoxes/pushboxes.c 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; + } +} + From 310686fa2236e170c511d94d241205e2db1dc9b3 Mon Sep 17 00:00:00 2001 From: CDsxy <1773844883@qq.com> Date: Fri, 23 Mar 2018 16:24:29 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E7=BC=96C=E4=BD=BF=E6=88=91=E5=A4=B4?= =?UTF-8?q?=E7=A7=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p03_Diophantus/Diophantus.c | 12 ++ level1/p04_ narcissus/narcissus.c | 22 +++ level1/p05_allPrimes/allprimes.c | 38 ++++ level1/p06_Goldbach/Goldbach.c | 57 ++++++ level1/p10_pushBoxes/pushBoxes1.3.c | 260 ++++++++++++++++++++++++++++ 5 files changed, 389 insertions(+) create mode 100644 level1/p03_Diophantus/Diophantus.c create mode 100644 level1/p04_ narcissus/narcissus.c create mode 100644 level1/p05_allPrimes/allprimes.c create mode 100644 level1/p06_Goldbach/Goldbach.c create mode 100644 level1/p10_pushBoxes/pushBoxes1.3.c diff --git a/level1/p03_Diophantus/Diophantus.c b/level1/p03_Diophantus/Diophantus.c new file mode 100644 index 00000000..cf36f7d9 --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.c @@ -0,0 +1,12 @@ +#include +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 +#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]; //终点数组 +int cnt=1; + +char MAP[WIDE][LEN]; +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); +int 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' ){ + 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(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; + } +} + +int BJudge(struct COD* Bnow,struct COD* BNew){ + char temp; + if(MAP[BNew->x][BNew->y] == ' '){ + temp=MAP[Bnow->x][Bnow->y]; + MAP[Bnow->x][Bnow->y]=MAP[BNew->x][BNew->y]; + MAP[BNew->x][BNew->y]=temp; + *Bnow = *BNew; + } + if(MAP[BNew->x][BNew->y] == 'X'){ + 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; + } + return cnt; +} + +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; + } +} + From 17ea34fbbde5bba554756d43ed72d4b91229f2ef Mon Sep 17 00:00:00 2001 From: CDsxy <1773844883@qq.com> Date: Sat, 24 Mar 2018 00:08:28 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=8E=A8=E7=AE=B1?= =?UTF-8?q?=E5=AD=90=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p10_pushBoxes/pushBoxes1.3.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/level1/p10_pushBoxes/pushBoxes1.3.c b/level1/p10_pushBoxes/pushBoxes1.3.c index b31e03e6..c89ed38c 100644 --- a/level1/p10_pushBoxes/pushBoxes1.3.c +++ b/level1/p10_pushBoxes/pushBoxes1.3.c @@ -192,14 +192,19 @@ int GetPlayerCommand(void){ return c; } -void Judge(int twd,struct COD* now,struct COD* New){ //判断人的移动 +void Judge(int twd,struct COD* now,struct COD* New){ //判断人的移动 char temp; - if(MAP[New->x][New->y] == ' ' || MAP[New->x][New->y] == 'X' ){ + 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]='o'; + MAP[now->x][now->y]=' '; + *now = *New; + } if(cnt){ if(MAP[New->x][New->y] == 'B'){ struct COD *Bnow=New; From 3abf4344f89b54ccf632abf107e4016b41668771 Mon Sep 17 00:00:00 2001 From: CDsxy <1773844883@qq.com> Date: Sun, 25 Mar 2018 09:05:31 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=89=88=E6=8E=A8?= =?UTF-8?q?=E7=AE=B1=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p10_pushBoxes/pushBoxes1.3.c | 84 +++++++++++------------------ 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/level1/p10_pushBoxes/pushBoxes1.3.c b/level1/p10_pushBoxes/pushBoxes1.3.c index c89ed38c..829b5576 100644 --- a/level1/p10_pushBoxes/pushBoxes1.3.c +++ b/level1/p10_pushBoxes/pushBoxes1.3.c @@ -1,34 +1,34 @@ #include -#include +#include //getch的原型 #include #include #define WIDE 9 #define LEN 10 -#define Boxpush MAP #define ENDJUDGE(c) MAP[end[c].x][end[c].y] != 'B' //终点判断 -struct COD{ +struct COD{ //指明坐标的结构 int x; int y; }; -struct COD end[4]; //终点数组 -int cnt=1; +struct COD end[4]; //终点数组记录每一关卡的终点 +int cnt=1; //用于异常检测 -char MAP[WIDE][LEN]; +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); -int BJudge(struct COD* Bnow,struct COD* BNew); +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); +void Put(void);//输出二维数组的当前值 int main(void){ - struct COD* now; + struct COD* now; struct COD loc=Initmap_1(); now = &loc; @@ -37,7 +37,7 @@ int main(void){ PlayerMove(GetPlayerCommand(),now); } Put(); - printf("you win!,turn to level 2\n"); + printf("you win!,turn to level 2!\n"); Sleep(1000); system("cls"); @@ -89,14 +89,18 @@ void BuildrBarrier(int st,int end,int row){ } } -struct COD Initmap_1(void){ //初始化第一关地图 - int i,j; - struct COD start; +void Initmap(void){ + int i,j; for(i=1;ix][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'){ + if(MAP[New->x][New->y] == ' ' || MAP[New->x][New->y] == 'X' ){ MAP[New->x][New->y]='o'; MAP[now->x][now->y]=' '; - *now = *New; - } + *now = *New; + } if(cnt){ if(MAP[New->x][New->y] == 'B'){ struct COD *Bnow=New; @@ -213,7 +198,7 @@ int GetPlayerCommand(void){ cnt=1; } } - for(int i=0;i<4;i++){ //避免X被吞掉 + for(int i=0;i<4;i++){ //回复X的值 if(MAP[end[i].x][end[i].y] == ' '){ MAP[end[i].x][end[i].y] = 'X'; } @@ -232,15 +217,9 @@ void PlayerMove(int twd,struct COD* now){ } } -int BJudge(struct COD* Bnow,struct COD* BNew){ +void BJudge(struct COD* Bnow,struct COD* BNew){ char temp; - if(MAP[BNew->x][BNew->y] == ' '){ - temp=MAP[Bnow->x][Bnow->y]; - MAP[Bnow->x][Bnow->y]=MAP[BNew->x][BNew->y]; - MAP[BNew->x][BNew->y]=temp; - *Bnow = *BNew; - } - if(MAP[BNew->x][BNew->y] == 'X'){ + 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; @@ -248,10 +227,9 @@ int BJudge(struct COD* Bnow,struct COD* BNew){ if(MAP[BNew->x][BNew->y] == '#' || MAP[BNew->x][BNew->y] == 'B'){ cnt = 0; } - return cnt; } -void BoxMove(int Btwd,struct COD* Bnow){ //箱子移动 +void BoxMove(int Btwd,struct COD* Bnow){ struct COD m =*Bnow; struct COD *BNew=&m; switch(Btwd){ From 504962912318dcd0f1d7c0e6836e4aa1db53ba82 Mon Sep 17 00:00:00 2001 From: CDsxy <1773844883@qq.com> Date: Mon, 2 Apr 2018 16:42:52 +0800 Subject: [PATCH 8/9] =?UTF-8?q?level=201=E9=98=B6=E6=AE=B5=E6=80=A7?= =?UTF-8?q?=E8=83=9C=E5=88=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p11_linkedList/linkedList.c | 83 ++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 level1/p11_linkedList/linkedList.c 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; + } +} From 16e2a4bef3bff0ae82f6e37a15c095877c35916a Mon Sep 17 00:00:00 2001 From: CDsxy <1773844883@qq.com> Date: Sun, 15 Apr 2018 10:52:27 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E3=80=82=E3=80=82=E6=95=88=E7=8E=87?= =?UTF-8?q?=E6=9E=81=E5=85=B6=E4=BD=8E=E4=B8=8B=E7=9A=84=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level2/GA/GA.c | 198 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 level2/GA/GA.c 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