diff --git a/level0/level0.c b/level0/level0.c new file mode 100644 index 00000000..f395605c --- /dev/null +++ b/level0/level0.c @@ -0,0 +1,446 @@ +### C0 +#include +int main() +{ + printf("Hello world! I'm Ceng Feng!"); + return 0; +} + +### C1 +(1) +#include +int main() +{ + double a; + int b; + scanf("%lf",&a); + if(a>0) + printf("It's a positive number."); + else if(a==0) + printf("It's neiher a positive number nor a negative one"); + else + printf("It's a negative number."); + b=(int)a; + if(a==b) + { + if(b%2==0) + printf("\nIt's an even number."); + else if(b%2==1) + printf("\nIt's an odd number"); + } + else printf("\nIt's not an even number or an odd one."); + return 0; +} + +(2) +#include +int main() +{ + int a; + scanf("%d",&a); + if(a%13==0) + printf("%d is the multiple of 13.",a); + else + printf("%d is not the multiple of 13.",a); + return 0; +} + +(3) +#include +int main() +{ + int a,first=1; + scanf("%d",&a); + if(a%4==0) + { + if(a%100==0) + first=0; + else + first=1; + if(a%400==0) + first=1; + } + if(first) + printf("%d is a leap year.",a); + else + printf("%d is not a leap year.",a); + return 0; +} + +(4) +#include +int main() +{ + int a,b,max; + scanf("%d %d",&a,&b); + if(a>b) + max=a; + else + max=b; + printf("The max number is %d.",max); + return 0; +} + +(5) +#include +int main() +{ + int a,b,c,max; + scanf("%d %d %d",&a,&b); + max=a; + if(b>max) + max=b; + if(c>max) + max=c; + printf("The max number is %d.",max); + return 0; +} + +(6) +#include +#define INF 1000000 +int a[1000000]; +int main() +{ + int i,n,max=-INF; + scanf("%d",&n); + for(i=0;imax) + max=a[i]; + } + printf("The max number is %d.",max); + return 0; +} + +### C2 +(1) +#include +int main() +{ + int i; + for(i=0;i<10000;i++) + printf("你好! "); + return 0; +} + +(2) +#include +int main() +{ + system("color 0a"); + while(1) + { + printf("0 1"); + } + return 0; +} + +(3) +#include +int main() +{ + system("color 0a"); + while(1) + { + printf("%d\t",rand()/1000); + } + return 0; +} + +(4) +#include +int main() +{ + int i,first=1; + for(i=2;i<=100;i++) + { + if(i%2)continue; + else + { + if(!first)printf(" %d",i); + else printf("%d",i); + first=0; + } + } + return 0; +} + +(5) +#include +int main() +{ + int i,first=1; + for(i=1;i<=100;i++) + { + if(i%3==0&&i%5!=0) + { + if(!first) + printf(" %d",i); + else + { + printf("%d",i); + first=0; + } + } + } + return 0; +} + +(6) +#include +int main() +{ + int i,first=1,sum=0; + for(i=1;i<=100;i++) + { + if(i%7==0||i%10==7) + { + if(!first) + printf(" %d",i); + else + { + printf("%d",i); + first=0; + } + sum+=i; + } + } + printf("\n%d",sum); + return 0; +} + +(7) +#include +int main() +{ + int i,j; + for(i=1;i<=9;i++) + { + for(j=1;j<=9;j++) + { + if(j!=9) + { + if(i*j<10) + printf("%d*%d=%d ",i,j,i*j); + else + printf("%d*%d=%d ",i,j,i*j); + } + else printf("%d*%d=%d",i,j,i*j); + } + printf("\n"); + } + return 0; +} + +(8) +#include +int a[100000]; +int main() +{ + int i,n; + scanf("%d",&n); + for(i=0;i=0;i--) + { + if(i==n-1) + printf("%d",a[i]); + else + printf(" %d",a[i]); + } + return 0; +} + +(9) +#include +int a[100000]; +int main() +{ + int i,j,first=1; + for(i=0;i<5;i++) + scanf("%d",&a[i]); + for(i=0;i<9;i++) + { + first=1; + for(j=0;j<5;j++) + if(i==a[j]) + { + first=0;break; + } + if(first) + printf("%d\n",i); + } + return 0; +} + +(10) +#include +int a[100]; +int main() +{ + int i,j,first=1; + for(i=0;i<=9;i++)a[i]=0; + for(i=0;i<5;i++) + { + scanf("%d",&j); + a[j]+=1; + } + for(i=0;i<=9;i++) + if(a[i]) + { + for(j=1;j<=a[i];j++) + printf("%d ",i); + } + return 0; +} + +###C3 +(1) +#include +int sum(int* p,int n) +{ + int i,all=0; + for(i=0;i +double factorial(int n) +{ + int i; + double ans=1; + for(i=1;i<=n;i++) + { + ans=ans*i; + } + return ans; +} +int main() +{ + int n; + double ans; + scanf("%d",&n); + ans=factorial(n); + printf("%.0lf",ans); + return 0; +} + +(3) +#include +double a[1000]; +void fbnq(int n) +{ + int i; + for(i=2;i +int isrightangle(double a,double b,double c) +{ + if(a*a+b*b==c*c) + return 1; + else + return 0; +} +int main() +{ + double a,b,c; + int first; + scanf("%d%d%d",&a,&b,&c); + first=isrightangle(a,b,c); + if(first) + printf("That is a right angle triangle."); + else + printf("That is not a right angle triangle."); + return 0; +} + +(5) +#include +char s[1000][1000]; +int isoverlap(int a,int b,int c,int d,int e,int f,int g,int h) +{ + int i,j,left,up,down,right; + for(i=a;i<=c;i++) + for(j=b;j<=d;j++) + s[i][j]+=1; + for(i=a;i<=c;i++) + for(j=b;j<=d;j++) + s[i][j]+=1; + left=bg?c:g; + right=d>h?d:h; + for(i=left;i<=right;i++) + for(j=up;j<=down;j++) + if(s[i][j]==2)return 1; + return 0; +} +int main() +{ + int a,b,c,d,e,f,g,h; + int first; + printf("请输入第一个矩阵坐标:\n"); + scanf("%d%d%d%d",&a,&b,&c,&d); + printf("请输入第二个矩阵坐标:\n"); + scanf("%d%d%d%d",&e,&f,&g,&h); + first=isoverlap(a,b,c,d,e,f,g,h); + if(first) + printf("Overlap"); + else + printf("Not overlap"); + return 0; +} + +(6) +#include +#define rate 6.7201 +double dollartoyuan(double dollar) +{ + double yuan; + yuan=dollar*rate; + return yuan; +} +int main() +{ + double yuan,dollar; + printf("请输入美元数额:\n"); + scanf("%lf",&dollar); + yuan=dollartoyuan(dollar); + printf("您可换取的人民币数额为:%lf",yuan); + return 0; +} diff --git a/level1/p01_runningLetter/runningR.c b/level1/p01_runningLetter/runningR.c new file mode 100644 index 00000000..b2f685a3 --- /dev/null +++ b/level1/p01_runningLetter/runningR.c @@ -0,0 +1,22 @@ +#include +#define WIDTH 119 +int main() +{ + int r=0,i=0,j=0; + while(1) + { + for(i=0;i=0;i--) + { + for(j=i-1;j>=0;j--)printf(" "); + printf("R"); + system("cls"); + } + } + return 0; +} diff --git a/level1/p02_isPrime/isPrime.c b/level1/p02_isPrime/isPrime.c new file mode 100644 index 00000000..322de3a2 --- /dev/null +++ b/level1/p02_isPrime/isPrime.c @@ -0,0 +1,20 @@ +p02 +#include +int main() +{ + int n,i; + int first=1; + scanf("%d",&n); + for(i=2;i*i<=n;i++) + { + if(n%i==0){ + first=0; + break; + } + } + if(first) + printf("n is a prime."); + else + printf("n is not a prime."); + return 0; +} diff --git a/level1/p03_Diophantus/Diophantus.c b/level1/p03_Diophantus/Diophantus.c new file mode 100644 index 00000000..0f6c2846 --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.c @@ -0,0 +1,20 @@ +#include +int main() +{ + int i,j,dezi; + for(i=1;i<=200;i++) + {//age of father + if(i%6==0&&i%12==0&&i%7==0) + { + dezi=i/6+i/12+i/7+5; + for(j=1;j<=100;j++) + {//age of son + if(dezi+j+4==i&&j==i/2&&i%2==0) + { + printf("When his son died, he is %d years old.",i-4); + return 0; + } + } + } + } +} diff --git a/level1/p04_ narcissus/narcissus.c b/level1/p04_ narcissus/narcissus.c new file mode 100644 index 00000000..17556125 --- /dev/null +++ b/level1/p04_ narcissus/narcissus.c @@ -0,0 +1,20 @@ +p04 +#include +int trisum(int n) +{ + int a,b,c; + a=n%10; + b=(n/10)%10; + c=n/100; + return a*a*a+b*b*b+c*c*c; +} +int main() +{ + int a,b,c,i; + for(i=100;i<1000;i++) + { + if(i==trisum(i)) + printf("%d\n",i); + } + return 0; +} diff --git a/level1/p05_allPrimes/allPrimes.c b/level1/p05_allPrimes/allPrimes.c new file mode 100644 index 00000000..906da3f6 --- /dev/null +++ b/level1/p05_allPrimes/allPrimes.c @@ -0,0 +1,27 @@ +p05 +#include +#include +#define maxn 10000000 +clock_t start,stop; +double duration; +int p[maxn+5]; +int main() +{ + int i,j; + for(i=2;i<=maxn;i++) + p[i]=1; + start=clock(); + for(i=2;i<=maxn;i++) + { + if(p[i]) + for(j=2*i;j<=maxn;j=j+i) + p[j]=0; + } + stop=clock(); + duration=((double)(stop-start))/CLK_TCK; + for(i=2;i<=maxn;i++) + if(p[i]) + printf("%d ",i); + printf("\nThe time is %lf seconds",duration); + return 0; +} diff --git a/level1/p06_Goldbach/Goldbach.c b/level1/p06_Goldbach/Goldbach.c new file mode 100644 index 00000000..72734fcb --- /dev/null +++ b/level1/p06_Goldbach/Goldbach.c @@ -0,0 +1,23 @@ +#include +int main() +{ + int p[110]; + int i,j,ans1,ans2; + for(i=2;i<=100;i++)p[i]=1; + for(i=2;i<=100;i++) + { + for(j=2*i;j<=100;j=j+i)p[j]=0; + } + for(i=2;i<=100;i++)if(i%2==0) + { + for(j=2;j<=50;j++) + if(p[j]&&i-j>1&&p[i-j]) + { + ans1=j; + ans2=i-j; + printf("%d=%d+%d\n",i,j,i-j); + break; + } + } + return 0; +} diff --git a/level1/p07_encrypt_decrypt/encrypt_decrypt.c b/level1/p07_encrypt_decrypt/encrypt_decrypt.c new file mode 100644 index 00000000..07e0d430 --- /dev/null +++ b/level1/p07_encrypt_decrypt/encrypt_decrypt.c @@ -0,0 +1,34 @@ +#include +#include +char string[1000]; +void encrypt(char *s); +void decrypt(char *s); +int main() +{ + gets(string); + encrypt(string); + puts(string); + decrypt(string); + puts(string); + return 0; +} +void encrypt(char *s) +{ + int i,len; + len=strlen(s); + for(i=0;i +int count=0; +int hanoi(int n,char A,char B,char C) +{ + if(n==1){ + printf("%c-->%c\n",A,B); + count++; + return; + } + hanoi(n-1,A,C,B); + printf("%c-->%c\n",A,B);count++; + hanoi(n-1,C,B,A); + return count; +} +int main() +{ + int n; + scanf("%d",&n); + int count; + count=hanoi(n,'A','B','C'); + printf("%d",count); + return 0; +} diff --git a/level1/p08_hanoi/hanoi.jpg b/level1/p08_hanoi/hanoi.jpg deleted file mode 100755 index f596da2c..00000000 Binary files a/level1/p08_hanoi/hanoi.jpg and /dev/null differ diff --git a/level1/p09_maze/maze.txt b/level1/p09_maze/maze.txt new file mode 100644 index 00000000..155450f9 --- /dev/null +++ b/level1/p09_maze/maze.txt @@ -0,0 +1,22 @@ +##################### +# # # # +# ### # # # ### +# # # # # # # # +######### # # # ### # +# # # # # # # +# ##### ### # # ### # +# # # # # # +### ##### ####### ### +# # # +# ##### ### ####### # + # # # # # +##### ### ####### # # +# # # # # # +# ##### # # # # # ### +# # # # # # # # # +# ### # # # # ##### # +# # # # # # # # +# # # # # # # # ### # +# # # # # # +##################### + diff --git "a/level1/p09_maze/\350\277\267\345\256\253.c" "b/level1/p09_maze/\350\277\267\345\256\253.c" new file mode 100644 index 00000000..f9ee66b7 --- /dev/null +++ "b/level1/p09_maze/\350\277\267\345\256\253.c" @@ -0,0 +1,106 @@ +#include +#include +#include +char mazemap[50][50]; +void show(); +int x=11,y=0; +int main() +{ + FILE *fin; + fin = fopen("/tmp/maze.txt","r"); + int i,j; + for(i=0;i<21;i++) + { + fgets(mazemap[i],25,fin); + } + show(x,y); + char c; + while((c=getch())!=-1) + { + if(c=='a') + { + if(y>0&&mazemap[x][y-1]!='#') + { + y--; + show(x,y); + } + } + else if(c=='s') + { + if(x<20&&mazemap[x+1][y]!='#') + { + x++; + show(x,y); + } + } + else if(c=='d') + { + if(y<20&&mazemap[x][y+1]!='#') + { + y++; + show(x,y); + } + } + else if(c=='w') + { + if(x>0&&mazemap[x-1][y]!='#') + { + x--; + show(x,y); + } + } + else if(c=='g') + { + show(x,y); + printf("лл룡"); + break; + } + else + { + printf("Ч"); + } + if(x==13&&y==20) + { + printf("ףأɹˣ"); + break; + } + } + + + fclose(fin); + return 0; +} +void show(int x,int y) +{ + system("cls"); + printf("a: s: w: d: g:\n"); + int i,j; + for(i=0;i<21;i++) + { + for(j=0;j<21;j++) + { + if(i==x&&j==y) + { + printf("$"); + continue; + } + printf("%c",mazemap[i][j]); + } + printf("\n"); + } + return; +} + + + + + + + + + + + + + + diff --git a/level1/p10_pushBoxes/pushboxes.c b/level1/p10_pushBoxes/pushboxes.c new file mode 100644 index 00000000..a890be81 --- /dev/null +++ b/level1/p10_pushBoxes/pushboxes.c @@ -0,0 +1,179 @@ +#include +int x,y,boxnumber,cnt,howmany; +int box[100][3],position[100][3]; +char boxmap[25][25]; +void loadfile(); +void loadmap(); +void showmap(); +void where(int a,int b,int c,int d); +int main() +{ + int i; + loadfile(); + loadmap(); + showmap(); + howmany=0;cnt=0; + char c; + while((c=getch())!=-1) + { + if(c=='a') + { + where(0,-1,0,-2); + cnt++; + showmap(); + } + else if(c=='s') + { + where(1,0,2,0); + cnt++; + showmap(); + } + else if(c=='d') + { + where(0,1,0,2); + cnt++; + showmap(); + } + else if(c=='w') + { + where(-1,0,-2,0); + cnt++; + showmap(); + } + else if(c=='g') + { + showmap(); + printf("лл룡"); + break; + } + else if(c=='r') + { + loadfile(); + loadmap(); + showmap(); + howmany=0;cnt=0; + } + else + { + printf("Ч"); + } + for(i=1;i<=boxnumber;i++) + { + if(boxmap[position[i][1]][position[i][2]]=='@') + { + howmany++; + boxmap[position[i][1]][position[i][2]]='#'; + showmap(); + } + } + if(howmany==boxnumber) + { + printf("ףأɹ!"); + FILE *fin; + if(howmany==1) + { + fin = fopen("/tmp/pushboxone.txt","a+"); + } + else + { + fin = fopen("/tmp/pushboxtwo.txt","a+"); + } + fprintf(fin,"˴սķΪ%d\n",cnt); + break; + } + + } + return; +} +void loadfile() +{ + FILE *fin; + int choice; + while(1) + { + printf("ҪսĹؿ(1-2)"); + scanf("%d",&choice); + if(choice==1) + { + fin = fopen("/tmp/pushboxone.txt","r"); + break; + } + else if(choice==2) + { + fin = fopen("/tmp/pushboxtwo.txt","r"); + break; + } + else + { + printf("error!\n"); + } + } + int i,j; + for(i=0;i<21;i++) + { + fgets(boxmap[i],25,fin); + } + fscanf(fin,"%d %d",&x,&y); + fscanf(fin,"%d",&boxnumber); + for(i=1;i<=boxnumber;i++) + { + fscanf(fin,"%d %d",&box[i][1],&box[i][2]); + fscanf(fin,"%d %d",&position[i][1],&position[i][2]); + } + return; +} +void loadmap() +{ + int i; + for(i=1;i<=boxnumber;i++) + { + boxmap[position[i][1]][position[i][2]]='$'; + boxmap[box[i][1]][box[i][2]]='@'; + } + boxmap[x][y]='i'; + return; +} +void showmap() +{ + system("cls"); + printf("a: s: w: d: g: r:\n"); + int i,j; + for(i=0;i<21;i++) + { + for(j=0;j<21;j++) + { + printf("%c",boxmap[i][j]); + } + printf("\n"); + } + return; +} +void where(int a,int b,int c,int d) +{ + if(x+a>=0&&x+a<=20&&y+b>=0&&y+b<=20) + { + if(boxmap[x+a][y+b]!='#') + { + if(boxmap[x+a][y+b]==' ') + { + boxmap[x][y]=' '; + boxmap[x+a][y+b]='i'; + x+=a;y+=b; + } + else if(boxmap[x+a][y+b]=='@') + { + if(x+c>=0&&x+c<=20&&y+d>=0&&y+d<=20) + { + if(boxmap[x+c][y+d]==' '||boxmap[x+c][y+d]=='$') + { + boxmap[x+c][y+d]='@'; + boxmap[x][y]=' '; + boxmap[x+a][y+b]='i'; + x+=a;y+=b; + } + } + } + } + } + return; +} diff --git a/level1/p10_pushBoxes/pushboxone.txt b/level1/p10_pushBoxes/pushboxone.txt new file mode 100644 index 00000000..1d67a54e --- /dev/null +++ b/level1/p10_pushBoxes/pushboxone.txt @@ -0,0 +1,25 @@ +##################### +# # # # +# ### # # # ### +# # # # # # +### ### # # # # ### # +# # # # # # +# ### ### # # ### # +# # # # # +## ## ## ####### ### +# # # +# ### ### ####### # + # # # # +## ### ####### # # +# # # # # # +# # ### # # # # # ### +# # # # # # # +# ### # # # # ##### # +# # # # # # # # +# # # # # # # # ### # +# # # # # +##################### +11 0 +2 3 +19 12 + diff --git a/level1/p10_pushBoxes/pushboxtwo.txt b/level1/p10_pushBoxes/pushboxtwo.txt new file mode 100644 index 00000000..cd63b8fb --- /dev/null +++ b/level1/p10_pushBoxes/pushboxtwo.txt @@ -0,0 +1,26 @@ +##################### +# # +# ### # ### +# # # # # # # # +######### # # # ### # +# # # # # # # +# ##### ### # # ### # +# # # # # # +### ##### # ## ## ### +# # # +# ##### ### ### # # # +# # # # # # +##### ### ####### # # +# # # # # +# ##### # # # # # # # +# # # # # # # # +# ### # # # # ##### # +# # # # # # # # +# # # # # # # # ### # +# # # # # +##################### +13 20 +2 16 +1 1 +9 14 +19 19 diff --git a/level1/p11_linkedList/linkedlist.c b/level1/p11_linkedList/linkedlist.c new file mode 100644 index 00000000..a553629a --- /dev/null +++ b/level1/p11_linkedList/linkedlist.c @@ -0,0 +1,99 @@ +#include +#include +int cnt=0; +struct node{ + int value; + struct node *p; +}; +struct node *head,*tail; +void add(); +void creat(); +void showvalue(); +void negate(); +int find(int l,int r); +int main() +{ + int i; + creat(); + for(i=1;i<10;i++)add();// + showvalue();// + printf("\n"); + negate();// + showvalue();//ʾֵ + printf("\n"); + int ans=find(0,cnt);// + printf("%d\n",ans); + int ans2=find(ans+1,cnt); + printf("%d",ans2); + return 0; +} +void creat() +{ + head=(struct node*)malloc(sizeof(struct node)); + head->value=cnt;cnt++; + head->p=NULL; + tail=head; + return; +} +void add() +{ + struct node *newone; + newone=(struct node*)malloc(sizeof(struct node)); + newone->value=cnt;cnt++; + newone->p =NULL; + tail->p =newone; + tail=newone; + return; +} +void showvalue() +{ + int i; + struct node *newone=head; + for(i=0;ivalue); + newone=newone->p ; + } + return; +} +void negate() +{ + struct node *newone=NULL,*newtwo=head,*newthree; + int i=0; + while(ip ; + newtwo->p =newone; + newone=newtwo; + newtwo=newthree; + i++; + } + newthree=head; + head=tail; + tail=newthree; + return; +} +int find(int l,int r) +{ + if(l>r)return -1; + int i; + struct node *newone=head; + for(i=0;ip ; + } + int ans=-1; + for(i=l;ivalue ==5) + { + ans=i; + break; + } + newone=newone->p ; + } + return ans; +} + + + diff --git a/level1/p12_warehouse/warehouse/rukou.c b/level1/p12_warehouse/warehouse/rukou.c new file mode 100644 index 00000000..e2b323be --- /dev/null +++ b/level1/p12_warehouse/warehouse/rukou.c @@ -0,0 +1,53 @@ +#include +#include +#include"warehouse.h" +int main() +{ + int n; + cnt=0; + duru(); + xianshi(); + while(1) + { + printf("˵\n1.ʾб\n2.\n3.\n4.˳ʾ\n"); + printf(""); + scanf("%d",&n); + switch(n) + { + case 1: + xianshi(); + break; + case 2: + ruku(); + break; + case 3: + chuku(); + break; + case 4: + tuichu(); + break; + default: + break; + } + if(n==4){ + break; + } + if(n>4){ + printf("\n\n"); + continue; + } + } + return 0; +} + + + + + + + + + + + + diff --git a/level1/p12_warehouse/warehouse/warehouse.c b/level1/p12_warehouse/warehouse/warehouse.c new file mode 100644 index 00000000..8e09ea79 --- /dev/null +++ b/level1/p12_warehouse/warehouse/warehouse.c @@ -0,0 +1,125 @@ +#include +#include +#include"warehouse.h" +void xianshi() +{ + int i; + printf("б\n"); + printf(":ͺ\\\n"); + for(i=1;i<=cnt;i++) + { + printf("%d:%s\\%d\n",i,xinghao[i],shuliang[i]); + } + printf("ʾ\n\n\n"); + return; +} + +void ruku() +{ + printf("ͺż\n"); + cnt++; + scanf("%s %d",xinghao[cnt],&shuliang[cnt]); + printf("\n\n\n"); + return; +} + +void chuku() +{ + int chukushu; + printf("ż\n"); + int num; + scanf("%d",&num); + scanf("%d",&chukushu); + if(num>cnt) + { + printf("ﲻ\n\n\n"); + } + else if(shuliang[num]>=chukushu) + { + shuliang[num]-=chukushu; + printf("ѳ\n\n\n"); + } + else + { + printf("\n\n\n"); + } + return; +} + +void tuichu() +{ + cnt2=0; + int i; + for(i=1;i<=cnt;i++) + { + if(shuliang[i]>0) + { + strcpy(xinghao2[i],xinghao[i]); + shuliang2[i]=shuliang[i]; + cnt2++; + } + } + for(i=1;i<=cnt2;i++) + { + strcpy(xinghao[i],xinghao2[i]); + shuliang[i]=shuliang2[i]; + } + cnt=cnt2; + baocun(); + printf("ʾѹر"); + return; +} + +void duru() +{ + FILE *fin; + fin = fopen("/tmp/warehouse.txt","r"); + while(1) + { + cnt++; + fscanf(fin,"%s %d",xinghao[cnt],&shuliang[cnt]); + if(shuliang[cnt]==-1) + { + cnt--; + break; + } + } + fclose(fin); + return; +} + +void baocun() +{ + FILE *fout; + fout = fopen("/tmp/warehouse.txt","w"); + int i=1; + while(i<=cnt) + { + fprintf(fout,"%s %d\n",xinghao[i],shuliang[i]); + i++; + } + fprintf(fout,"hello -1\n"); + fclose(fout); + return; +} + + + + + + + + + + + + + + + + + + + + + diff --git a/level1/p12_warehouse/warehouse/warehouse.h b/level1/p12_warehouse/warehouse/warehouse.h new file mode 100644 index 00000000..6d1056cc --- /dev/null +++ b/level1/p12_warehouse/warehouse/warehouse.h @@ -0,0 +1,17 @@ +#ifndef _WAREHOUSE_H +#define _WAREHOUSE_H +#include +#include +char xinghao[100005][15]; +char xinghao2[100005][15]; +int shuliang[100005]; +int shuliang2[100005]; +int cnt,cnt2; +void xianshi(); +void ruku(); +void chuku(); +void tuichu(); +void duru(); +void baocun(); +#endif + diff --git "a/\344\272\224\345\255\220\346\243\213/gomoku.c" "b/\344\272\224\345\255\220\346\243\213/gomoku.c" new file mode 100644 index 00000000..8d3f792f --- /dev/null +++ "b/\344\272\224\345\255\220\346\243\213/gomoku.c" @@ -0,0 +1,409 @@ +#include +#include"gomoku.h" +void drawmap() +{ + int i,j; + map[0][0]=' '; + for(i=1;i<=15;i++) + { + if(i<10) + { + map[0][i]=map[i][0]='0'+i; + } + else + { + map[0][i]=map[i][0]='A'+i-10; + } + } + for(i=1;i<=15;i++) + { + for(j=1;j<=15;j++) + { + map[i][j]='+'; + } + } + return; +} + +void showmap() +{ + int i,j; + for(i=0;i<=15;i++) + { + for(j=0;j<=15;j++) + { + printf("%c ",map[i][j]); + } + printf("\n"); + } + return; +} + +int judge(int x,int y) +{ + int m,n,l,r; + m=dfs(x,y,0,-1)+dfs(x,y,0,1)+1; + n=dfs(x,y,-1,0)+dfs(x,y,1,0)+1; + l=dfs(x,y,-1,-1)+dfs(x,y,1,1)+1; + r=dfs(x,y,-1,1)+dfs(x,y,1,-1)+1; + if(m>=5||n>=5||l>=5||r>=5)return 1; + else return 0; +} + + +int dfs(int x,int y,int dx,int dy) +{ + int count=0,i; + while(x+dx>=1&&x+dx<=15&&y+dy>=1&&y+dy<=15) + { + if(map[x+dx][y+dy]==map[x][y]) + { + count++; + x=x+dx;y=y+dy; + } + else + { + if(dx>0||dy>0) + { + for(i=7;i<=11;i++) + { + if(x+dx>=1&&x+dx<=15&&y+dy>=1&&y+dy<=15) + { + chess[i]=map[x+dx][y+dy]; + } + x=x+dx;y=y+dy; + } + } + if(dx<0||dy<0) + { + for(i=5;i>=1;i--) + { + if(x+dx>=1&&x+dx<=15&&y+dy>=1&&y+dy<=15) + { + chess[i]=map[x+dx][y+dy]; + } + x=x+dx;y=y+dy; + } + } + break; + } + } + return count; +} + +int score(int m,int n,int l,int r) +{ + //ʮַ + int level[20]={0,100000,10000,5000,1000,500,400,100,90,50,10,9,5,2,1,0}; + + if(m==11||n==11||l==11||r==11) + return level[1]; + if(m==10||n==10||l==10||r==10) + return level[2]; + if((m==9&&n==9)||(m==9&&l==9)||(m==9&&r==9)||(n==9&&l==9)||(n==9&&r==9)||(l==9&&r==9)) + return level[2]; + if((m==9&&n==7)||(m==9&&l==7)||(m==9&&r==7)||(n==9&&l==7)||(n==9&&r==7)||(l==9&&r==7)|| + (m==7&&n==9)||(m==7&&l==9)||(m==7&&r==9)||(n==7&&l==9)||(n==7&&r==9)||(l==7&&r==9)) + return level[2]; + if((m==7&&n==7)||(m==7&&l==7)||(m==7&&r==7)||(n==7&&l==7)||(n==7&&r==7)||(l==7&&r==7)) + return level[3]; + if((m==5&&n==7)||(m==5&&l==7)||(m==5&&r==7)||(n==5&&l==7)||(n==5&&r==7)||(l==5&&r==7)|| + (m==7&&n==5)||(m==7&&l==5)||(m==7&&r==5)||(n==7&&l==5)||(n==7&&r==5)||(l==7&&r==5)) + return level[4]; + if(m==9||n==9||l==9||r==9) + return level[5]; + if(m==8||n==8||l==8||r==8) + return level[6]; + if(m==7||n==7||l==7||r==7) + return level[7]; + if(m==6||n==6||l==6||r==6) + return level[8]; + if((m==4&&n==4)||(m==4&&l==4)||(m==4&&r==4)||(n==4&&l==4)||(n==4&&r==4)||(l==4&&r==4)) + return level[9]; + if(m==4||n==4||l==4||r==4) + return level[10]; + if(m==3||n==3||l==3||r==3) + return level[11]; + if(m==5||n==5||l==5||r==5) + return level[12]; + if(m==2||n==2||l==2||r==2) + return level[13]; + return level[14]; +} + + +int score_x_y(int x,int y) +{ + int i,count_m,count_n,count_l,count_r,m,n,l,r; + int finalscore; + + for(i=1;i<=11;i++)chess[i]='!'; + count_m=dfs(x,y,0,-1)+dfs(x,y,0,1)+1; + chess[6]=map[x][y]; + m=chesstype(count_m); + + for(i=1;i<=11;i++)chess[i]='!'; + count_n=dfs(x,y,-1,0)+dfs(x,y,1,0)+1; + chess[6]=map[x][y]; + n=chesstype(count_n); + + for(i=1;i<=11;i++)chess[i]='!'; + count_l=dfs(x,y,-1,-1)+dfs(x,y,1,1)+1; + chess[6]=map[x][y]; + l=chesstype(count_l); + + for(i=1;i<=11;i++)chess[i]='!'; + count_r=dfs(x,y,-1,1)+dfs(x,y,1,-1)+1; + chess[6]=map[x][y]; + r=chesstype(count_r); + + finalscore=score(m,n,l,r); + + return finalscore; +} + + +int chesstype(int count) +{ + //ʮһͣWIN5,LIVE4,DIE4,LOWDIE4,LIVE3,LOWLIVE3,DIE3,LIVE2,LOWLIVE2,DIE2NOTHEREAT + char NOTHINGFLAG='+'; + char mycolor=chess[6],hiscolor='0'+'&'-chess[6]; + char colorleft=chess[5],colorright=chess[7]; + int left=5,right=7; + + int WIN5=11,LIVE4=10,DIE4=9,LOWDIE4=8,LIVE3=7; + int LOWLIVE3=6,DIE3=5,LIVE2=4,LOWLIVE2=3,DIE2=2,NOTHREAT=1; + + if (count>=5) + { + return WIN5; + } + if (count==4) + { + if(colorleft == NOTHINGFLAG && colorright == NOTHINGFLAG)//߶Ͽλþ + return LIVE4; + else if (colorleft == hiscolor && colorright == hiscolor)//߶Ͽλþǿ + return NOTHREAT; + else if (colorleft == NOTHINGFLAG || colorright == NOTHINGFLAG)//߶Ͽλֻһ + return DIE4; + } + if(count == 3) + { + char colorleft1 = chess[left - 1]; + char colorright1 = chess[right + 1]; + if (colorleft == NOTHINGFLAG && colorright == NOTHINGFLAG)//߶Ͽλþ + { + if (colorleft1 == hiscolor && colorright1 == hiscolor) + return DIE3; + else if (colorleft1 == mycolor || colorright1 == mycolor) + return LOWDIE4; + else if (colorleft1 == NOTHINGFLAG || colorright1 == NOTHINGFLAG) + return LIVE3; + } + else if (colorleft == hiscolor && colorright == hiscolor)//߶Ͽλþǿ + { + return NOTHREAT; + } + else if (colorleft == NOTHINGFLAG || colorright == NOTHINGFLAG)//߶Ͽλֻһ + { + if (colorleft == hiscolor) + { + if (colorright1 == hiscolor) + return NOTHREAT; + if (colorright1 == NOTHINGFLAG) + return DIE3; + if (colorright1 == mycolor) + return LOWDIE4; + } + if (colorright == hiscolor) + { + if (colorleft1 == hiscolor) + return NOTHREAT; + if (colorleft1 == NOTHINGFLAG) + return DIE3; + if (colorleft1 == mycolor) + return LOWDIE4; + } + } + } + + if (count == 2) + { + int colorleft1 = chess[left - 1]; + int colorright1 = chess[right + 1]; + int colorleft2 = chess[left - 2]; + int colorright2 = chess[right + 2]; + if (colorleft == NOTHINGFLAG && colorright == NOTHINGFLAG)//߶Ͽλþ + { + if ((colorright1 == NOTHINGFLAG && colorright2 == mycolor) || + (colorleft1 == NOTHINGFLAG && colorleft2 == mycolor)) + return DIE3; + else if (colorleft1 == NOTHINGFLAG && colorright1 == NOTHINGFLAG) + return LIVE2; + + if ((colorright1 == mycolor && colorright2 == hiscolor) || + (colorleft1 == mycolor && colorleft2 == hiscolor)) + return DIE3; + + if ((colorright1 == mycolor && colorright2 == mycolor) || + (colorleft1 == mycolor && colorleft2 == mycolor)) + return LOWDIE4; + + if ((colorright1 == mycolor && colorright2 == NOTHINGFLAG) || + (colorleft1 == mycolor && colorleft2 == NOTHINGFLAG)) + return LOWLIVE3; + } + else if (colorleft == hiscolor && colorright == hiscolor)//߶Ͽλþǿ + { + return NOTHREAT; + } + else if (colorleft == NOTHINGFLAG || colorright == NOTHINGFLAG)//߶Ͽλֻһ + { + if (colorleft == hiscolor) + { + if (colorright1 == hiscolor || colorright2 == hiscolor) { + return NOTHREAT; + } + else if (colorright1 == NOTHINGFLAG && colorright2 == NOTHINGFLAG) { + return DIE2; + } + else if (colorright1 == mycolor && colorright2 == mycolor) { + return LOWDIE4; + } + else if (colorright1 == mycolor || colorright2 == mycolor) { + return DIE3; + } + } + if (colorright == hiscolor) + { + if (colorleft1 == hiscolor || colorleft2 == hiscolor) { + return NOTHREAT; + } + else if (colorleft1 == NOTHINGFLAG && colorleft2 == NOTHINGFLAG) { + return DIE2; + } + else if (colorleft1 == mycolor && colorleft2 == mycolor) { + return LOWDIE4; + } + else if (colorleft1 == mycolor || colorleft2 == mycolor) { + return DIE3; + } + } + } + } + + if (count == 1) + { + int colorleft1 = chess[left - 1]; + int colorright1 = chess[right + 1]; + int colorleft2 = chess[left - 2]; + int colorright2 = chess[right + 2]; + int colorleft3 = chess[left - 3]; + int colorright3 = chess[right + 3]; + + if (colorleft == NOTHINGFLAG && colorleft1 == mycolor && + colorleft2 == mycolor && colorleft3 == mycolor) + return LOWDIE4; + if (colorright == NOTHINGFLAG && colorright1 == mycolor && + colorright2 == mycolor && colorright3 == mycolor) + return LOWDIE4; + + if (colorleft == NOTHINGFLAG && colorleft1 == mycolor && + colorleft2 == mycolor && colorleft3 == NOTHINGFLAG && colorright == NOTHINGFLAG) + return LOWLIVE3; + if (colorright == NOTHINGFLAG && colorright1 == mycolor && + colorright2 == mycolor && colorright3 == NOTHINGFLAG && colorleft == NOTHINGFLAG) + return LOWLIVE3; + + if (colorleft == NOTHINGFLAG && colorleft1 == mycolor && + colorleft2 == mycolor && colorleft3 == hiscolor && colorright == NOTHINGFLAG) + return DIE3; + if (colorright == NOTHINGFLAG && colorright1 == mycolor && + colorright2 == mycolor && colorright3 == hiscolor && colorleft == NOTHINGFLAG) + return DIE3; + + if (colorleft == NOTHINGFLAG && colorleft1 == NOTHINGFLAG && + colorleft2 == mycolor && colorleft3 == mycolor) + return DIE3; + if (colorright == NOTHINGFLAG && colorright1 == NOTHINGFLAG && + colorright2 == mycolor && colorright3 == mycolor) + return DIE3; + + if (colorleft == NOTHINGFLAG && colorleft1 == mycolor && + colorleft2 == NOTHINGFLAG && colorleft3 == mycolor) + return DIE3; + if (colorright == NOTHINGFLAG && colorright1 == mycolor && + colorright2 == NOTHINGFLAG && colorright3 == mycolor) + return DIE3; + + if (colorleft == NOTHINGFLAG && colorleft1 == mycolor && + colorleft2 == NOTHINGFLAG && colorleft3 == NOTHINGFLAG && colorright == NOTHINGFLAG) + return LOWLIVE2; + if (colorright == NOTHINGFLAG && colorright1 == mycolor && + colorright2 == NOTHINGFLAG && colorright3 == NOTHINGFLAG && colorleft == NOTHINGFLAG) + return LOWLIVE2; + + if (colorleft == NOTHINGFLAG && colorleft1 == NOTHINGFLAG && + colorleft2 == mycolor && colorleft3 == NOTHINGFLAG && colorright == NOTHINGFLAG) + return LOWLIVE2; + if (colorright == NOTHINGFLAG && colorright1 == NOTHINGFLAG && + colorright2 == mycolor && colorright3 == NOTHINGFLAG && colorleft == NOTHINGFLAG) + return LOWLIVE2; + + } + return NOTHREAT;//NOTHREAT +} + + + +void computer(int *x,int *y) +{ + int i,j,scoremax1=0,scoremax2=0,nowscore=0,maxx1,maxy1,maxx2,maxy2; + for(i=1;i<=15;i++) + for(j=1;j<=15;j++) + { + if(map[i][j]=='+') + { + map[i][j]='0'; + nowscore=score_x_y(i,j); + if(nowscore>scoremax1) + { + scoremax1=nowscore; + maxx1=i; + maxy1=j; + } + map[i][j]='&'; + nowscore=score_x_y(i,j); + if(nowscore>scoremax2) + { + scoremax2=nowscore; + maxx2=i; + maxy2=j; + } + map[i][j]='+'; + } + } + *x=scoremax1>=scoremax2?maxx1:maxx2;*y=scoremax1>=scoremax2?maxy1:maxy2; + return; +} + + + + + + + + + + + + + + + + + + + + + diff --git "a/\344\272\224\345\255\220\346\243\213/gomoku.h" "b/\344\272\224\345\255\220\346\243\213/gomoku.h" new file mode 100644 index 00000000..68d6f877 --- /dev/null +++ "b/\344\272\224\345\255\220\346\243\213/gomoku.h" @@ -0,0 +1,16 @@ +#ifndef _GOMOKU_H +#define _GOMOKU_H +#include +#include +char map[20][20]; +char chess[20];//ijһϢ +void drawmap();// +void showmap();//ʾ +void computer(int* x,int* y);//óλ +int score_x_y(int x,int y);//λãx,yӵ÷ +int score(int m,int n,int l,int r);//÷ +int chesstype(int count);//жijһ +int judge(int x,int y);//жӮ +int dfs(int x,int y,int dx,int dy);//ж +#endif + diff --git "a/\344\272\224\345\255\220\346\243\213/rukou.c" "b/\344\272\224\345\255\220\346\243\213/rukou.c" new file mode 100644 index 00000000..0c92a29b --- /dev/null +++ "b/\344\272\224\345\255\220\346\243\213/rukou.c" @@ -0,0 +1,61 @@ +#include +#include"gomoku.h" +int main() +{ + drawmap(); + showmap(); + int i; + + map[7][7]='0'; + system("cls"); + showmap();// + + + for(i=2;;i++) + { + int a,b; + if(i%2==1) + { + int x,y; + computer(&x,&y); + a=x;b=y; + } + else scanf("%d %d",&a,&b); + if(a>=0&&a<=15&&b>=0&&b<=15) + { + if(map[a][b]!='+') + { + i--; + } + if(i%2==1&&map[a][b]=='+') + { + map[a][b]='0'; + } + else if(i%2==0&&map[a][b]=='+') + { + map[a][b]='&'; + } + system("cls"); + showmap(); + } + else + { + i--; + } + + if(judge(a,b)) + { + if(map[a][b]=='0') + { + printf("You lose it!"); + } + else + { + printf("You win it!"); + } + break; + } + + } + return 0; +}