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

Homework #3

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
33 changes: 33 additions & 0 deletions level1/p01_runningLetter/runningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>


void main() {
int n = 0;
const int right_border = 105, left_border = 0;
Crazycat-tj marked this conversation as resolved.
Show resolved Hide resolved
char value = 1;//value为1时向右移动,否则向左移动
while (1) {
if (value) {
for (int i = 0; i < n; i++) {
printf(" ");
}
printf("Running Word");
n++;
system("cls");//清屏
if (n == right_border) {
value = 0;
}
}
else {
for (int i = 0; i < n; i++) {
printf(" ");
}
printf("Running Word");
n--;
system("cls");
if (n == left_border) {
value = 1;
}
}
}
}
23 changes: 23 additions & 0 deletions level1/p02_isPrime/isprime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <math.h>

int is_prime(int n){
int i;
for(i=2;i<=sqrt(n);i++){
if(n%i==0){
return 0;
}
}
return 1;
}

int main(){
int n;
printf("Please enter a number: ");
scanf("%d",&n);
if(is_prime(n)&&n>1){
printf("It is a prime number.");
}else{
printf("It isn't a prime number.'");
}
}
12 changes: 12 additions & 0 deletions level1/p03_Diophantus/Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
//����ͼ����
Crazycat-tj marked this conversation as resolved.
Show resolved Hide resolved
int main(){
float x;
for(x=1;x<200;x++){
if((x/6+x/12+x/7+5+x/2+4)==x){
printf("%.0f",x);
break;
}
}
return 0;
}
14 changes: 14 additions & 0 deletions level1/p04_ narcissus/narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

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

//��������
int is_prime1(){
int i,n,m,t = 0;
for(n = 2;n < 1000;n ++){
m = 1;
for(i = 2;i <= sqrt(n);i ++){
if(n % i == 0){
m = 0;
break;
}
}
if(m == 1){
printf("%d\t",n);
t ++;
}
if(t == 15){
printf("\n");
t = 0;
}
}
return 0;
}

//����ɸ
int is_prime2(){
char a[1000] = {1};
int i,x,m = 0;
for(i = 2;i < 1000;i ++){
if(a[i] == 0){
for(x = i*i;x < 1000;x = x + i)a[x] = 1;
printf("%d\t",i);
m ++;
if(m == 15){
printf("\n");
m = 0;
}
}
}
}

int main(){
int n,m = 0;
clock_t start_t,end_t;
double total_t;

//��������
start_t = clock();
is_prime1();
end_t=clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("\n�������е���ʱ��Ϊ: %fs\n",total_t);

//����ɸ
start_t = clock();
is_prime2();
end_t = clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("\n�������е���ʱ��Ϊ: %fs",total_t);
return 0;
}
26 changes: 26 additions & 0 deletions level1/p06_Goldbach/Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool is_prime(int n){
int i;
for(i=2;i<=sqrt(n);i++){
if(n%i==0)return false;
}
return true;
}

int main(){
int n,m,x=0;
for(n = 4;n <= 100;n += 2){
for(m = 2;m <= n;m ++){
if(is_prime(m) && is_prime(n - m)){
printf("%3d = %2d + %2d\n",n,m,n - m);
x++;
break;
}
}
if(x==49)printf("The Goldbach is true");
}
return 0;
}