From 64cf41c2d04b02f461989eb29b9ef42f37af0e1e Mon Sep 17 00:00:00 2001 From: Qi Wei Date: Wed, 10 Apr 2019 18:42:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- level1/p01_runningLetter/runningLetter.c | 32 ++++++ level1/p02_isPrime/isPrime.c | 32 ++++++ level1/p03_Diophantus/Diophantus.c | 7 ++ level1/p04_ narcissus/narcissus.c | 13 +++ level1/p05_allPrimes/allPrimes.c | 24 ++++ level1/p06_Goldbach/Goldbach.c | 32 ++++++ level1/p07_encrypt_decrypt/encrypt_decrypt.c | 25 +++++ level1/p08_hanoi/hanoi.c | 32 ++++++ level1/p09_maze/maze.c | 110 +++++++++++++++++++ 9 files changed, 307 insertions(+) create mode 100644 level1/p01_runningLetter/runningLetter.c create mode 100644 level1/p02_isPrime/isPrime.c 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/p07_encrypt_decrypt/encrypt_decrypt.c create mode 100644 level1/p08_hanoi/hanoi.c create mode 100644 level1/p09_maze/maze.c diff --git a/level1/p01_runningLetter/runningLetter.c b/level1/p01_runningLetter/runningLetter.c new file mode 100644 index 00000000..e681dbfb --- /dev/null +++ b/level1/p01_runningLetter/runningLetter.c @@ -0,0 +1,32 @@ +#include +#include +#include +int getwidth() { + CONSOLE_SCREEN_BUFFER_INFO csbi; + int columns, rows; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; + // rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; + return columns; +} +int main() { + int space; + while (1) { + space = getwidth() - 1; + for(int i = 0; i < space; i++) { + for(int j = 0; j < i; j++) { + printf(" "); + } + printf("X"); + system("cls"); + } + for(int i = space; i > 0; i--) { + for(int j = 0; j < i; j++) { + printf(" "); + } + printf("X"); + system("cls"); + } + } + return 0; +} diff --git a/level1/p02_isPrime/isPrime.c b/level1/p02_isPrime/isPrime.c new file mode 100644 index 00000000..36d8fe72 --- /dev/null +++ b/level1/p02_isPrime/isPrime.c @@ -0,0 +1,32 @@ +#include +#include +int main() { + int x, i; + while (1) { + printf("Please input a number: "); + scanf("%d", &x); + while (1) { + if(x <= 0 || x % 1 != 0) { + printf("Please input a positive integer: "); + scanf("%d", &x); + } + else + break; + } + if(x == 1) + printf("%d is not a prime.\n", x); + else if(x == 2) + printf("%d is not a prime.\n", x); + else { + for(i = 2; x % i != 0; i++) { + if (i > sqrt(x)) { + printf("%d is a prime.\n", x); + break; + } + } + if (i <= sqrt(x)) + printf("%d is not a prime.\n", x); + } + } + return 0; +} \ No newline at end of file diff --git a/level1/p03_Diophantus/Diophantus.c b/level1/p03_Diophantus/Diophantus.c new file mode 100644 index 00000000..ef0d7b46 --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.c @@ -0,0 +1,7 @@ +#include +int main() { + int lt; + for(lt = 0; lt - lt/2 - lt/6 - lt/7 -lt/12 != 5 + 4 || lt%2 + lt%6 + lt%7 + lt%12 != 0; lt++) + ; + printf("Diophantus was %d years old when his son died.", lt - 4); +} \ No newline at end of file diff --git a/level1/p04_ narcissus/narcissus.c b/level1/p04_ narcissus/narcissus.c new file mode 100644 index 00000000..97504a6b --- /dev/null +++ b/level1/p04_ narcissus/narcissus.c @@ -0,0 +1,13 @@ +#include +#include +int main() { + int a, b, c, i; + for (i = 100; i < 1000; i++) { + c = i%10; + b = (i%100 - i%10)/10; + a = (i - i%100)/100; + if (pow(a,3) + pow(b,3) + pow(c,3) == i) + printf("%d is a narcissus.\n", i); + } + return 0; +} \ No newline at end of file diff --git a/level1/p05_allPrimes/allPrimes.c b/level1/p05_allPrimes/allPrimes.c new file mode 100644 index 00000000..51081264 --- /dev/null +++ b/level1/p05_allPrimes/allPrimes.c @@ -0,0 +1,24 @@ +#include +#include +#include +int main() { + int start, stop, i, j; + start = GetTickCount(); + int prime[200]; + prime[0] = 2; + int num = 0; + for(i = 3; i <= 1000; i++) { + for(j = 0; i%prime[j] != 0 && j <= num; j++) { + if(prime[j] > sqrt(i)) { + prime[num + 1] = i; + num++; + break; + } + } + } + for(i = 0; i <= num; i++) + printf("%d ",prime[i]); + stop = GetTickCount(); + printf("\ntime: %ld ms\n", stop - start); + return 0; +} diff --git a/level1/p06_Goldbach/Goldbach.c b/level1/p06_Goldbach/Goldbach.c new file mode 100644 index 00000000..6dad1b5e --- /dev/null +++ b/level1/p06_Goldbach/Goldbach.c @@ -0,0 +1,32 @@ +#include +#include +int main() { + int prime[50]; + int i, j, k; + int num = 0; + int flag = 0; + prime[0] = 2; + for(i = 3; i <= 100; i++) { + for(j = 0; i%prime[j] != 0 && j <= num; j++) { + if(prime[j] > sqrt(i)) { + prime[num + 1] = i; + num++; + break; + } + } + } + for(i = 4; i <= 100; i += 2) { + flag = 0; + for(j = 0; prime[j] <= i/2 && flag == 0; j++) { + for(k = 0; k <= num; k++) { + if (prime[k] == i - prime[j]) { + printf("i = %d x = %d y =%d\n", i, prime[j], prime[k]); + flag = 1; + break; + } + } + } + } + printf("Goldbach conjecture is true for the first 100 numbers."); + 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..4e9a18ac --- /dev/null +++ b/level1/p07_encrypt_decrypt/encrypt_decrypt.c @@ -0,0 +1,25 @@ +#include +#include +int main() { + int mode, i; + char s[1000]; + while(1) { + printf("[0] Encrypt\n[1] Decrypt\nPlease choose mode: "); + scanf("%d", &mode); + if(mode == 0) { + printf("Please input plaintext:\n"); + scanf("%s", s); + for(i = 0; i < strlen(s); i++) + s[i] = (s[i]+61-(i+5)%6)%94+33; + printf("%s\n", s); + } + if(mode == 1) { + printf("Please input cipher:\n"); + scanf("%s", s); + for(i = 0; i < strlen(s); i++) + s[i] = (s[i]+61+(i+5)%6)%94+33; + printf("%s\n", s); + } + } + return 0; +} \ No newline at end of file diff --git a/level1/p08_hanoi/hanoi.c b/level1/p08_hanoi/hanoi.c new file mode 100644 index 00000000..ade5eb0d --- /dev/null +++ b/level1/p08_hanoi/hanoi.c @@ -0,0 +1,32 @@ +#include +void hanoi(int n, char a, char b, char c); +void move(char x, char y); + +int main() { + int n; + printf("Please input the number of disks: "); + scanf("%d", &n); + if(n > 15 || n < 1 || n % 1 != 0) { + printf("炸了"); + return 0; + } + hanoi(n, 'A', 'B', 'C'); + return 0; +} + +void hanoi(int n, char a, char b, char c) { + if(n == 1) { + move(a, c); + } + else { + hanoi(n - 1, a, c, b); + move(a, c); + hanoi(n - 1, b, a, c); + } +} + +void move(char x,char y) { + printf("%c -> %c\n", x, y); +} + + \ No newline at end of file diff --git a/level1/p09_maze/maze.c b/level1/p09_maze/maze.c new file mode 100644 index 00000000..b5ae0684 --- /dev/null +++ b/level1/p09_maze/maze.c @@ -0,0 +1,110 @@ +#include +#include + +void control(); +void move(int a, int b); +char getOp(); + +int map[23][25] = { + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {2,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0}, + {0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0}, + {0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0}, + {0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0}, + {0,1,0,1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,1,1,1,1,0}, + {0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0}, + {0,1,1,1,1,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0}, + {0,1,0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,1,0}, + {0,1,1,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,0,1,0}, + {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0}, + {0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,0}, + {0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0}, + {0,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}, + {0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0}, + {0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0}, + {0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,1,1,0}, + {0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0}, + {0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,1,1,0}, + {0,1,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,1,0,0,0}, + {3,1,0,1,1,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, +}; + +struct coordinate { + int x, y; +} p = {1, 0}, d = {21, 0}; + + +void printmap() { + int i, j; + for (i = 0; i < 23; i++) { + printf("\n"); + for (j = 0; j < 25; j++) { + if (map[i][j] == 0) + printf("XX"); + else if (map[i][j] == 1) + printf(" "); + else if (map[i][j] == 2) + printf("**"); + else + printf("##"); + } + } +} + +void control() { + char c = getOp(); + switch(c) { + case 'w': {move(-1, 0); break;} + case 's': {move(1, 0); break;} + case 'a': {move(0, -1); break;} + case 'd': {move(0, 1); break;} + } +} + +void move(int a, int b) { + if (map[p.x + a][p.y + b] != 0 && p.x + a>= 0 && p.x + b>= 0 && p.x < 23 && p.y < 25) { + map[p.x][p.y] = 1; + p.x += a; + p.y += b; + map[p.x][p.y] = 2; + } +} + +char getOp() +{ + INPUT_RECORD keyRec; + DWORD dwRes; + char c = 0; + HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); + while(1) + { + ReadConsoleInput(hIn, &keyRec, 1, &dwRes); + if(keyRec.EventType == KEY_EVENT) + { + if(keyRec.Event.KeyEvent.bKeyDown) + { + c = keyRec.Event.KeyEvent.uChar.AsciiChar; + if(c == 'w' || c == 's' || c == 'a' || c == 'd') + return c; + } + } + } + return c; +} + +int main() { + char c; + printmap(); + while (1) { + control(c); + system("cls"); + printmap(); + if (p.x == d.x && p.y == d.y) { + printf("\nSuccess!\n"); + return 0; + } + } + return 0; +}