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

交作业 #57

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
31 changes: 31 additions & 0 deletions practices/c/level1/p01_runningLetter/running letter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
int main(void)
{
printf("please input a character:");
char ch;
scanf_s("%c",&ch);
int a=0;
for(;a<100;++a)
{
system("cls");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进!

Sleep(100);
int i=0;
for(;i<a;++i)
printf(" ");
printf("%c",ch);
}
for(;!(a<0);a--)
{
system("cls");
Sleep(100);
int i=0;
for(;i<a;++i)
printf(" ");
printf("%c",ch);
}
return 0;
}


31 changes: 31 additions & 0 deletions practices/c/level1/p02_isPrime/IsPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
bool check(int n);
int main(void)
{
int num;
printf("please input a number : ");
scanf_s("%d", &num);
getchar();
while (num <= 1)
{
printf("error, please input a number is above 1 :");
scanf_s("%d", &num);
getchar();
}
if (check(num))
printf("\n%d is a prime", num);
else
printf("\n%d is not a prime", num);
getchar();
return 0;
}

bool check(int n)
{
for (int i = 2; i < n; ++i)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

循环体和if语句都要用大括号包起来

if (!(n%i))
return false;
return true;
}
14 changes: 14 additions & 0 deletions practices/c/level1/p03_Diophantus/Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<stdio.h>
int main(void)
{
for (int i = 10; i <= 100; ++i)
{
if (84 * i == 84 * 9 + 75 * i) {
printf("when his son died his age is %d", i - 4);
getchar();
return 0;
}

}
return 0;
}
16 changes: 16 additions & 0 deletions practices/c/level1/p04_ narcissus/narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(void)
{
for(int i=100;i<1000;++i)
{
int a=i/100;
int b=i%100/10;
int c=i%100%10;
if(i==pow(a,3)+pow(b,3)+pow(c,3))
printf("%d ",i);
}
getchar();
return 0;
}
34 changes: 34 additions & 0 deletions practices/c/level1/p05_allPrimes/allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
#include<time.h>
#include<stdbool.h>
bool check(int n, int array[]);
int main(void)
{
double op = clock();
int array[10000];
array[0] = 2;
array[1] = 0;
int num = 1;
for(int i=3;i<=10000;++i)
if (check(i, array))
{
array[num] = i;
++num;
array[num] = 0;
}
for (int a = 0; a < num; ++a)
printf("%d\n", array[a]);
double ed = clock();
printf("%lf",(ed - op)/1000);
getchar();
return 0;
}

bool check(int n, int array[])
{

for (int i = 0; array[i] != 0; ++i)
if (!(n%array[i]))
return false;
return true;
}
42 changes: 42 additions & 0 deletions practices/c/level1/p06_Goldbach/goldback.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<stdio.h>
#include<stdbool.h>
#include<time.h>
bool check(int n, int array[]);
int main(void)
{
double op, ed;
op = clock();
int array[10000];
array[0] = 2;
array[1] = 0;
int num = 1;
for (int i = 3; i <= 1000; ++i)
if (check(i, array))
{
array[num] = i;
++num;
array[num] = 0;
}
for (int a = 4; a <= 1000; a += 2)
for (int d = 0; array[d] != 0&&array[d]<=a/2+1; ++d)
{
for (int e = 0; array[e] != 0; ++e)
{
if (array[e] == a - array[d])
printf("%d=%d+%d\n", a, array[d], array[e]);
}
}
ed = clock();
printf("%f", (ed - op) / 1000);
getchar();
return 0;
}

bool check(int n, int array[])
{

for (int i = 0; array[i] != 0; ++i)
if (!(n%array[i]))
return false;
return true;
}
23 changes: 23 additions & 0 deletions practices/c/level1/p08_hanoi/hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<stdio.h>
void hanoi(int n, char a, char b, char c);
int main(void)
{
printf("please input the number you want: ");
int num;
scanf_s("%d", &num);
getchar();
hanoi(num, 'a', 'b', 'c');
getchar();
return 0;
}

void hanoi(int n, char a, char b, char c)
{
if (n == 1)
printf("%d: %c>>%c\n", n, a, c);
else {
hanoi(n - 1, a, c, b);
printf("%d :%c>>%c\n", n, a, c);
hanoi(n - 1, b, a, c);
}
}
119 changes: 119 additions & 0 deletions practices/c/level1/p09_maze/maze.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include<stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include<stdbool.h>
struct maze
{
int x;
int y;
int dir;
};
char m[30][30];
struct maze man;
const int xdir[5] = { 0, 1, 0, -1 ,0};//w a s d
const int ydir[5] = { -1,0,1,0 ,0};//w a s d
void map(void);
void show(void);
void ini(void);
void turn(void);
bool check(void);
void move(void);
void show0(void);
int main(void)
{
ini();
show();
while (true)
{
Sleep(60);
system("cls");
turn();
if (check())
move();
man.dir = 4;
show();
}
getchar();
return 0;
}

void map(void)
{
for (int i = 0; i < 30; ++i)
m[0][i] = '*';
for (int a = 4; a < 30; ++a)
m[a][0] = '*';
for (int b = 1; b < 25; ++b)
m[29][b] = '*';
for (int c = 1; c < 30; ++c)
m[c][29] = '*';
for (int a = 5; a < 10; ++a)
m[a][4] = '*';
for (int b = 1; b < 5; ++b)
m[4][b] = '*';
for (int i = 1; i < 20; ++i)
m[i][14] = '*';
for (int i = 9; i < 20; ++i)
m[19][i] = '*';
for (int i = 24; i < 30; ++i)
m[4][i] = '*';
for (int i = 14; i < 29; ++i)
m[i][24] = '*';
}

void show(void)
{
for (int a = 0; a < 30; ++a)
{
for (int b = 0; b < 30; ++b)
printf("%c", m[a][b]);
printf("\n");
}
}

void ini(void)
{
map();
man.x = 0;
man.y = 1;
m[man.y][man.x] = 'H';
}

void turn(void) {
if (_kbhit()) {
char dir = _getch(); //读取输入的键
switch (dir) { //改变方向
case 119: man.dir = 0; break;
case 100: man.dir =1; break;
case 115: man.dir = 2; break;
case 97: man.dir =3; break;
}
}
}

bool check(void)
{
if (m[man.y + ydir[man.dir]][man.x + xdir[man.dir]] == '*')
return false;
else
return true;

}

void move(void)
{
m[man.y][man.x] = ' ';
man.x = man.x + xdir[man.dir];
man.y = man.y + ydir[man.dir];
m[man.y][man.x] = 'H';
}

void show0(void)
{
for (int a = 0; a < man.y; ++a)
for (int b = 0; b < man.x; ++b)
if (m[man.y][man.x] == 'H')
printf("%c", m[man.y][man.x]);

}
Loading