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

c practices level1 -- 孔淳漳 #28

Open
wants to merge 14 commits 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
35 changes: 35 additions & 0 deletions practices/c/level1/p01_runningLetter/p01_Runningletter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <string.h>
#include <windows.h>

int main()
{
char c;
scanf("%c", &c);
system("mode con cols=16 lines=9");
int width = GetSystemMetrics ( SM_CXSCREEN );
for (int i = 0; i < 32; i++)
{
system("cls");
if(i<16)
{
for(int j = 0; j < i; j++)
{
printf(" ");
}
printf("%c", c);
Sleep(100);
}
else
{
for (int j = 0; j < 31 - i; j++)
{
printf (" ");
}
printf ("%c", c);
Sleep (100);
printf ("\n");
}
}
return 0;
}
19 changes: 19 additions & 0 deletions practices/c/level1/p02_isPrime/p02_isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<stdio.h>

int main(void)
{
int i, n;
scanf("%d", &n);
for(i = 2; i < n; i++)
{
if(n % i == 0)
{
break;
}
}
if(i == n)
printf("%d is a prime\n", n);
else
printf("%d isn't a prime\n", n);
return 0;
}
15 changes: 15 additions & 0 deletions practices/c/level1/p03_Diophantus/p03_Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include<stdio.h>

int main(void)
{
float i;
for(i = 0; i < 150; i++)
{
if((i / 6 + i / 12 + i / 7 + 5 + i / 2 + 4) == i)
{
printf("When Diophantus's son dead, Diophantus's age is %g", i - 4);
break;
}
}
return 0;
}
14 changes: 14 additions & 0 deletions practices/c/level1/p04_ narcissus/p04_Narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<stdio.h>
#include<math.h>

int main(void)
{
for(int i=100; i < 1000; i++)
{
if(pow (i / 100, 3) + pow((i % 100) / 10, 3) + pow(i % 10, 3) == i)
{
printf("%d\n", i);
}
}
return 0;
}
2 changes: 2 additions & 0 deletions practices/c/level1/p05_allPrimes/allprime.txt

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions practices/c/level1/p05_allPrimes/p05_allprime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdbool.h>
#define N 1000001

int main()
{
float start, finish;
start = clock();
bool prime[N];
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for(int i = 2; i < N; i++)
{
if(prime[i])
for(int j = i * 2; j < N; j += i)
{
prime[j] = false;
}
}
FILE * fp;
freopen("allprime.txt", "w", stdout);
for(int k = 0; k <N; k++)
{
if(prime[k])
printf("%d ", k);
}
finish = clock();
printf("\n%g\n", (finish - start) / CLOCKS_PER_SEC);
return 0;
}
31 changes: 31 additions & 0 deletions practices/c/level1/p06_Goldbach/p06_Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<stdio.h>

int isprime(int i);
int main()
{
for(int x = 4; x < 101; x += 2)
{
for(int y = 2; y <= x / 2; y++)
{
if(isprime(y) != 0)
if(isprime(x - y) != 0)
printf("%d = %d + %d\n", x, y, x - y);
}
}
printf("Proof done.\n");
return 0;
}

int isprime(int i)
{
int j;
for(j = 2; j < i; j++)
{
if(i % j == 0)
break;
}
if(i == j)
return 1;
else
return 0;
}
37 changes: 37 additions & 0 deletions practices/c/level1/p07_encrypt_decrypt/p07_encrypt_decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<stdio.h>
#include<string.h>

int main()
{
int i, n, command;
char text[256] = {'\0'}, encryption[256] = {'\0'};
printf("Plz enter your command.\n");
printf("1.Encrypt\n");
printf("2.Decrypt\n");
printf("3.Exit\n");
scanf("%d", &command);
if(command == 1)
{
printf("Plz enter the words you want to encrypt.\n");
scanf("%s", &text);
n = strlen(text);
for(i = 0; i < n; i++)
encryption[i] = text[i] + i + 6;
encryption[i] = '\0';
printf("Encrypt words are %s.\n", encryption);
}
else if(command == 2)
{
printf("Plz enter the words you want to decrypt.\n");
scanf("%s", &encryption);
n = strlen(encryption);
for(i = 0; i < n; i++)
text[i] = encryption[i] - i - 6;
text[i] = '\0';
printf("Decrypt words are %s.\n", text);
}
else if(command == 3)
return 0;
else
printf("Wrong command,plz try again.\n");
}
23 changes: 23 additions & 0 deletions practices/c/level1/p08_hanoi/p08_hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<stdio.h>

void move_record(int n, char a, char b, char c);

int main()
{
int n;
scanf("%d", &n);
move_record(n, 'a', 'b', 'c');
return 0;
}

void move_record(int n, char a, char b, char c)
{
if(n == 1)
printf("%c -> %c\n", a, c);
else
{
move_record(n-1, a ,c ,b);
printf("%c -> %c\n", a, c);
move_record(n-1, b, a ,c);
}
}
104 changes: 104 additions & 0 deletions practices/c/level1/p09_maze/p09_maze.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include<stdio.h>
#include<windows.h>
#define row 8
#define culomn 8

char maze[row][culomn] =
{
{'*', '*', '*', '*', '*', '*', '*', '*'},
{'*', '8', '*', '*', '*', '*', '*', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', '*', ' ', '*', '*', '*', ' ', '*'},
{'*', ' ', ' ', '*', ' ', ' ', ' ', '*'},
{'*', ' ', '*', '*', ' ', '*', '*', '*'},
{'*', ' ', '*', ' ', ' ', ' ', ' ', ' '},
{'*', '*', '*', '*', '*', '*', '*', '*'},
};

int x = 1, y = 1;
int nextx, nexty;
char street = ' ';

void print_maze();
void move_to_next_point();
void calculate_next_point(char direction);

void print_maze()
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < culomn; j++)
{
printf("%c", maze[i][j]);
}
printf("\n");
}
}

void calculate_next_point(char direction)
{
switch(direction)
{
case 'w':
{
nextx = x - 1;
break;
}
case 's':
{
nextx = x + 1;
break;
}
case 'a':
{
nexty = y - 1;
break;
}
case 'd':
{
nexty = y + 1;
break;
}
default:
break;
}
}

void move_to_next_point()
{
if(maze[nextx][nexty] == street)
{
char temp = maze[nextx][nexty];
maze[nextx][nexty] = maze[x][y];
maze[x][y] = temp;
x = nextx;
y = nexty;
}
else
{
nextx = x;
nexty = y;
}
}

int main()
{
nextx = x;
nexty = y;
print_maze();
char direction;
while(1)
{
scanf("%c", &direction);
system("cls") ;
calculate_next_point(direction);
move_to_next_point();
print_maze();
if(x == row - 2 && y == culomn - 1)
{
printf("You win\n");
break;
}
}
return 0;
}
8 changes: 8 additions & 0 deletions practices/c/level1/p10_pushBoxes/easy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
0 0 1 1 1 0 0 0 0 0
0 0 1 4 1 0 0 0 0 0
0 0 1 2 1 1 1 1 0 0
1 1 1 0 0 2 4 1 0 0
1 4 0 2 3 1 1 1 0 0
1 1 1 1 2 1 0 0 0 0
0 0 0 1 4 1 0 0 0 0
0 0 0 1 1 1 0 0 0 0
8 changes: 8 additions & 0 deletions practices/c/level1/p10_pushBoxes/hard.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1 1 1 1 1 1 1 0 0 0
1 0 0 4 2 0 1 1 1 0
1 0 4 2 4 2 0 0 1 0
1 5 2 4 2 4 3 0 1 0
1 0 4 2 4 2 0 1 1 0
1 0 0 4 2 0 0 1 0 0
1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0
8 changes: 8 additions & 0 deletions practices/c/level1/p10_pushBoxes/normal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
0 1 1 1 1 1 1 1 0 0
0 1 0 0 0 0 0 1 1 1
1 1 2 1 1 1 0 0 0 1
1 3 0 0 2 0 0 2 0 1
1 0 4 4 1 0 2 0 1 1
1 1 4 4 1 0 0 0 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
Loading