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
32 changes: 32 additions & 0 deletions level1/p01_runningLetter/p01_runningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<stdio.h>
#include <windows.h>
#define TIME 24
#define WIDE 80
#define TheChar 'A'


void run()
{
int i,j;

for (i=0;i<WIDE*2;i++)
{
if (i<=WIDE)
for (j=0;j<i;j++)
printf(" ");
else
for (j=WIDE;j>i-WIDE;j--)
printf(" ");
printf("%c",TheChar);
Sleep(TIME);
system("cls");
}

}

int main()
{
system("mode con cols=81 lines=50");
run();
return 0;
}
22 changes: 22 additions & 0 deletions level1/p02_isPrime/p02_isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
long long n;
scanf("%lld",&n);

long long i;
int flag=1;
for (i=2;i<=sqrt(n)+1;i++)
{
if (n%i == 0) flag=0;
}
if ( flag )
printf("%lld is a prime.",n);
else
printf("%lld is not a prime.",n);

return 0;
}
25 changes: 25 additions & 0 deletions level1/p03_Diophantus/p03_Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#define INTERVAL 4

int check(int x)
{
int son_die=x-(x/6+x/12+x/7+5)-INTERVAL;

if (son_die == x/2)
{
return 0;
}
return 1;
}

int main()
{
int the_age=1;
while ( check(the_age) )
{
the_age++;
}
printf("The age of diophantus when his son died is %d",the_age-INTERVAL);
return 0;
}
39 changes: 39 additions & 0 deletions level1/p04_ narcissus/p04_ narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#define MIN_N 100
#define MAX_N 1000

int cube(int x)
{
return(x*x*x);
}

int check(int number)
{
int bit,decade,hundred;
bit=number%10;
decade=(number/10)%10;
hundred=number/100;

if (cube(bit)+cube(decade)+cube(hundred) == number)
{
printf("%d=%d^3+%d^3+%d^3\n",number,hundred,decade,bit);
return 1;
}

return 0;
}

int main()
{
int i,num=0;

for (i=MIN_N;i<MAX_N;i++)
{
if ( check(i) )
num++;
}
printf("\nThe total of narcissus number is %d.",num);

return 0;
}
34 changes: 34 additions & 0 deletions level1/p05_allPrimes/p05_allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MIN_N 2
#define MAX_N 1000

int main()
{
int time_start,time_end,a[MAX_N+5]={0};

time_start=clock();

int i,j,num=0;
for (i=MIN_N;i<=MAX_N/2+1;i++)
{
if ( !a[i] )
for (j=2;j<=MAX_N/i;j++)
if (i*j<=MAX_N) a[i*j]=1;

}
for (int i=MIN_N;i<=MAX_N;i++)
if ( !a[i] )
{
printf("%d ",i);
num++;
if (num%10 == 0) printf("\n");
}
printf("\n\nThe total number of prime is %d.",num);

time_end=clock();

printf("\ntime use:%lf ms",(double)(time_end-time_start));
return 0;
}
29 changes: 29 additions & 0 deletions level1/p06_Goldbach/p06_Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MIN_N 1
#define MAX_N 50

int prime(int x)
{
int i;
for (i=MIN_N+1;i<=sqrt(x)+1;i++)
if (x%i == 0) return 0;
return 1;
}

int main()
{
int i,j;
for (i=MIN_N;i<=MAX_N;i++)
{
for (j=MIN_N;j<=i*2;j++)
if (prime(j) && prime(2*i-j))
{
printf("%d = %d + %d\n",2*i,j,2*i-j);
break;
}
}
printf("All even numbers within 100 accord with Goldbach's conjecture.");
return 0;
}
39 changes: 39 additions & 0 deletions level1/p07_encrypt_decrypt/p07_encrypt_decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 1000005

char Original_string[MAXN];
char After_handling[MAXN];

char encrypt(char c,int pos)
{
return(c+(pos+1)*(pos+2));
}

char decrypt(char c,int pos)
{
return(c-(pos+1)*(pos+2));
}



int main()
{
scanf("%s",Original_string);
int len=strlen(Original_string);

printf("After encrypt:");
int i;
for (i=0;i<len;i++)
{
printf("%c",encrypt(Original_string[i],i));
}

printf("\nAfter decrypt:");
for (i=0;i<len;i++)
{
printf("%c",decrypt(Original_string[i],i));
}
return 0;
}
36 changes: 36 additions & 0 deletions level1/p08_hanoi/p08_hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>

int tot=0;

void move(char a,char c)
{
tot++;
printf("Step %d: move from %c to %c\n",tot,a,c);
}

void hanoi(char a,char b,char c,int n)
{
if (n == 1)
{
move(a,c);
}
else
{
hanoi(a,c,b,n-1);
move(a,c);
hanoi(b,a,c,n-1);
}
}

int main()
{
int the_number;
printf("Please enter the size of hanoi:");
scanf("%d",&the_number);
printf("\nThe solution is:\n");

hanoi('A','B','C',the_number);

return 0;
}
Loading