diff --git a/level1/p01_runningLetter/running-letter.cpp b/level1/p01_runningLetter/running-letter.cpp new file mode 100644 index 00000000..21a783f6 --- /dev/null +++ b/level1/p01_runningLetter/running-letter.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +void print_1(int i) +{ + int j=0; + while (j=0;i--) print_2(i); + return 0; +} diff --git a/level1/p02_isPrime/isPrime.cpp b/level1/p02_isPrime/isPrime.cpp new file mode 100644 index 00000000..8b6fa7ff --- /dev/null +++ b/level1/p02_isPrime/isPrime.cpp @@ -0,0 +1,37 @@ +#include +#include + +int judge(int num) +{ + int i,judge=0; + for (i=2;i<=sqrt(num);i++) + { + judge=num%i; + if (judge==0) + { + break; + } + else continue; + } + if (num==2||num==3) + { + judge=1; + } + return judge; +} + +int main() +{ + int num; + printf("Please enter a nunber:\n"); + scanf("%d",&num); + if (judge(num)==0) + { + printf("%d is not a prime number.\n",num); + } + else + { + printf("%d is a prime number.\n",num); + } + return 0; +} diff --git a/level1/p03_Diophantus/Diophantus.cpp b/level1/p03_Diophantus/Diophantus.cpp new file mode 100644 index 00000000..33841d99 --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.cpp @@ -0,0 +1,15 @@ +#include + +int main() +{ + int x; + for (x=1;x<200;x++) + { + if ((x/6.0+x/12.0+x/7.0+5+x/2.0+4)==(x/1.0)) + { + printf("Diophantus was %d years old\n",x); + } + else continue; + } + return 0; +} diff --git a/level1/p04_ narcissus/narcissus.cpp b/level1/p04_ narcissus/narcissus.cpp new file mode 100644 index 00000000..b42f3144 --- /dev/null +++ b/level1/p04_ narcissus/narcissus.cpp @@ -0,0 +1,30 @@ +# include +# include + +int judge(int n) +{ + int unit,tens,hundreds,m=n; + hundreds=n/100; + n-=hundreds*100; + tens=n/10; + unit=n-tens*10; + if ((pow(unit,3)+pow(tens,3)+pow(hundreds,3))==m) + { + return 1; + } + else return 0; +} + +int main() +{ + int i; + for (i=100;i<1000;i++) + { + if(judge(i)==1) + { + printf("%-4d",i); + } + else continue; + } + return 0; +} diff --git a/level1/p05_allPrimes/allPrimes.cpp b/level1/p05_allPrimes/allPrimes.cpp new file mode 100644 index 00000000..4005ae2d --- /dev/null +++ b/level1/p05_allPrimes/allPrimes.cpp @@ -0,0 +1,41 @@ +#include +#include + +int judge(int num) +{ + int i,judge=0; + for (i=2;i<(num/2+1);i++) + { + judge=num%i; + if (judge==0) + { + break; + } + else continue; + } + if (num==2||num==3) + { + judge=1; + } + return judge; +} + +int main() +{ + int a=clock(); + int i,c; + for (i=2;i<1000;i++) + { + if (judge(i)==0) + { + continue; + } + else + { + printf("%-4d",i); + } + } + int b=clock(); + printf ("\nRunning time:%d ms",c=b-a); + return 0; +} diff --git a/level1/p06_Goldbach/Goldbach.cpp b/level1/p06_Goldbach/Goldbach.cpp new file mode 100644 index 00000000..d4815a21 --- /dev/null +++ b/level1/p06_Goldbach/Goldbach.cpp @@ -0,0 +1,42 @@ +#include +#include + +int judge(int num) +{ + int i,judge=0; + for (i=2;i<(sqrt(num)+1);i++) + { + judge=num%i; + if (judge==0) + { + break; + } + else continue; + } + if (num==2||num==3) + { + judge=1; + } + return judge; +} + +int main() +{ + int n,i,j; + printf("Please enter the range:\n"); + scanf("%d",&j); + for (n=4;n<=j;n+=2) + { + for (i=2;i +#define K 2 + +void encrypt(char *p) +{ + for (int i=0;i<100;i++,p++) + { + if ((*p)=='\0') + {break;} + (*p)+=K; + printf("%c",*p); + } +} + +void decrypt(char *p) +{ + for (int i=0;i<100;i++,p++) + { + if ((*p)=='\0') + {break;} + (*p)-=K; + printf("%c",*p); + } +} + +int main() +{ + int n; + void (*p1)(char *); + char ch[100]; + printf("Please choose 1 or 2(1--encrypt, 2--decrypt) :\n"); + scanf("%d",&n); + printf("Please enter your text:\n"); + scanf("%s",ch); + (n==1) ? p1=encrypt :p1=decrypt; + printf("Result:\n"); + (*p1)(ch); + return 0; +} diff --git a/level1/p08_hanoi/hanio.cpp b/level1/p08_hanoi/hanio.cpp new file mode 100644 index 00000000..bf7145a8 --- /dev/null +++ b/level1/p08_hanoi/hanio.cpp @@ -0,0 +1,30 @@ +#include + +void move(char x,char y) +{ + printf("%c-->%c\n",x,y); +} + +void hanoi(int n,char one,char two,char three) +{ + if (n == 1) + { + move(one,three); + } + else + { + hanoi(n-1,one,three,two); + move(one,three); + hanoi(n-1,two,one,three); + } +} + +int main() +{ + int n; + printf("Please enter the number of disks:\n"); + scanf("%d",&n); + printf("The steps are as follows:\n"); + hanoi(n,'A','B','C'); + +} diff --git a/level1/p09_maze/maze.cpp b/level1/p09_maze/maze.cpp new file mode 100644 index 00000000..61e5cbe5 --- /dev/null +++ b/level1/p09_maze/maze.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +#define ROW 10 +#define COL 10 + + + +int main(){ + void output(int (*p)[COL],int x,int y,char ch); //函数声明 + void move(int (*p)[COL],char ch,int *px,int *py); + void print(int *p); + void goto_xy(int,int); + + int maze[ROW][COL]={ //迷宫数组 + 35,43,35,35,35,35,35,35,35,35, + 35,43,43,43,35,35,35,43,35,35, + 35,35,35,43,35,35,35,43,35,35, + 35,35,35,43,43,43,43,35,35,35, + 35,43,43,43,43,35,43,35,35,35, + 35,43,35,35,43,35,43,35,35,35, + 35,43,43,35,35,35,43,35,43,35, + 35,43,43,43,43,35,43,43,43,35, + 35,43,35,35,35,35,35,35,43,43, + 35,35,35,35,35,35,35,35,35,35 + }; + + int x = 0,y = 1; //定义变量 + int *px = &x,*py = &y; + int (*p)[COL] = maze; + char m; + + output(p,x,y,'w'); //迷宫实现 + + m = getch(); + while (1){ + move(p,m,px,py); + if (maze[ROW-2][COL-1] == 64){ + break; + } + m = getch(); + } + printf("Congratulations!\n"); + system("pause"); + return 0; +} + +void print(int *p){ //输出迷宫数组 + printf("\n"); + for (int i = 0; i +#include +#include +#include +#include +#define ROW 39 //迷宫行数 +#define COL 39 //迷宫列数 +#define DOWN 46 //玩家输入移动指令时光标的位置 + +typedef struct _SITE { //自定义坐标类型SITE + +INT X; + +INT Y; + +} SITE; + +int main(){ + void output(int (*p)[COL],int x,int y,char ch); //函数声明 + void originalmaze(int (*p)[COL]); + void makemaze(int (*p)[COL], int *px, int *py); + void replace(int (*p)[COL]); + int destination(int (*p)[COL]); + void move(int (*p)[COL],char ch,int *px,int *py); + void print(int *p); + void goto_xy(int,int); + + int x = 0, y = 0, desti; //定义变量 + + int *px = &x, *py = &y; + int maze[ROW][COL]; + int (*p)[COL] = maze; + char m; + + originalmaze(p); + makemaze(p,px,py); + replace(p); + desti = destination(p); + + print((int *) p); + output(p,x,y,'w'); //迷宫实现 + + m = getch(); + while (1){ + move(p,m,px,py); + if (maze[0][desti] == 64){ + break; + } + m = getch(); + } + printf("Congratulations!\n"); + system("pause"); + return 0; +} + +void print(int *p){ //输出迷宫数组 + printf("\n"); + for (int i = 0; i < ROW; i++){ + for (int j = 0; j +#include +#include +#include +#include +#define ROW 39 //迷宫行数 +#define COL 39 //迷宫列数 +#define DOWN 46 //玩家输入移动指令时光标的位置 + +typedef struct _SITE { //自定义坐标类型SITE + +INT X; + +INT Y; + +} SITE; + +int main(){ + void output(int (*p)[COL],int x,int y,char ch); //函数声明 + void originalmaze(int (*p)[COL]); + void makemaze(int (*p)[COL], int *px, int *py); + void replace(int (*p)[COL]); + int destination(int (*p)[COL]); + void move(int (*p)[COL],char ch,int *px,int *py); + void print(int *p); + void goto_xy(int,int); + + int x = 0, y = 0, desti; //定义变量 + int *px = &x, *py = &y; + int maze[ROW][COL]; + int (*p)[COL] = maze; + char m; + + originalmaze(p); + makemaze(p,px,py); + replace(p); + desti = destination(p); + + output(p,x,y,'w'); //迷宫实现 + + m = getch(); + while (1){ + move(p,m,px,py); + if (maze[0][desti] == 64){ + break; + } + m = getch(); + } + printf("Congratulations!\n"); + system("pause"); + return 0; +} + +void print(int *p){ //输出迷宫数组 + printf("\n"); + for (int i = 0; i +#include +#include +#include + +int main(){ + void chooselevel(int *plevel); + void showscore(); + void getrowcolaim(FILE *fin,int *prow,int *pcol,int *paim); + void creatmap(FILE *fin,int *p,int row,int col); + void output(int *p,int x,int y,int n,int COL); //函数声明 + int getmove(int *p,char ch,int *px,int *py,int ROW,int COL); + void print(int *p,int ROW,int COL); + void findplayer(int *p,int *px,int *py,int ROW,int COL); + void goto_xy(int,int); + int gameover(int *p,int ROW,int COL,int AIM); + void savescores(FILE *fout,int steps); + + int x = 0,y = 0; //定义变量 + int *px = &x,*py = &y; + + char m; + + int row, col, aim; + int *prow = &row, *pcol = &col, *paim = &aim; + + FILE *fin,*fout; + + int level; + int *plevel = &level; + + chooselevel(plevel); //调用选关函数 + while (1){ + switch (level){ + case 0:{ //调用函数查看纪录 + showscore(); + system("pause"); + system("cls"); + chooselevel(plevel); + } + case 1:{ //打开对应关卡文件 + if ((fin = fopen("map1.dat","r+")) == NULL) { + printf("Can't open this map!\n"); + exit(0); + } + if ((fout = fopen("score1.dat","a+")) == NULL) { + printf("Can't read the scores!\n"); + exit(0); + } + break; + } + case 2:{ + if ((fin = fopen("map2.dat","r+")) == NULL) { + printf("Can't open this map!\n"); + exit(0); + } + if ((fout = fopen("score2.dat","a+")) == NULL) { + printf("Can't read the scores!\n"); + exit(0); + } + break; + } + default:; + } + if (level != 0){ + break; + } + } + + + getrowcolaim(fin,prow,pcol,paim); //获取地图行数,列数和箱子数 + + const int ROW = row, COL = col, AIM = aim; //创建对应的大小数组 并 读入地图数组 + int map[ROW][COL]; + int *p = &map[0][0]; + creatmap(fin,&map[0][0],ROW,COL); + + + print(&map[0][0],ROW,COL); //输出地图 + findplayer(&map[0][0],px,py,ROW,COL); //定位玩家初始坐标 + goto_xy(0,ROW+9); + + int steps = 0; //此变量记录玩家步数 + + m = getch(); + while (1){ + + if (m == 'q') { //输入 q 退出游戏 + printf("Game Over!\n"); + system("pause"); + exit(0); + } + + int temp = getmove(p,m,px,py,ROW,COL); //玩家每推着箱子走一步,计数器+1 + if (temp == 1){ + steps++; + } + goto_xy(0,ROW+9); + printf("已走步数:%d\n",steps); + + if (gameover(&map[0][0],ROW,COL,AIM) == 0){ //箱子全部到达目的地,游戏结束 + break; + } + + m = getch(); + } + printf("Congratulations!\n"); //收尾工作 + savescores(fout,steps); //保存成绩 + fclose(fin); //关闭文件 + fclose(fout); + system("pause"); + return 0; +} + +void chooselevel(int *plevel) { //选关函数 + printf("推箱子小游戏\n"); + printf("输入 0 查看纪录 1,2... 选关\n"); + + scanf("%d",plevel); + system("cls"); + +} + +void showscore(){ //从对应文件在读入纪录 + printf("请输入你想查看纪录的关卡:\n"); + int n; + scanf("%d",&n); + FILE *fscore; + switch(n){ + case 1:{ + if ((fscore = fopen("score1.dat","r")) == NULL) { + printf("Can't read your score!\n"); + exit(0); + } + break; + } + case 2:{ + if ((fscore = fopen("score2.dat","r")) == NULL) { + printf("Can't read your score!\n"); + exit(0); + } + break; + } + default:; + } + rewind(fscore); + + while(!feof(fscore)){ + putchar(getc(fscore)); + } + putchar(10); //输出一个空格 +} + +void getrowcolaim(FILE *fin,int *prow,int *pcol,int *paim){ //从文件中读入地图行数,列数和箱子数 + int row,col,aim; + fscanf(fin,"%d,%d,%d,",&row,&col,&aim); + *prow = row; *pcol = col; *paim = aim; +} + +void creatmap(FILE *fin,int *p,int row,int col){ //从文件中读入地图数组 + + for (int i = 0; i < row; i++){ + for (int j = 0; j < col; j++){ + fscanf(fin,"%d,",(p+(i*col)+j)); + } + } +} + +void print(int *p,int ROW,int COL){ //输出地图数组 + for (int i = 0; i +#include +#define LEN sizeof(Stu) + +typedef struct Student{ + long num; + float score; + struct Student *next; +}Stu; + +int n; +Stu * creat(); +void print(Stu *head); +Stu * reverse(Stu *head); +int search(Stu *head,int t); + +int main(){ + int t=0; + Stu *pt = creat();//创建链表 + printf("\n"); + print(pt); + pt = reverse(pt);//反转链表 + printf("\nreverse:\n"); + print(pt); + printf("\n"); + t = search(pt,t);//查找第一个满足要求的节点 + printf("%d ",t); + t = search(pt,t);//查找下一个满足要求的节点 + printf("%d ",t); + return 0; +} + +Stu * creat() { + Stu *head = NULL, *p1 = NULL, *p2 = NULL; + p1 = p2 = (Stu *) malloc(LEN); + scanf("%ld %f",&p1 -> num,&p1 -> score); + while (p1 -> num != 0){ + n++; + if (n == 1){ + head = p1; + } + else{ + p2 -> next = p1; + } + p2 = p1; + p1 = (Stu *) malloc(LEN); + scanf("%ld %f",&p1 -> num,&p1 -> score); + } + p2 -> next = NULL; + return head; +} + +void print(Stu *head){ + Stu *p = head; + while (head != NULL){ + printf("%ld %.1f\n",p -> num,p -> score); + if (p -> next == NULL) { + break; + } + p = p -> next; + } +} + +Stu * reverse(Stu *head) { + /*递归法 + if (head == NULL || head -> next == NULL) { + return head; + } + Stu *p = head -> next; + Stu *newhead = reverse(p); + p -> next = head; + head -> next = NULL; + return newhead; */ + + Stu *cur = head, *temp = head; + if ((cur -> next) != NULL){ + cur = cur -> next; + temp -> next = NULL; + } + while (1){ + temp = cur; + cur = cur -> next; + temp -> next = head; + head = temp; + if (cur -> next == NULL){ + cur -> next = temp; + break; + } + } + head = cur; + return head; +} + +int search(Stu *head,int t){ + int m=0; + Stu *p = head; + while(head != NULL){ + m++; + if((p->score < 60) || (p->next == NULL)){ + if(m>t){ + break; + } + } + p = p -> next; + } + if((m>=n)&&(p->score >= 60)){ + return -1; + } + return m; +} + diff --git a/level1/p12_warehouse/warehouse.c b/level1/p12_warehouse/warehouse.c new file mode 100644 index 00000000..e5fb914c --- /dev/null +++ b/level1/p12_warehouse/warehouse.c @@ -0,0 +1,165 @@ +#include +#include + +//货号为4位,数量为2位 + +void print(); +void outList(); +void add(); +void move(); + +int main() { + print(); + int m; + m=getch(); + while(1){ + system("cls"); + switch(m){ + case 49:{ + printf("货号 数量\n"); + outList(); + break; + } + case 50:{ + add(); + break; + } + case 51:{ + move(); + break; + } + case 52:{ + printf("感谢使用!\n"); + system("pause"); + exit(0); + break; + } + default:; + } + system("cls"); + print(); + m=getch(); + } + + + return 0; +} + +void print(){ + printf("1.显示存货列表\n"); + printf("2.入库\n"); + printf("3.出库\n"); + printf("4.退出程序\n"); +} + +void outList(){ + FILE *fin; + if((fin=fopen("list.dat","r"))==NULL){ + printf("读取存货列表失败!\n"); + system("pause"); + system("cls"); + return; + } + rewind(fin); + while(!feof(fin)){ + putchar(getc(fin)); + } + putchar(10); + fclose(fin); + + system("pause"); + system("cls"); +} + +void add(){ + FILE *fout; + if((fout=fopen("list.dat","a+"))==NULL){ + printf("打开文件失败!"); + system("pause"); + system("cls"); + return; + } + char ch='y'; + while(ch=='y'){ + char goods[20]; + int i = 0; + printf("请输入货物型号(以 # 结束):\n"); + goods[0] = getchar(); + + while (goods[i] != '#'){ + fprintf(fout,"%c",goods[i]); + i++; + goods[i] = getchar(); + } + fprintf(fout," : "); + + int num; + printf("请输入货物数量:\n"); + getchar(); + scanf("%d",&num); + getchar(); + if(num<10){ + fprintf(fout,"%d",0); + fprintf(fout,"%d\n",num); + } + else{ + fprintf(fout,"%d\n",num); + } + + printf("是否继续?(y/n)\n"); + ch = getch(); + } + fclose(fout); +} + +void move(){ + FILE *fout; + if((fout=fopen("list.dat","r+"))==NULL){ + printf("打开文件失败!"); + system("pause"); + system("cls"); + return; + } + + int goods=0,num1=0,num2=0,num3=0; + char ch='y'; + while(ch=='y'){ + long tag=7; + printf("请输入货号:\n"); + scanf("%d",&goods); + getchar(); + tag=tag+1+(goods-1)*10; + fseek(fout,tag,SEEK_SET); + + printf("请输入出库数量:\n"); + scanf("%d",&num1); + getchar(); + fscanf(fout,"%d\n",&num2); + rewind(fout); + fseek(fout,tag,SEEK_SET); + num3=num2-num1; + if(num3<10){ + if(num3>0){ + fprintf(fout,"%d",0); + fprintf(fout,"%d",num3); + } + else{ + printf("出库数量大于库存数量,请重新输入!\n"); + continue; + } + } + else{ + fprintf(fout,"%d",num3); + } + + fseek(fout,tag,SEEK_SET); + printf("操作成功!此货物目前数量:"); + fscanf(fout,"%d\n",&num2); + printf("%d\n",num2); + + printf("是否继续?(y/n)\n"); + ch=getch(); + } + fclose(fout); +} +