Skip to content
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
42 changes: 42 additions & 0 deletions level1/p01_runningLetter/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
/* 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;
}
34 changes: 34 additions & 0 deletions level1/p02_isPrime/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#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<number;i++){
if(number==MIN_PRIME){
goto is_prime;
}
else if(number%i!=0){
continue;
}
else{
goto is_not_prime;
}
}

/*print*/
is_prime:
printf("%d is a prime.\n",number);
return 0;
is_not_prime:
printf("%d is not a prime.\n",number);
return 0;
}
10 changes: 10 additions & 0 deletions level1/p03_Diophantus/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>

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;
}
27 changes: 27 additions & 0 deletions level1/p04_ narcissus/p04_ narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* 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;
}
42 changes: 42 additions & 0 deletions level1/p05_allPrimes/p05_allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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<number;i++)
{
if(number==MIN_PRIME)
{
return 1;
}
else if(number%i!=0)
{
continue;
}
else
{
return 0;
}
}

}
18 changes: 18 additions & 0 deletions level1/p06_Goldbach/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>

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;
}
20 changes: 20 additions & 0 deletions level1/p07_encrypt_decrypt/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>

/* 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;
}
59 changes: 59 additions & 0 deletions level1/p08_hanoi/hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>

/* 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("�������IJ���\n");
scanf("%d",&n);
change(n,'A','B');
return 0;
}
136 changes: 136 additions & 0 deletions level1/p09_maze/maze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#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<ROW; i++){
for (int j = 0; j<COL; j++,p++){
//if (*p == 43 || *p == 64){
printf(" ");
//}
printf("%c",*p);
}
printf("\n");
}

printf("�Թ�С��Ϸ\n");
printf("ͼ����\n");
printf("%c --ǽ��, %c --��·, %c --����\n",35,43,64);
printf("����w, a, s, d�ƶ�����\n");
printf("w -- ����, a -- ����, s -- ����, d -- ����\n");

}

void goto_xy(int x,int y){ //��λ���λ�õ�ָ������
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos={x,y};
SetConsoleCursorPosition(hOut,pos);
}

void output(int (*p)[COL],int x,int y,char ch){ //ʵ������ƶ�

if (ch == 'w'){
*(*(p+x)+y) = 64;
system("cls");
print((int *) p);
goto_xy(0,20);
}
else {
*(*(p+x)+y) = 43;
system("cls");
print((int *) p);
goto_xy(0,20);
}
}

int isWall(int (*p)[COL],int x,int y){ //�ж�����ƶ�λ���Ƿ�Ϊǽ��
if (*(*(p+x)+y) == 43){
return 0;
}
else return 1;
}

void move(int (*p)[COL],char ch,int *px,int *py){ //ʵ������ƶ�
int x = *px, y = *py;
switch (ch){
case 'w':
if (isWall(p,x-1,y) == 0){
output(p,x,y,'b');
output(p,x-1,y,'w');
*px = x-1;
}
break;
case 'a':
if (isWall(p,x,y-1) == 0){
output(p,x,y,'b');
output(p,x,y-1,'w');
*py = y-1;
}
break;
case 's':
if (isWall(p,x+1,y) == 0){
output(p,x,y,'b');
output(p,x+1,y,'w');
*px = x+1;
}
break;
case 'd':
if (isWall(p,x,y+1) == 0){
output(p,x,y,'b');
output(p,x,y+1,'w');
*py = y+1;
}
break;
default:;
}
}



Loading