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

pr-meltout #45

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
38 changes: 38 additions & 0 deletions level1/p01_runningLetter/runningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <windows.h>
#include <string.h>

#define MAXN 100
#define EDGE 120
#define SPEED 10

void PrintStr(char s[], int x, int y)
{
HANDLE hd;
COORD pos;
pos.X = x;
pos.Y = y;
hd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hd, pos);
printf("%s",s);
}

int main()
{
char a[MAXN]="a";
int x = 1, y = 0, dir = 1, len = strlen(a);
PrintStr(a, 0, 0); Sleep(50);
while (1)
{
system("cls");
if (x == 0 || x == EDGE - len)
{
dir *= -1;
y++;
}
x += dir;
PrintStr(a, x, y);
Sleep(SPEED);
}
return 0;
}
24 changes: 24 additions & 0 deletions level1/p02_isPrime/isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <math.h>

typedef long long ll;

int isPrime(ll x)
{
if(x==1)return 0;
ll sq = sqrt(x);
for (int i = 2; i <= sq; ++i)
{
if (x%i == 0)return 0;
}
return 1;
}

int main()
{
ll num;
scanf("%I64d",&num);
if (isPrime(num))printf("a prime\n");
else printf("not a prime\n");
return 0;
}
15 changes: 15 additions & 0 deletions level1/p03_Diophantus/Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>
#include <math.h>

int main()
{
for (int age = 0; age <= 200; ++age)
{
if (age % 12 == 0 && age % 7 == 0 && age - age / 6 - age / 12 - age / 7 - 5 - 4 == age / 2)
{
printf("age = %d\n", age - 4);
break;
}
}
return 0;
}
16 changes: 16 additions & 0 deletions level1/p04_ narcissus/narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <math.h>

int main()
{
int x, y, z;
for (int i = 100; i < 1000; ++i)
{
x = i / 100;
y = (i - i / 100 * 100) / 10;
z = i % 10;
if (pow(x, 3) + pow(y, 3) + pow(z, 3) == i)
printf("%d\n", i);
}
return 0;
}
34 changes: 34 additions & 0 deletions level1/p05_allPrimes/allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <time.h>
#define MAXN 1000

int notprime[MAXN + 5], primes[MAXN + 5], len;

void GetPrime()
{
for (int i = 2; i <= MAXN; i++)
{
if (!notprime[i])
{
primes[len++] = i;
printf("%d\n", i);
}
for (int j = 0; j < len; j++)
{
if (i*primes[j] > MAXN) break;
notprime[i*primes[j]] = 1;
if (i%primes[j] == 0) break;
}
}
}

int main()
{
clock_t StartTime, FinishTime;
StartTime = clock();
GetPrime();
FinishTime = clock();
printf("%.3lf seconds", (double)(FinishTime - StartTime) / CLOCKS_PER_SEC);
//system("pause");
return 0;
}
46 changes: 46 additions & 0 deletions level1/p06_Goldbach/Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <time.h>
#include <string.h>

#define MAXN 100

int isprime[MAXN + 5], primes[MAXN + 5], len;

void GetPrime()
{
memset(isprime, 1, sizeof(isprime));
isprime[0] = isprime[1] = 0;
for (int i = 2; i <= MAXN; i++)
{
if (isprime[i])primes[len++] = i;
for (int j = 0; j < len; j++)
{
if (i*primes[j] > MAXN) break;
isprime[i*primes[j]] = 0;
if (i%primes[j] == 0) break;
}
}
}

int main()
{
GetPrime();
int flag;
for (int i = 4; i <= 100; i += 2)
{
flag = 0;
for (int j = 0; j < len; ++j)
{
if (isprime[i - primes[j]])
flag = 1;
}
if (!flag)
{
printf("false\n");
return 0;
}
}
printf("true\n");
//system("pause");
return 0;
}
130 changes: 130 additions & 0 deletions level1/p07_encrypt_decrypt/encrypt_decrypt(rsa&xor).c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MINN 1000
#define MAXN 10000

typedef long long ll;

ll n, p, q, e, d, x, y, phi, iv, cipher[MAXN];
ll len, MesLen, primes[MAXN + 5], isprime[MAXN + 5];
char Message[MAXN], DecMes[MAXN];

int GetRand(int LowerBound, int UpperBound)
{
return rand() % (UpperBound - LowerBound + 1) + LowerBound;
}

void GetPrime()
{
memset(isprime, 1, sizeof(isprime));
isprime[0] = isprime[1] = 0;
for (int i = 2; i <= MAXN; i++)
{
if (isprime[i])primes[len++] = i;
for (int j = 0; j < len; j++)
{
if (i*primes[j] > MAXN) break;
isprime[i*primes[j]] = 0;
if (i%primes[j] == 0) break;
}
}
}

ll gcd(ll a, ll b)
{
if (b == 0)return a;
return gcd(b, a%b);
}

ll pow_mod(ll a, ll b, ll mod)
{
ll ans = 1;
while (b)
{
if (b & 1)ans = ans * a%mod;
a = a * a%mod; b >>= 1;
}
return ans;
}

ll exgcd(ll a, ll b)
{
ll d = a, tmp;
if (b != 0)
{
tmp = x; x = y; y = tmp;
d = exgcd(b, a % b);
tmp = x; x = y; y = tmp;
y -= (a / b) * x;
}
else
{
x = 1; y = 0;
}
return d;
}

ll mod_inverse(ll a, ll mod)
{
exgcd(a, mod);
return (mod + x % mod) % mod;
}

void init()
{
srand(time(NULL));
iv = GetRand(0, 255);
GetPrime();
p = primes[GetRand(1000, len - 1)];
q = primes[GetRand(1000, len - 1)];
n = p * q;
phi = (p - 1)*(q - 1);
e = GetRand(10, phi - 1);
while (gcd(e, phi) != 1)
e = GetRand(10, phi - 1);
d = mod_inverse(e, phi);
}

ll RSA_enc(ll x)
{
x = pow_mod(x, e, n);
return x;
}

ll RSA_dec(ll x)
{
return (char)pow_mod(x, d, n);
}

void get_enc()
{
cipher[0] = iv;
for (int i = 1; i <= MesLen; ++i)
cipher[i] = RSA_enc(cipher[i - 1] ^ (ll)Message[i - 1]);
}

void get_dec()
{
cipher[0] = iv;
for (int i = 0; i < MesLen; ++i)
DecMes[i] = (char)RSA_dec(cipher[i + 1]) ^ cipher[i];
}

int main()
{
init();
scanf("%s", Message);
MesLen = strlen(Message);
get_enc();
printf("cipher is : ");
for (int i = 1; i <= MesLen; ++i)
printf("%010I64d", cipher[i]);
printf("\n");
get_dec();
printf("Decrypted Message is : %s", DecMes);
return 0;
}
66 changes: 66 additions & 0 deletions level1/p07_encrypt_decrypt/encrypt_decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAXN 1000005

int XorArray[MAXN], n;
char Message[MAXN], Cipher[MAXN * 2], DecMess[MAXN];

char ToHex(int _oct)
{
if (_oct < 10)return _oct + '0';
else return _oct - 10 + 'a';
}

int ToOct(int _hex)
{
if (_hex >= '0' && _hex <= '9')return _hex - '0';
else return _hex - 'a' + 10;
}

int GetRand(int LowerBound, int UpperBound)
{
return rand() % (UpperBound - LowerBound + 1) + LowerBound;
}

void GenerateXorArray()
{
n = GetRand(10, 100);
for (int i = 0; i < n; ++i)
XorArray[i] = GetRand(0, 255);
}

void Encrypt()
{
int num, len = strlen(Message);
for (int i = 0; i < len; ++i)
{
num = (int)Message[i] ^ XorArray[i%n];
Cipher[i * 2] = ToHex(num / 16);
Cipher[i * 2 + 1] = ToHex(num % 16);
}
}

void Decrypt()
{
int num, len = strlen(Cipher);
for (int i = 0; i < len; i += 2)
{
num = ToOct(Cipher[i]) * 16 + ToOct(Cipher[i + 1]);
DecMess[i / 2] = (char)(num^XorArray[(i / 2) % n]);
}
}

int main()
{
srand(time(NULL));
GenerateXorArray();
scanf("%s", Message);
Encrypt();
printf("Cipher = %s\n", Cipher);
Decrypt();
printf("Decrypted Message = %s\n", DecMess);
return 0;
}
Loading