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

lvl1_1_to_8 #29

Open
wants to merge 10 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
46 changes: 46 additions & 0 deletions practices/c/level1/p01_runningLetter/lvl1_1_Running_letter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* code: Running Letter
* author: Kyrios0
* date: 2017.02.26
* state: finished
* version: 1.0.0
*/

#include<stdio.h>
#include<windows.h>

void gotoxy(int x, int y)
{
HANDLE hOutput;
COORD coo;
coo.X = x;
coo.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, coo);
}

int main(int argc, char** argv)
{
system("chcp 437");
system("color 0f");
system("mode con cols=63 lines=32");

int x = 0, y = 0, round = 0;

while(1)
{
system("cls");

gotoxy(x, y);
putchar('#');
gotoxy(x, y);

(round % 2 == 0) ? x++ : x--;
if((x == 0) || (x == 63))
{
round++;
}

Sleep(25);
}
return 0;
}
58 changes: 58 additions & 0 deletions practices/c/level1/p02_isPrime/lvl1_2_isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// isPrime.c
//


/* code: Step0 for Eula Prime (change from lvl1_5)
* author: Kyrios0
* date: 2017.02.21
* state: finished
* version: 2.0.2
*/

#include<stdio.h>
#include<stdlib.h>//malloc()
#include<string.h>
#include<Windows.h>



int main(int argc, char** argv)
{
long max;
puts("Prime Number Detection System");
puts("Please enter the number you want to detect(It should be no less than 2)");
scanf("%ld", &max);

long ct = 0;
int *judge;
judge = (int*)malloc((max + 1) * sizeof(int));
long *prime;
prime = (long*)malloc((max + 1) * sizeof(long));
memset(prime, 0, (max + 1) * sizeof(prime));
memset(judge, 0, (max + 1) * sizeof(judge));
judge[0] = judge[1] = 1;
for (long i = 2; i < max; i++)
{
if (!judge[i])
{
prime[ct] = i;
ct++;
}

for (int j = 0; j < ct && i * prime[j] <= max; j++)
{
judge[i * prime[j]] = 1;
if (!(i%prime[j]))
{
break;
}

}
}

printf((!judge[max]) ? ("%d is a prime number\n") : ("%d is a composite number\n"), max);

system("pause");

return 0;
}
4 changes: 3 additions & 1 deletion practices/c/level1/p03_Diophantus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@

年级是他的一半。

问儿子死时丢番图多大?
问儿子死时丢番图多大?

/* *just for test* */
27 changes: 27 additions & 0 deletions practices/c/level1/p03_Diophantus/lvl1_3_Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* author: Kyrios0
* date: 2017.02.23
* version: 1.0.0
* state: finished
*/

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

int main(int argc, char *argv[]) {
system("chcp 437");
system("cls");
double age = 0.0, ans = 0.0;
while(1)
{
ans = (1.0/6.0 + 1.0/12.0 + 1.0/7.0 + 1.0/2.0) * age + 9.0;
if(ans - age < 0.1)
{
printf("He was %.0f years old at that time. \n", ans - 4);
system("pause");
return 0;
}
age += 1.0;
}
return 0;
}
2 changes: 1 addition & 1 deletion practices/c/level1/p04_ narcissus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

水仙花数:n位数的每个数位的n次方之和等于该n位数本身

例如:153=1^3+5^3+3^3
例如:153=1^3 + 5^3 + 3^3
36 changes: 36 additions & 0 deletions practices/c/level1/p04_ narcissus/lvl1_4_narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* code: Narcissus
* author: Kyrios0
* date: 2017.02.26
* state: finished
* version: 1.0.0
*/

#include "stdafx.h"
#include<stdio.h>
#include<windows.h>
#include<math.h>

int main(int argc, char** argv)
{
system("chcp 437");
system("cls");
int hun, ten, one, sum = 100;//hun = 100
puts("The following is the number of narcissus in 1000");

while (sum < 1000)
{
one = sum % 10;
ten = sum / 10 % 10;
hun = sum / 100;

if ((int)(pow(double(one), 3.0) + pow(double(ten), 3.0) + pow(double(hun), 3.0)) == sum)
{
printf("%d\n", sum);
}

sum++;
}

system("pause");
return 0;
}
68 changes: 68 additions & 0 deletions practices/c/level1/p05_allPrimes/lvl1_5_allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// allPrime.c
//

/* code: Step0 for Eula
* author: Kyrios0
* date: 2017.02.21
* state: finished
* version: 2.0.2
*/
#define MAX 200000000
#include<stdio.h>
#include<stdlib.h>//malloc()
#include<string.h>
#include<Windows.h>

int judge[MAX];
long prime[MAX];



int main(int argc, char** argv)
{
long ct = 0;
judge[0] = judge[1] = 1;

for (long i = 2; i < MAX; i++)
{
if (!judge[i])
{
prime[ct] = i;
ct++;
}

for (int j = 0; j < ct && i * prime[j] < MAX; j++)
{
judge[i * prime[j]] = 1;
if (!(i%prime[j]))
{
break;
}

}
}

FILE *fpt;
fpt = fopen("PrimeList.txt", "w");

puts("Start writing... Please wait a moment.");

for (long i = 0; ; i++)//output
{
if (!prime[i])
{
break;
}
fprintf(fpt, "%d, ", prime[i]);
if ((i % 100000 == 0) && (i != 0))
{
printf("%d numbers has been writen: %d \n", i, prime[i]);
}
}

puts("DONE!");

system("pause");

return 0;
}
83 changes: 83 additions & 0 deletions practices/c/level1/p06_Goldbach/lvl1_6_Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Goldbach.c
//

/* code: Goldbach (change from lvl1_5)
* author: Kyrios0
* date: 2017.02.26
* state: finished
* version: 2.0.1
*/

#include<stdio.h>
#include<string.h>
#include<Windows.h>
int prime[1000000];
int judge[80000];

int main(int argc, char** argv)
{
long MAX = 1000000;//about 8min
long ct = 0;
judge[0] = judge[1] = 1;

for (long i = 2; i < MAX; i++)
{
if (!judge[i])
{
prime[ct] = i;
ct++;
}

for (int j = 0; j < ct && i * prime[j] < MAX; j++)
{
judge[i * prime[j]] = 1;
if (!(i%prime[j]))
{
break;
}

}

}

FILE *fpt;
fpt = fopen("Goldbach.txt", "w");
puts("Start writing... Please wait a moment.");

int x = 0, y = 0;

for (int i = 4; i <= 1000000;)
{
if (prime[x] < i - 300)//The speed can be increased by 40% if i == 100,000
{
x++;
}

if (i - prime[x] != prime[y])
{
x++;
if (prime[x] > 1000000)
{
x = 0;
y++;
}
}
else
{
fprintf(fpt, "%d = %d + %d\n", i, prime[x], prime[y]);
if (i % 20000 == 0)
{
printf("%d lines has been writen.\n", i / 2);
}
x = 0; y = 0, i += 2;
continue;
}

}

puts("DONE!");

system("pause");

return 0;
}
13 changes: 12 additions & 1 deletion practices/c/level1/p07_encrypt_decrypt/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
### 功能要求:

1. 分别编写“加密”、“解密”函数,输入为任意长度的字符串
<<<<<<< HEAD
1. 分别编写“加密”、“解密”函数,输入为任意长度的字符串

### Writeup

> 想起寒假学密码学看到的Feistel结构加解密于是产生了用C语言实现的想法。 然后我大概不会再用C语言写加解密了...//IDE:VS2017 RC
>
> P.S.:程序执行加密的时候如果明文过短可能会出BUG,具体原理不详,断在了system语句.
> P.P.S.:附上的文件中,一份源代码,一份程序
=======
1. 分别编写“加密”、“解密”函数,输入为任意长度的字符串
>>>>>>> parent of ba96cb9... lvl1_7
Loading