diff --git a/level1/p01_runningLetter/main.c b/level1/p01_runningLetter/main.c new file mode 100644 index 00000000..9df6682e --- /dev/null +++ b/level1/p01_runningLetter/main.c @@ -0,0 +1,42 @@ +#include +#include +#include +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char *argv[]) { + int a,b; + a = 0; + while(1){ + //从左向右 + while (a<=79) + { + system ("cls"); + b = 1; + while (b<=a) + { + printf(" "); + ++b; + } + printf("H"); + Sleep(100); + ++a; + } + a--; + //从右向左 + while (a> 0) //如果使用大于等于,将会于开头处输出两个0空格的H,导致停顿时间变长 + { + system ("cls"); + b = 1; + while (b<=a) + { + printf(" "); + ++b; + } + printf("H"); + Sleep(100); + --a; + } + } + system("pause"); + return 0; +} diff --git a/level1/p02_isPrime/main.c b/level1/p02_isPrime/main.c new file mode 100644 index 00000000..c8173408 --- /dev/null +++ b/level1/p02_isPrime/main.c @@ -0,0 +1,34 @@ +#include +#include +#define MIN_PRIME 2 +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char *argv[]) { + + /*input*/ + printf("Please input a number\n"); + int number; + scanf("%d",&number); + + /*judgement*/ + int i; + for(i=MIN_PRIME;i + +int main() { + for(float FatherAge=1;FatherAge<200;FatherAge++){ + if(FatherAge/6+FatherAge/12+FatherAge/7+5+FatherAge/2+4==FatherAge){ + printf("%f\n",FatherAge-4); + } + } + return 0; +} \ No newline at end of file diff --git a/level1/p04_ narcissus/p04_ narcissus.c b/level1/p04_ narcissus/p04_ narcissus.c new file mode 100644 index 00000000..07b47d3f --- /dev/null +++ b/level1/p04_ narcissus/p04_ narcissus.c @@ -0,0 +1,27 @@ +#include +#include +#include +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char *argv[]) { + int number; + int hundred; + int ten; + int one; + int target; + + number = 100; + while(number<=999) + { + hundred = number/100; + ten = (number-hundred*100)/10; + one = number%10; + target = pow(hundred,3)+pow(ten,3)+pow(one,3); + if(target==number) + { + printf("%d\n",number); + } + number++; + } + return 0; +} diff --git a/level1/p05_allPrimes/p05_allPrimes.c b/level1/p05_allPrimes/p05_allPrimes.c new file mode 100644 index 00000000..d1063f9b --- /dev/null +++ b/level1/p05_allPrimes/p05_allPrimes.c @@ -0,0 +1,42 @@ +#include +#include +#include +#define MIN_PRIME 2 +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char *argv[]) { + int number; + clock_t t1,t2; + t1 = clock(); + number = MIN_PRIME; + while(number<=1000) + { + if(is_prime(number)) + { + printf("%d\n",number); + } + number++; + } + t2 = clock(); + printf("%d\n",t2-t1); + return 0; +} +int is_prime(number){ + int i; + for (i=MIN_PRIME;i + +int main() { + int Primes[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; + for (int i = 2; i < 99; i += 2) { + for (int j = 0; j < 25; j++) { + if (Primes[j] >= i)break; + for (int k = j; k < 25; k++) { + if (Primes[j] + Primes[k] == i) + printf("%d=%d+%d\n", i, Primes[j], Primes[k]); + else if (Primes[j] + Primes[k] > i) { + break; + } + } + } + } + return 0; +} \ No newline at end of file diff --git a/level1/p07_encrypt_decrypt/main.c b/level1/p07_encrypt_decrypt/main.c new file mode 100644 index 00000000..b62c704c --- /dev/null +++ b/level1/p07_encrypt_decrypt/main.c @@ -0,0 +1,20 @@ +#include +#include + +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ +/*reverse encrypt*/ +int main(int argc, char *argv[]) { + char c[1000]; + fgets(c,(sizeof c / sizeof c[0]),stdin); + int d = strlen(c); + + char a[1000]; + int j = 0; + for (int i=d-2;i>=0;i--) + { + a[i] = c [j]; + j++; + } + puts(a); + return 0; +} diff --git a/level1/p08_hanoi/hanoi.c b/level1/p08_hanoi/hanoi.c new file mode 100644 index 00000000..553c4a41 --- /dev/null +++ b/level1/p08_hanoi/hanoi.c @@ -0,0 +1,59 @@ +#include +#include + +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ +int change(n,position1,position2) +{ + //得到position3 + char position3; + if(position1 == 'A') + { + if(position2 == 'B') + { + position3 = 'C'; + } + else + { + position3 = 'B'; + } + } + else if(position1 == 'B') + { + if(position2 == 'A') + { + position3 = 'C'; + } + else + { + position3 = 'A'; + } + } + else + { + if(position2 == 'A') + { + position3 = 'B'; + } + else + { + position3 = 'A'; + } + } + + if(n==1) + { + printf("%c->%c\n",position1,position2); + return 0; + } + + change(n-1,position1,position3); + change(1,position1,position2); + change(n-1,position3,position2); +} +int main(int argc, char *argv[]) { + int n; + printf("输入塔的层数\n"); + scanf("%d",&n); + change(n,'A','B'); + return 0; +} 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 + +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; +} +