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

level 1 working program #20

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
530 changes: 527 additions & 3 deletions C语言学习笔记.md

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions level1/p01_runningLetter/RunningLetter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<stdio.h>
#include<stdlib.h>

int main(){
int i,j,k;
while(true){
for(i=0;i<20;i++){
if(i>10){
k=20-i;
}else{
k=i;
}
for(j=0;j<k;j++){
printf(" ");
}
printf("A");
system("cls");
}


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

int main(){
int x,isPrime=0,i;
scanf("%d",&x );
if (x%2==0){
isPrime=1;
}else{
for(i=3;i<=sqrt(x);i+=2){
if(x%i==0){
isPrime=1;
break;
}
}
}

if(isPrime==1){
printf("It's not a Prime\n" );
}else{
printf("It's a Prime\n");
}
return 0;
}
16 changes: 16 additions & 0 deletions level1/p03_Diophantus/Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include<stdio.h>
#include<math.h>

int main(){
float age=2.0;
while (fabs((1.0/6)*age+(1.0/12)*age+(1.0/7)*age+5+(1.0/2)*age+4-age)>=0.01){
age++;
}

printf("When his son died Diophantus is %.0f years old\n",age-4 );

return 0;
}


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>

int main(){
int x,i,j,k;
for(x=100;x<1000;x++){
i=x/100;
j=(x-i*100)/10;
k=(x-i*100-j*10);
if(x==i*i*i+j*j*j+k*k*k){
printf("%d\n",x);
}
}
}
42 changes: 42 additions & 0 deletions level1/p05_allPrimes/allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<Windows.h>

int main(){
int PrimesTable[500];
int i,j,count=0;
int isPrime;
long time1,time2;
int k=0;
PrimesTable[0]=2;

time1=GetTickCount();

for(i=3;i<=1000;i++){
isPrime=1;
for(j=0;PrimesTable[j]<=(int)sqrt(i);j++){
if(i%(PrimesTable[j])==0){
isPrime=0;
break;
}

}
if(isPrime==1){
count++;
PrimesTable[count]=i;
}
}

time2=GetTickCount();
printf("Time is %d\n",(int)(time2-time1));

i=0;
for(i=0;i<=count;i++){
printf("%d ",PrimesTable[i]);
k++;
if(k%5==0){
printf("\n");
}
}
}
43 changes: 43 additions & 0 deletions level1/p06_Goldbach/Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<stdio.h>

int main(){
int PrimesTable[500];
int i,j,k,count=0;
int isPrime;
int Goldbach;
PrimesTable[0]=2;

for(i=3;i<=100;i++){
isPrime=1;
for(j=0;j<count;j++){
if(i%(PrimesTable[j])==0){
isPrime=0;
break;
}

}
if(isPrime==1){
count++;
PrimesTable[count]=i;
}
}
for(i=4;i<=100;i+=2){
Goldbach=0;
for(j=0;j<=count;j++){
for(k=0;k<=count;k++){
if(PrimesTable[j]+PrimesTable[k]==i){
Goldbach=1;

}
}
}

}

if(Goldbach==1){
printf("Goldbach is true within 100");
}else{
printf("Wrong");
}

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

char * encrypt(char [],int key);
char * decrypt(char [],int key);

int main(){
char ps1[128]= {'\0'};
char ps2[128]= {'\0'};
int i,x;
int key;
int result;
gets(ps1);

printf("0 for decrypt and 1 for encrypt.\n");
scanf("%d",&result);
printf("Key please.\n");
scanf("%d",&key);

if(result==0){
decrypt(ps1,key);
}else if(result==1){
encrypt(ps1,key);
}else{
printf("error");
}

printf("%s",ps1);
}

char * encrypt(char ps[],int key){
int x=strlen(ps);
int i;

for(i=0;i<x;i++){
ps[i]+=i+key;
}

return ps;
}

char * decrypt(char ps[],int key){
int x=strlen(ps);
int i;

for(i=0;i<x;i++){
ps[i]-=i+key;
}

return ps;
}


26 changes: 26 additions & 0 deletions level1/p08_hanoi/hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<stdio.h>

void hanoi(int x,char i,char j, char k);

int main(void){
int x;
scanf("%d",&x);
if(x<1){
printf("Wrong");
}else{
hanoi(x,'A','B','C');
}

return 0;
}

void hanoi(int x,char i,char j, char k){

if(x==1){
printf("%c��%c\n",i,k);
}else{
hanoi(x-1,i,k,j);
printf("%c��%c\n",i,k);
hanoi(x-1,j,i,k);
}
}
93 changes: 93 additions & 0 deletions level1/p09_maze/maze.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include<stdio.h>
#include<conio.h>
#include<Windows.h>

// case 72:left
// case 80:right
// case 75:up
// case 77:down



int main(){
int step=0;
int x=1,y=1,i=0,j=0;
int ch1=0,ch2=0;
int maze[8][10]={ //����������

{1,1,1,1,1,1,1,1,2,1},
{1,0,1,0,0,0,0,1,0,1},
{1,0,0,0,0,0,1,0,0,1},
{1,0,0,1,1,0,0,1,0,1},
{1,0,0,1,0,0,0,0,0,1},
{1,0,1,0,1,1,0,1,0,1},
{1,0,0,0,0,0,0,1,0,1},
{1,1,1,1,1,1,1,1,1,1}};

for(i=0;i<8;i++){
for(j=0;j<10;j++){
if(x==i&&y==j){
printf("x");
}else if(maze[i][j]==0){
printf(" ");
}else if(maze[i][j]==2){
printf("!");
}else{
printf("@");
}
}
printf("\n");
}


while(1){



if (ch1=getch()){

ch2=getch();
switch (ch2){
case 75:if(maze[x][y-1]!=1){y-=1;}break;//up
case 77:if(maze[x][y+1]!=1){y+=1;}break;//down
case 72:if(maze[x-1][y]!=1){x-=1;}break;//left
case 80:if(maze[x+1][y]!=1){x+=1;}break;//right
default:break;break;
}

COORD crd = {0, 0};
HANDLE hndl = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hndl, crd);//------����

for(i=0;i<8;i++){
for(j=0;j<10;j++){
if(maze[x][y]==2){
system("cls");
printf("You win!");
break;
}else if(x==i&&y==j){
printf("x");
}else if(maze[i][j]==0){
printf(" ");
}else if(maze[i][j]==2){
printf("!");
}else{
printf("@");
}
}
printf("\n");
}

if(maze[x][y]==2){
break;
}


}
}

}




10 changes: 10 additions & 0 deletions level1/p10_pushBoxes/maze1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1 1
2 2
1 1 1 1 1 1 1 1 2 1
1 0 1 0 0 0 0 1 0 1
1 0 0 0 0 0 1 0 0 1
1 0 0 1 1 0 0 1 0 1
1 0 0 0 0 0 0 0 0 1
1 0 1 0 1 1 0 1 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
10 changes: 10 additions & 0 deletions level1/p10_pushBoxes/maze2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1 1
2 8
1 1 1 1 1 1 1 1 2 1
1 0 1 0 0 0 0 1 0 1
1 0 1 0 0 0 1 0 0 1
1 0 1 1 1 0 0 1 0 1
1 0 0 0 0 0 0 0 0 1
1 0 1 0 1 1 0 1 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
10 changes: 10 additions & 0 deletions level1/p10_pushBoxes/maze3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1 1
2 5
1 1 1 1 1 1 1 1 2 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
Loading