Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提交作业 #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions level1/p01_runningLetter/runningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
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;
}
32 changes: 32 additions & 0 deletions level1/p02_isPrime/isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <math.h>
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;
}
7 changes: 7 additions & 0 deletions level1/p03_Diophantus/Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
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);
}
13 changes: 13 additions & 0 deletions level1/p04_ narcissus/narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include <math.h>
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;
}
24 changes: 24 additions & 0 deletions level1/p05_allPrimes/allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <math.h>
#include <windows.h>
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;
}
32 changes: 32 additions & 0 deletions level1/p06_Goldbach/Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <math.h>
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;
}
25 changes: 25 additions & 0 deletions level1/p07_encrypt_decrypt/encrypt_decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <string.h>
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;
}
32 changes: 32 additions & 0 deletions level1/p08_hanoi/hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
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);
}


110 changes: 110 additions & 0 deletions level1/p09_maze/maze.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <stdio.h>
#include <windows.h>

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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};

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;
}