diff --git a/level1/p01_runningLetter/runningLatter.cpp b/level1/p01_runningLetter/runningLatter.cpp new file mode 100644 index 00000000..6ef1bd3d --- /dev/null +++ b/level1/p01_runningLetter/runningLatter.cpp @@ -0,0 +1,32 @@ +#include +#include +/* + 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 + +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; +} diff --git a/level1/p03_Diophantus/diophantus.cpp b/level1/p03_Diophantus/diophantus.cpp new file mode 100644 index 00000000..a1a2c556 --- /dev/null +++ b/level1/p03_Diophantus/diophantus.cpp @@ -0,0 +1,16 @@ +#include +#include + +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; +} diff --git a/level1/p04_ narcissus/narcissus b/level1/p04_ narcissus/narcissus new file mode 100644 index 00000000..9c3076af --- /dev/null +++ b/level1/p04_ narcissus/narcissus @@ -0,0 +1,19 @@ +#include +#include +#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; +} diff --git a/level1/p05_allPrimes/AllPrimes b/level1/p05_allPrimes/AllPrimes new file mode 100644 index 00000000..c8dee783 --- /dev/null +++ b/level1/p05_allPrimes/AllPrimes @@ -0,0 +1,31 @@ +#include +#include + +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; +} diff --git a/level1/p06_Goldbach/Goldbach.cpp b/level1/p06_Goldbach/Goldbach.cpp new file mode 100644 index 00000000..d15332c3 --- /dev/null +++ b/level1/p06_Goldbach/Goldbach.cpp @@ -0,0 +1,26 @@ +#include +#include + +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; +} diff --git a/level1/p07_encrypt_decrypt/encrypt_decrypt.cpp b/level1/p07_encrypt_decrypt/encrypt_decrypt.cpp new file mode 100644 index 00000000..d921c583 --- /dev/null +++ b/level1/p07_encrypt_decrypt/encrypt_decrypt.cpp @@ -0,0 +1,25 @@ +//encrypt_decrypt : only can transform A~Z (65-90)and a~z (97-122) +//by huangchongyi 2018081306006 +#include +#include +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; +} diff --git a/level1/p08_hanoi/hanoi.cpp b/level1/p08_hanoi/hanoi.cpp new file mode 100644 index 00000000..fb5f97cc --- /dev/null +++ b/level1/p08_hanoi/hanoi.cpp @@ -0,0 +1,18 @@ +#include + +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; +} diff --git a/level1/p09_maze/maze.cpp b/level1/p09_maze/maze.cpp new file mode 100644 index 00000000..f5411e6d --- /dev/null +++ b/level1/p09_maze/maze.cpp @@ -0,0 +1,122 @@ +// I don't know if I set the target in my own account successfully + +#include +#include +#include +#include +#include +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=(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)=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; +} diff --git a/level1/p10_pushBoxes/map1.txt b/level1/p10_pushBoxes/map1.txt new file mode 100644 index 00000000..c7297da6 --- /dev/null +++ b/level1/p10_pushBoxes/map1.txt @@ -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 diff --git a/level1/p10_pushBoxes/map2.txt b/level1/p10_pushBoxes/map2.txt new file mode 100644 index 00000000..4b789ad3 --- /dev/null +++ b/level1/p10_pushBoxes/map2.txt @@ -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 diff --git a/level1/p10_pushBoxes/map3.txt b/level1/p10_pushBoxes/map3.txt new file mode 100644 index 00000000..530d7467 --- /dev/null +++ b/level1/p10_pushBoxes/map3.txt @@ -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 diff --git a/level1/p10_pushBoxes/pushboxes.cpp b/level1/p10_pushBoxes/pushboxes.cpp new file mode 100644 index 00000000..7371c6a0 --- /dev/null +++ b/level1/p10_pushBoxes/pushboxes.cpp @@ -0,0 +1,105 @@ +// □ stand for box v==1 +// ■ stand for player v==2 +// ○ stand for the place the box should be put in v===3 +// ● stand for the place with a box in it v==4 +// █ stand for block v==5 +#include +#include +#include +#include +const int maxn=20; +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 mov[5][2]={{0,0},{1,0},{-1,0},{0,-1},{0,1}}; +struct pushbox{ + int map[maxn][maxn]; + int width,height; + int length,sumlength,score; + int remain; + int player_x,player_y; + void init(){ + remain=0; + for(int i=1;i<=height;i++) + for(int j=1;j<=width;j++) { + if(map[i][j]==1) remain++; + if(map[i][j]==2) {player_x=i,player_y=j;} + } + } + void print(){ + for(int i=1;i<=height;i++){ + for(int j=1;j<=width;j++) + if (!map[i][j]) printf(" "); + else if (map[i][j]==1) printf("□"); + else if (map[i][j]==2) printf("■"); + else if (map[i][j]==3) printf("○"); + else if (map[i][j]==4) printf("●"); + else if (map[i][j]==5) printf("█"); + printf("\n"); + } + } + int one_step(int d){ + int nx=player_x+mov[d][0]; + int ny=player_y+mov[d][1]; + if (nx>=1&&nx<=height) + if (ny>=1&&ny<=width) + if (map[nx][ny]!=5&&map[nx][ny]!=4&&map[nx][ny]!=3) + if (map[nx][ny]==0) {map[nx][ny]=2;map[player_x][player_y]=0;player_x=nx;player_y=ny;} + if (map[nx][ny]==1){ + int nnx=nx+mov[d][0],nny=ny+mov[d][1]; + if (nnx>=1&&nnx<=height) + if (nny>=1&&nny<=width) + if (map[nnx][nny]==0) { map[nx][ny]=2;map[nnx][nny]=1;map[player_x][player_y]=0; player_x=nx;player_y=ny;} + if (map[nnx][nny]==3) { map[nx][ny]=2;map[nnx][nny]=4;map[player_x][player_y]=0; remain--;player_x=nx;player_y=ny;} + } + if (remain) return 0; + return 1; + } + + void load(int level){ + FILE *fp; + if (level==1) fp = fopen("map1.txt","r"); + if (level==2) fp = fopen("map2.txt","r"); + if (level==3) fp = fopen("map3.txt","r"); + + fscanf(fp,"%d%d",&width,&height); + for(int i=1;i<=height;i++) + for(int j=1;j<=width;j++) fscanf(fp,"%d",&map[i][j]); + fclose(fp); + } + + + +}game; + +void game_start(){ + int level=1; + int success; + while (level<=3){ + game.load(level); + game.init(); game.print(); + success=0; + while (!success){ + success=game.one_step(get_d()); + system("cls"); + game.print(); + } + system("cls"); + printf("YOU WIN !!! Level Up!!!"); + level++; + } +} +int main(){ + game_start(); + return 0; + +} diff --git a/level1/p11_linkedList/linkedlist.cpp b/level1/p11_linkedList/linkedlist.cpp new file mode 100644 index 00000000..7aefe00f --- /dev/null +++ b/level1/p11_linkedList/linkedlist.cpp @@ -0,0 +1,80 @@ +#include +#include + +using namespace std; + +struct Node{ + int val,id; + Node* next; + Node(int va,int i){ + val=va;id=i; + next=NULL; + } +}*head=NULL; +int n; +void insert(Node* &o,int v,int i){ + if(!o){ + o=new Node(v,i); + return; + } + insert(o->next,v,i); + return; +} +void print(Node* o){ + for(;o;o=o->next){ + printf("%d %d\n",o->val,o->id); + } + return; +} +void build(Node* &o,int i){ + if(i>n) return; + int a; + scanf("%d",&a); + insert(o,a,i); + build(o->next,i+1); + + return ; +} +Node* rebuild(Node* o,int i){ + if ( i==1 ) return o; + if ( i==2 ) { + o->next->id=1; + insert(o->next,o->val,2); + delete o; + return o->next; + } + o->next=rebuild(o->next,i-1); + insert(o->next,o->val,i); + delete o; + return o->next; +} +Node* search(Node *o,int x){ + if(!o) return NULL; + if(o->val==x) return o; + return search(o->next,x); +} +int main(){ + scanf("%d",&n); + // the requirement 1 2 + build(head,1); + print(head); + + // the requirement 3 + head=rebuild(head,n); + print(head); + + // the requirement 4 + Node* p=search(head,5); + if(!p){ + printf("-1\n"); + } + else { + printf("%d\n",p->id); + + // the requirement 5 + Node *nextp=search(p->next,5); + if (!nextp) printf("-1\n"); + else printf("%d\n",nextp->id); + } + return 0; +} diff --git a/level1/p12_warehouse/warehouse.cpp b/level1/p12_warehouse/warehouse.cpp new file mode 100644 index 00000000..59a4dd37 --- /dev/null +++ b/level1/p12_warehouse/warehouse.cpp @@ -0,0 +1,136 @@ +// just put save.txt and load.txt in the same direction of warehouse.cpp is ok . +// it doesn't matter if you don't create the two txt , my program take it in to consideration + +#include +#include +#include + +using namespace std; + +struct Item{ + int amount,id; + char name[10]; + Item *next; + Item(int i,int n,char *s): id(i),amount(n),next(NULL) { + strcpy(name,s); + } + void print(){ + printf("the name of the %d th item : %s , and the amount is %d\n",id,name,amount); + if (next) next->print(); + else printf("we have these %d items above in total\n",id); + } + +}*head=NULL; + +void modify(Item* &o,char* s,int amount,int sigma){ // sigma=1 is in_warehouse and sigma=-1 is out_warehouse + if (!o && sigma==1){ // the first item so the id=1 + o=new Item(1,amount,s); + printf("Add successfully !\n"); + return ; + } + if (!o && sigma==-1){ + printf("ERROR! we don't have such item named '%s'\n",s); + return; + } + if ( strcmp(s,o->name) ==0 ){ + if (sigma==1){ + o->amount+=amount; + printf("Add successfully !\n"); + return ; + } + if (sigma==-1 && amount > o->amount ){ + printf("ERROR! we have this item but we don't have enough, we only have %d\n",o->amount); + return ; + } + if (sigma==-1 && amount <= o->amount ){ + o->amount-=amount; + printf("Delete successfully !\n"); + } + if (o->amount==0){ // exactly out this item with no remain so attach the next to the ahead and delete this pointer + Item* p=o; + o=o->next; + delete(p); // to save memory + return ; + } + return ; + } + if ( !o->next ) { + if (sigma==-1) { + printf("ERROR! we don't have such item named '%s'\n",s); + } + else { // is not the first item so the id is o->id + 1 + o->next=new Item(o->id+1,amount,s); + printf("Add successfully !\n"); + } + return ; + } + modify(o->next,s,amount,sigma); + return ; +} + +void print_instruction(){ + printf("please choose your requirment ,just input the number \n"); + printf("1: Show the inventory\n"); + printf("2: put something in storage\n"); + printf("3:take out something from storage\n"); + printf("4: save and end\n"); +} +void print(){ // op==1 + if( !head ) printf("We have no storage at present \n"); + else head->print(); + system("pause"); +} + +char read_name[10]; +int read_amount; +void add(){ // op==2 + printf("please imput the name , the amount\n"); + scanf("%s%d",read_name,&read_amount); + modify(head,read_name,read_amount,1); + system("pause"); +} +void delet(){ // op==3 + printf("please imput the name , the amount\n"); + scanf("%s%d",read_name,&read_amount); + modify(head,read_name,read_amount,-1); + system("pause"); +} +void save(){ // op==4 + FILE *fp = fopen("save.txt","w"); + Item *o=head; + while(o){ + fprintf(fp,"%s %d\n",o->name,o->amount); + o=o->next; + } + fclose(fp); + printf("save successfully !\n press any key to END THE PROGRAM "); +} +void load(){ + FILE *fp = fopen("load.txt","r"); + while(fp && fscanf(fp,"%s%d",read_name,&read_amount)==2){ + modify(head,read_name,read_amount,1); + } + system("cls"); + printf("Program Start !\n"); + if (fp) printf(" Load successfully !") ; // different output defends on whether we have load.txt + fclose(fp); + system("pause"); +} +int main(){ + load(); + system("cls"); + print_instruction(); + int op; // op is short for "operation" + while ( scanf("%d",&op) ){ + system("cls"); + if (op==1) print(); + else if (op==2) add(); + else if (op==3) delet(); + else if (op==4) { + save(); break; + } + system("cls"); + print_instruction(); + } + return 0; +}