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


void main() {
int n = 0;
const int RIGHT_BORDER = 105, LEFT_BORDER = 0;
char 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>
//丢番图问题
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;
}
99 changes: 99 additions & 0 deletions level1/p07_encrypt_decrypt/encrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*���Կ������������*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//����
void encode(char str[], int n) {
int i;
char c;
for (i = 0; i < strlen(str); ++i) {
c = str[i];
if (str[i] >= 'A' && str[i] <= 'Z') {
if (c + n % 26 <= 'Z') {
str[i] = (char)(c + n % 26);
}
else {
str[i] = (char)(c + n % 26 - 26);
}
}
else if (str[i] >= 'a' && str[i] <= 'z') {
if (c + n % 26 <= 'z') {
str[i] = (char)c + n % 26;
}
else {
str[i] = (char)c + n % 26 - 26;
}
}
else {
str[i] = c;//������ĸ��������
}
}
printf("\nAfter encode: \n");
puts(str);
}

//����
void decode(char str[], int n) {
int i;
char c;
for (i = 0; i <= strlen(str); ++i) {
c = str[i];
if (str[i] <= 'z' && str[i] >= 'a') {
if (c - n % 26 > 'a') {
str[i] = (char)(c - n % 26);
}
else {
str[i] = (char)(c - n % 26 + 26);
}
}
else if (str[i] <= 'Z' && str[i] >= 'A') {
if (c - n % 26 > 'A') {
str[i] = (char)(c - n % 26);
}
else {
str[i] = (char)(c - n % 26 + 26);
}
}
else {
str[i] = c;
}
}
printf("\nAfter decode: \n");
puts(str);
}


int main() {
char str[50];
int k, q = 1;
int n,c;
while (q) {
system("cls");
printf("Please enter your word:\n");
scanf_s("%s", str,50);
printf("Please set the encode function: \n");
scanf_s("%d", &n);
printf("-----------------\n");
printf("1: Encryption\n");
printf("2: Decryption\n");
printf("-----------------\n");
printf("\nPlease choose: ");
scanf_s("%d", &k);
if (k == 1) {
encode(str, n);
}
else if (k == 2) {
decode(str, n);
}

printf("Do you want to quit (1/0): \n");
scanf_s("%d", &c);
if (c == 1) {
q = 0;
}
}
return 0;

}

28 changes: 28 additions & 0 deletions level1/p08_hanoi/haoni.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
int m = 0;
void move(int disk, char A, char C) {
m++;
printf("�� %d ���ƶ��� �� %d ��Բ�̴� %c �ƶ��� %c\n", m, disk, A, C);
}
void hanoi(int n, char A, char B, char C) {
if (n == 1) {
move(n, A, C);
}
else {
hanoi(n - 1, A, C, B);
move(n, A, C);
hanoi(n - 1, B, A, C);
}
}

int main() {
char A = 'A';
char B = 'B';
char C = 'C';
int disks;
printf("������Բ�̵ĸ���\n");
scanf("%d", &disks);
hanoi(disks, A, B, C);
return 0;
}

Loading