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

Annie's Homework #56

Open
wants to merge 12 commits into
base: Annie0405
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
31 changes: 31 additions & 0 deletions level1/p03_Diophantus/Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<stdio.h>

int main()
{
int Diophantus_age;
int childhood, youth, bachelor, before_having_son, between_death, son_age;
int numerator, denominator;

//人生中某些阶段占总年龄比例的倒数
childhood = 6;
youth = 12;
bachelor = 7;
son_age = 2;

//人生中某些阶段经历的年数
before_having_son = 5;
between_death = 4;

//解方程
numerator = (before_having_son + between_death) * childhood * youth * bachelor * son_age;
denominator = childhood * youth * bachelor * son_age
- childhood * youth * bachelor
- childhood * youth * son_age
- childhood * bachelor * son_age
- youth * bachelor * son_age;
Diophantus_age = numerator / denominator;

printf("%d\n", Diophantus_age - between_death);

return 0;
}
22 changes: 22 additions & 0 deletions level1/p04_ narcissus/narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<stdio.h>

int main()
{
int a, b, c; //个位数、十位数、百位数
int num = 100; //当前枚举的数

while(num >= 100 && num <= 999)
{
c = num / 100;
b = num / 10 % 10;
a = num % 10;

if(num == a * a * a + b * b * b + c * c * c)
printf("%d ", num);

++num;
}
printf("\n");

return 0;
}
55 changes: 55 additions & 0 deletions level1/p05_allPrimes/all_primes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//打印2~1000以内的所有素数
//打印出总的计算时间
//尽可能地优化算法的效率

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
int n = 2; //当前检索的数
int prime[200] = {0}; //用于存放找到的素数
int i;

clock_t start, finish;
double duration;
//测量一个事件的持续时间

start = clock(); //计时开始

prime[0] = 2;

//若已检索出的任何一个素数都不是当前检索的数的因子
//那么当前检索的数是素数
while(++n <= 1000)
{
for(i = 0; prime[i]; ++i)
{
if(n % prime[i] == 0)
break;
}

if(prime[i] == 0)
prime[i] = n;
}

finish = clock(); //计时结束
duration = (double)(finish - start) / CLOCKS_PER_SEC;

//打印检索到的素数
i = -1;
while(prime[++i])
{
printf("%5d", prime[i]);

//每15个素数输出为一行
if((i + 1) % 15 == 0)
printf("\n");
}

//打印总的计算时间
printf("\n\nTime to calculate all primes is %f seconds.\n", duration);

return 0;
}
68 changes: 68 additions & 0 deletions level1/p06_Goldbach/Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//在100范围内验证哥德巴赫猜想的正确性
//原初猜想的现代陈述:任一大于5的整数都可写成三个质数之和

#include<stdio.h>
#define N 100 //上界
#define M 6 //下界

int main()
{
int i, j1, j2, j3;
int prime[50] = {0}; //用于存放100以内的质数
void Prime(int prime[]); //把100以内的素数存放到prime数组里

Prime(prime);

for(i = M; i <= N; ++i)
{
//如果验证了当前数符合哥德巴赫猜想,就跳到最外层循环的末尾
for(j1 = 0; prime[j1] <= (i - 4); ++j1)
for(j2 = j1; prime[j2] <= (i - 4); ++j2)
for(j3 = j2; prime[j3] <= (i - 4); ++j3)
if(i == prime[j1] + prime[j2] + prime[j3])
goto sf;

//如果没有跳到末尾,则当前数不符合哥德巴赫猜想,输出"Error!",终止程序并返回“1”
printf("Error!\n");
return 1;

sf:
;
}

printf("Goldbach conjecture within 100 is verified.\n");
return 0;
}

void Prime(int prime[])
{
int i, j;
int num; //当前检索的数

prime[0] = 2;
i = 0;
num = 2;

while(++num <= N)
{
for(j = 0; prime[j]; ++j)
{
if(num % prime[j] == 0)
break;
}

if(prime[j] == 0)
prime[j] = num;
}

//这段用来测试,完成后删去
j = -1;
while(prime[++j])
{
printf("%5d ", prime[j]);

if((j + 1) % 10 == 0)
printf("\n");
}
printf("\n\n");
}
156 changes: 156 additions & 0 deletions level1/p07_encrypt_decrypt/decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef unsigned long UL;

extern const int LEN2;

UL n, d;

void decrypt(char cipher[LEN2])
{
UL decrypt_single(UL c);

//获取私钥
FILE *fp = fopen("private_key.txt", "r");
if(fp == NULL) {printf("Error004!\n"); exit(EOF);}
fscanf(fp, "n = %lu", &n);
while(fgetc(fp) != '\n') ;
fscanf(fp, "d = %lu", &d);
while(fgetc(fp) != '\n') ;
fclose(fp);

//test
printf("n = %lu\td = %lu\n", n, d);

//n的长度
int len_n = 0;
UL temp_n = n;
while(temp_n)
{
temp_n = temp_n / 10;
++len_n;
}

//test
printf("len_n = %d\n", len_n);

//密文的长度
const int len_cip = strlen(cipher);

//把密文还原成一段只包含数字的字符串
char num_str[LEN2];
for(int i = 0; i < LEN2; ++i)
{
num_str[i] = '\0';
}
int ns_i = 0; //num_str数组的下标
int temp_i; //把字母还原为数值
for(int i = 0; i < len_cip; ++i)
{
if(cipher[i] >= 'a' && cipher[i] <= 'z')
{
temp_i = cipher[i] - 97;
num_str[ns_i] = temp_i / 10;
num_str[ns_i + 1] = temp_i % 10;
ns_i = ns_i + 2;
}
else if(cipher[i] >= 'A' && cipher[i] <= 'Z')
{
temp_i = cipher[i] - 39;
num_str[ns_i] = temp_i / 10;
num_str[ns_i + 1] = temp_i % 10;
ns_i = ns_i + 2;
}
else
{
num_str[ns_i] = cipher[i] - '0';
num_str[ns_i + 1] = cipher[i + 1] - '0';
ns_i = ns_i + 2;
++i;
}
}

//test
int j_j = 0;
printf("num_str : ");
for(int i = 0; i < 5; ++i)
{
int temp_j = j_j + len_n;
for(; j_j < temp_j; ++j_j)
{
printf("%d", (int)num_str[j_j]);
}
printf("\t");
}
printf("\n");

//明文的长度
int len = ns_i / len_n;

//test
printf("len = %d\n", len);

//把上一步得到的字符串分割成单个的数
UL num[len];
UL temp_num;
UL t10;
for(int i = 0; i < len; ++i)
{
temp_num = 0;
t10 = 1;
for(int j = (i + 1) * len_n - 1; j >= i * len_n; --j)
{
temp_num = temp_num + (int)num_str[j] * t10;
t10 = t10 * 10;
}
num[i] = temp_num;
}

//test
printf("num : ");
for(int i = 0; i < len; ++i)
{
printf("%lu ", num[i]);
}
printf("\n");

//还原字符串
char plain[len + 1];
plain[len] = '\0';
printf("temp_result : ");//test
for(int i = 0; i < len; ++i)
{
//test
UL temp_result = decrypt_single(num[i]);
printf("%lu ", temp_result);

plain[i] = (char)temp_result;
}
printf("\n");//test

//test
printf("The ASCII of plain : ");
for(int i = 0; i < len; ++i)
{
printf("%d ", (int)plain[i]);
}
printf("\n");

//输出
printf("The plain text is: \n");
printf("%s\n\n", plain);
}

UL decrypt_single(UL c)
{
UL result = 1;
for(UL i = 0; i < d; ++i)
{
result = result * c;
result = result % n;
}

return result;
}
Loading