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 finished #72

Open
wants to merge 19 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
32 changes: 32 additions & 0 deletions level1/p01_runningLetter/runningLatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<cstdio>
#include<cstdlib>
/*
By HuangChongYi 2018081306006
*/
const int _Maxlength=80
const int time=5000000

using namespace std;
void sleep(){
for (int i=1;i<=time;i++){
}
return;
}
int abs(int x){
return x>0 ? x : -x;
}
int main(){
int blank;
for(int i=0;i<2*_Maxlength;i++){
sleep();
blank=_Maxlength-abs(_Maxlength-i);
for(int j=0;j<blank;j++)
printf(" ");
printf("8\n");
sleep();
system("cls");
}
return 0;
}


18 changes: 18 additions & 0 deletions level1/p02_isPrime/Is_prime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include<cstdio>

using namespace std;

// by huang chong yi 2018081306006
bool Is_prime(int x){
for(int i=2;i*i<=x;i++)
if(x%i==0) return false;
return true;
}

int main(){
int n;
scanf("%d",&n);
if ( Is_prime(n) ) printf("%d is a prime !\n",n);
else printf("%d is not a prime !\n",n);
return 0;
}
16 changes: 16 additions & 0 deletions level1/p03_Diophantus/diophantus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include<cstdio>
#include<cstring>

using namespace std;

// x=1/6 x +1/12 x +1/7 x + 5 + 1/2x + 4

int main(){
double chushu=1.0/6.0 + 1.0/12.0+ 1.0/7.0 + 1.0/ 2.0 ;
double beichushu=5.0+4.0 ;
double x=beichushu / (1.0-chushu);
int intx = int (x+0.5);
int ans = intx-4;
printf("%d\n",ans);
return 0;
}
19 changes: 19 additions & 0 deletions level1/p04_ narcissus/narcissus
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<cstdio>
#include<cstring>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
//by HuangChongYi 2018081306006
using namespace std;
int answer[999];
int answer_count;
int main(){
answer_count=0;
FOR(i,1,9)
FOR(j,0,9)
FOR(k,0,9)
if (i*100+j*10+k==i*i*i+j*j*j+k*k*k)
answer[++answer_count]=i*100+j*10+k;
FOR(i,1,answer_count){
printf("%d\t",answer[i]);
}
return 0;
}
31 changes: 31 additions & 0 deletions level1/p05_allPrimes/AllPrimes
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<cstdio>
#include<cstring>

using namespace std;

#define maxn 1000+10

bool Is_not_prime[maxn];
int prime[maxn];

// the time efficiency is O (n log log n ) in theory
// by huang chong yi 2018081306006
int min(int a,int b){
return a < b ? a : b;
}
int main(){
memset(Is_not_prime,0,sizeof(Is_not_prime)); // 0 means prime and 1 means not a prime;
memset(prime,0,sizeof(prime));
int cnt=0; // count the prime;
for (int i=2;i<=1000;i++)
if (! Is_not_prime[i]){ // is a prime
for(int j=i*2;j<=1000;j+=i)
Is_not_prime[j]=true;
prime[++cnt]=i;
}
for(int i=1;i<=cnt;i++){
printf("\t%d ",prime[i]);
if (i%10==0) printf("\n"); // print with every 10 primes in a line ;
}
return 0;
}
26 changes: 26 additions & 0 deletions level1/p06_Goldbach/Goldbach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<cstdio>
#include<cstring>

using namespace std;
#define FOR(i,a,b) for(int i=a;i<=b;i++)
bool is_prime(int x){
for(int i=2;i*i<=x;i++){
if (x%i==0) return false;
}
return true;
}
int main(){
int flag;
FOR(i,4,100)
if (!(i&1)){ // i%2==0
flag=1;
FOR(j,i>>1,i)
if(flag)
if(is_prime(j)&&is_prime(i-j)){
printf("%d=%d+%d\n",i,i-j,j);
flag=0;
}
if (flag) printf("Congratulation! You have proved goldbach is wrong !!!!!\n");
}
return 0;
}
25 changes: 25 additions & 0 deletions level1/p07_encrypt_decrypt/encrypt_decrypt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//encrypt_decrypt : only can transform A~Z (65-90)and a~z (97-122)
//by huangchongyi 2018081306006
#include<cstdio>
#include<cstring>
using namespace std;
char key[11]="I HATE YOU"; //the key to encrypt or decrypt
void encrypt_or_decrypt(char *a,int sigma){ //sigma is to show to encrypt or decrypt
for(int i=0;a[i]!='\0';i++){
if (a[i]>='a'&&a[i]<='z') a[i]=(26+a[i]-'a'+sigma*((key[i%10]-'a')%26))%26 +'a';
if (a[i]>='A'&&a[i]<='Z') a[i]=(26+a[i]-'A'+sigma*((key[i%10]-'A')%26))%26 +'A';
}
return;
}
int main(){
char passage[100]="I LOVE you i love YOU I LOVE you i love YOU I LOVE you i love YOU I LOVE you i love YOU ";
printf("the origin :%s\n",passage);

encrypt_or_decrypt(passage,1);
printf("the encrypt : %s\n",passage);

encrypt_or_decrypt(passage,-1);
printf("the decrypt : %s\n",passage);

return 0;
}
18 changes: 18 additions & 0 deletions level1/p08_hanoi/hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include<cstdio>

using namespace std;

// by huang chong yi 2018081306006
void move(int brick,char place_a,char place_b){
if(brick==0) return ;
move(brick-1,place_a,294-place_a-place_b); // 294 is the sum of 'a' , 'b' and 'c' ;
printf("move the %dth from %c to %c\n",brick,place_a,place_b);
move(brick-1,294-place_a-place_b,place_b);
}

int main(){
int n;
scanf("%d",&n);
move(n,'a','c'); //the total move is 2^n -1 ;
return 0;
}
122 changes: 122 additions & 0 deletions level1/p09_maze/maze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// I don't know if I set the target in my own account successfully

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<time.h>
#include<conio.h>
int map[100][100]; // 0 is road ,1 is block, 3 is person, 2 is terminal
int n=11,m=20;
#define alpha (1)
#define beta 60
#define sleep_time 5000
void sleep(){
for(int i=1;i<sleep_time;i++);
return ;
}
int random(int l,int r){
return rand()%(r-l+1) +l;
}
void print(){
printf("Please use UP, DWON, LEFT and RIGHT to go out of the maze\nYou are in ● and the exit is □\n\n");
for(int i=0;i<=n*2;i++){
for(int j=0;j<=m*2;j++)
if (map[i][j]==1) printf("█"); //because space should be a two-char ,so it can't be put in a map of char;
else if (map[i][j]==0) printf(" "); // so I can only use if and else
else if (map[i][j]==2) printf("□"); // exit
else if (map[i][j]==3) printf("●"); // player
printf("\n");
}
}

void build(int up,int dn,int lt,int rt){ // build the maze by divide it into two part and connet these two part
if(up==dn&&lt==rt) return; // and then build that two part in the same way
int d=random(0,1);
if ( (dn-up)>=(rt-lt)*alpha ) d=1;
if ( (rt-lt)>=(dn-up)*alpha ) d=0;
if (up==dn) d=0;
if (lt==rt) d=1;
if ( d ){
int d1=random(up,dn-1);
int d2=random(lt,rt);
map[d1*2][d2*2-1]=0;
build(up,d1,lt,rt);
build(d1+1,dn,lt,rt);
}
if ( !d ){
int d1=random(lt,rt-1);
int d2=random(up,dn);
map[d2*2-1][d1*2]=0;
build(up,dn,lt,d1);
build(up,dn,d1+1,rt);
}
// if (dn-up>=2&&rt!=m&&rt-lt>=2&&dn!=n) // make the map be a forest but not a single tree , and the bigger beta is ,the more trees is would be
// if (random(1,100)<beta)
// map[(dn+up)/2*2-1][(rt+lt)/2*2]=0;
return ;
}

int player_x,player_y,exit_x,exit_y;

void init_maze(){
srand( (unsigned)time( NULL ) );
for(int i=0;i<=n*2;i++) //the cell is in (i*2-1,j*2-1)
for(int j=0;j<=m*2;j++){
if(i%2+j%2==2) map[i][j]=0;
else map[i][j]=1;
}
build(1,n,1,m);
// build(1,n,1,m); if I use the funtion twice , the maze will be easier to solve and the map won't look like a " tree "
player_x=random(1,n/2-1)*2-1;
player_y=random(1,m/2-1)*2-1;
exit_x=random(n/2,n)*2-1;
exit_y=random(m/2,m)*2-1;
map[player_x][player_y]=3;
map[exit_x][exit_y]=2;
return ;
}
int mov[5][2]={{0,0},{1,0},{-1,0},{0,-1},{0,1}};
int get_d(){
char ch=getch();
if (ch==-32){
ch=getch();
if (ch==72) return 2;
if (ch==80) return 1;
if (ch==75) return 3;
if (ch==77) return 4;
return 0;
}
return 0;
}
int move(int d){ // d is short for direction
int nx=player_x+mov[d][0];
int ny=player_y+mov[d][1];
if (nx>=0&&nx<=2*n)
if (ny>=0&&ny<=2*m)
if (map[nx][ny]!=1){
if (map[nx][ny]==2) return 1; // success
map[player_x][player_y]=0;
player_x=nx; player_y=ny;
map[player_x][player_y]=3;
}
return 0;
}
void game_start(){
init_maze();
print();
int success=0;
while(!success){
success=move( get_d() );
system("cls");
print();
sleep();
}
sleep();sleep();sleep();
system("cls");
printf("YOU WIN !!!");
return;
}
int main(){
game_start();
return 0;
}
8 changes: 8 additions & 0 deletions level1/p10_pushBoxes/map1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
7 7
5 5 5 5 5 5 5
5 0 0 2 0 0 5
5 0 0 0 0 0 5
5 0 0 1 0 0 5
5 0 0 0 0 0 5
5 0 3 0 0 0 5
5 5 5 5 5 5 5
11 changes: 11 additions & 0 deletions level1/p10_pushBoxes/map2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
10 10
5 5 5 5 5 5 5 5 5 5
5 2 0 0 0 0 0 0 0 5
5 0 1 0 0 0 0 3 0 5
5 0 0 0 0 0 0 0 0 5
5 0 0 0 5 5 0 0 0 5
5 0 0 0 5 5 0 0 0 5
5 0 0 0 0 0 0 0 0 5
5 0 3 0 0 0 0 1 0 5
5 0 0 0 0 0 0 0 0 5
5 5 5 5 5 5 5 5 5 5
13 changes: 13 additions & 0 deletions level1/p10_pushBoxes/map3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
12 12
5 5 5 5 5 5 5 5 5 5 5 5
5 2 0 0 0 0 0 0 0 0 0 5
5 0 3 0 0 3 0 1 0 0 0 5
5 0 0 0 5 0 0 0 5 0 0 5
5 0 0 0 5 0 0 0 5 0 0 5
5 0 0 0 5 0 1 0 5 0 0 5
5 0 0 0 5 0 0 0 5 0 0 5
5 0 0 0 5 5 5 5 5 0 0 5
5 0 1 0 0 0 0 0 0 0 0 5
5 5 5 0 0 0 0 0 0 3 0 5
5 5 5 0 0 0 0 0 0 0 0 5
5 5 5 5 5 5 5 5 5 5 5 5
Loading