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
26 changes: 26 additions & 0 deletions level1/p01_runningLetter/running-letter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

void print_1(int i)
{
int j=0;
while (j<i) {printf(" ");j++;}
}

void print_2(int i)
{
print_1(i);
printf("C\n");
Sleep(70);
system("cls");
}

int main()
{
int i;
system("mode con cols=101 lines=30 ");
for (i=0;i<=100;i++) print_2(i);
for (i=100;i>=0;i--) print_2(i);
return 0;
}
37 changes: 37 additions & 0 deletions level1/p02_isPrime/isPrime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <math.h>

int judge(int num)
{
int i,judge=0;
for (i=2;i<=sqrt(num);i++)
{
judge=num%i;
if (judge==0)
{
break;
}
else continue;
}
if (num==2||num==3)
{
judge=1;
}
return judge;
}

int main()
{
int num;
printf("Please enter a nunber:\n");
scanf("%d",&num);
if (judge(num)==0)
{
printf("%d is not a prime number.\n",num);
}
else
{
printf("%d is a prime number.\n",num);
}
return 0;
}
15 changes: 15 additions & 0 deletions level1/p03_Diophantus/Diophantus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include<stdio.h>

int main()
{
int x;
for (x=1;x<200;x++)
{
if ((x/6.0+x/12.0+x/7.0+5+x/2.0+4)==(x/1.0))
{
printf("Diophantus was %d years old\n",x);
}
else continue;
}
return 0;
}
30 changes: 30 additions & 0 deletions level1/p04_ narcissus/narcissus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# include <stdio.h>
# include <math.h>

int judge(int n)
{
int unit,tens,hundreds,m=n;
hundreds=n/100;
n-=hundreds*100;
tens=n/10;
unit=n-tens*10;
if ((pow(unit,3)+pow(tens,3)+pow(hundreds,3))==m)
{
return 1;
}
else return 0;
}

int main()
{
int i;
for (i=100;i<1000;i++)
{
if(judge(i)==1)
{
printf("%-4d",i);
}
else continue;
}
return 0;
}
41 changes: 41 additions & 0 deletions level1/p05_allPrimes/allPrimes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <time.h>

int judge(int num)
{
int i,judge=0;
for (i=2;i<(num/2+1);i++)
{
judge=num%i;
if (judge==0)
{
break;
}
else continue;
}
if (num==2||num==3)
{
judge=1;
}
return judge;
}

int main()
{
int a=clock();
int i,c;
for (i=2;i<1000;i++)
{
if (judge(i)==0)
{
continue;
}
else
{
printf("%-4d",i);
}
}
int b=clock();
printf ("\nRunning time:%d ms",c=b-a);
return 0;
}
42 changes: 42 additions & 0 deletions level1/p06_Goldbach/Goldbach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <math.h>

int judge(int num)
{
int i,judge=0;
for (i=2;i<(sqrt(num)+1);i++)
{
judge=num%i;
if (judge==0)
{
break;
}
else continue;
}
if (num==2||num==3)
{
judge=1;
}
return judge;
}

int main()
{
int n,i,j;
printf("Please enter the range:\n");
scanf("%d",&j);
for (n=4;n<=j;n+=2)
{
for (i=2;i<n;i++)
{
if (((judge(i)!=0)) && ((judge(n-i))!=0))
{
printf("%d = %d+%d \n",n,i,(n-i));
break;
}
else continue;
}
}
return 0;
}

39 changes: 39 additions & 0 deletions level1/p07_encrypt_decrypt/encrypt-decrypt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include<stdio.h>
#define K 2

void encrypt(char *p)
{
for (int i=0;i<100;i++,p++)
{
if ((*p)=='\0')
{break;}
(*p)+=K;
printf("%c",*p);
}
}

void decrypt(char *p)
{
for (int i=0;i<100;i++,p++)
{
if ((*p)=='\0')
{break;}
(*p)-=K;
printf("%c",*p);
}
}

int main()
{
int n;
void (*p1)(char *);
char ch[100];
printf("Please choose 1 or 2(1--encrypt, 2--decrypt) :\n");
scanf("%d",&n);
printf("Please enter your text:\n");
scanf("%s",ch);
(n==1) ? p1=encrypt :p1=decrypt;
printf("Result:\n");
(*p1)(ch);
return 0;
}
30 changes: 30 additions & 0 deletions level1/p08_hanoi/hanio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>

void move(char x,char y)
{
printf("%c-->%c\n",x,y);
}

void hanoi(int n,char one,char two,char three)
{
if (n == 1)
{
move(one,three);
}
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}

int main()
{
int n;
printf("Please enter the number of disks:\n");
scanf("%d",&n);
printf("The steps are as follows:\n");
hanoi(n,'A','B','C');

}
Loading