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

完成level1作业 && 🤗完成飞机游戏🤗 🛩💨🛸💥 2020080601010 #30

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

#include <iostream>

using namespace std;

void runningLetter() {
//输入字符或者字母
string s;
cin >> s;

int x = 0, len = s.size(); // x为坐标

//光标位置
HANDLE hout;
COORD pos;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
pos.X = 0, pos.Y = 0; //设置Y坐标(不变)

//控制台窗口信息
CONSOLE_SCREEN_BUFFER_INFO scr;

//用于判断是否碰到右侧边界
bool judge = false;

DWORD result;

while (true) {
GetConsoleScreenBufferInfo(hout, &scr); //每次刷新获取窗口大小
if (x + len >= scr.dwSize.X)
judge = true;
else if (x <= 0)
judge = false;

pos.X = x;

SetConsoleCursorPosition(hout, pos); //设置光标位置
cout << s;

if (judge == false)
++x;
else if (judge == true)
--x;

Sleep(50); //设置刷新速度
system("cls");
}
CloseHandle(hout);
}

int main() {
runningLetter();
return 0;
}
15 changes: 15 additions & 0 deletions level1/p02_isPrime/isPrime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <cmath>
#include <iostream>

using namespace std;

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

int main() {
cout << isPrime(49999);
return 0;
}
20 changes: 20 additions & 0 deletions level1/p03_Diophantus/Diophantus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>

using namespace std;

double Diophantus() {
double aDad, aSon = 35, temp;

do {
++aSon;
aDad = 2 * aSon;
temp = aDad * 33 / 84 + 5 + 4 + aSon;
} while (temp != aDad);

return aDad - 4;
}

int main() {
cout << Diophantus();
return 0;
}
20 changes: 20 additions & 0 deletions level1/p04_ narcissus/narcissus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>

using namespace std;

void narcissus() {
int x, y, z, n = 100;
while (n < 1000) {
z = n % 10;
y = n / 10 % 10;
x = n / 100;
if (x * x * x + y * y * y + z * z * z == n) cout << n << ' ';
++n;
}
cout << endl;
}

int main() {
narcissus();
return 0;
}
31 changes: 31 additions & 0 deletions level1/p05_allPrimes/allPrimes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <ctime>
#include <iostream>

using namespace std;

void allPrimes() {
clock_t startTime = clock();

int list[170];
int i, n, cnt = 0;

for (n = 2; n < 1001; ++n) {
for (i = 0; i < cnt; ++i)
if (n % list[i] == 0) goto end;

list[cnt++] = n;
cout << n << ' ';

end:
continue;
}

clock_t endTime = clock();

cout << "\n计算时间: " << endTime - startTime << endl;
}

int main() {
allPrimes();
return 0;
}
33 changes: 33 additions & 0 deletions level1/p06_Goldbach/Goldbach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>

using namespace std;

bool Goldbach() {
int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
bool meet = false;

for (int n = 4; n <= 100; n += 2) {
meet = false;
for (int i = 0; i < 15; ++i)
for (int j = i; j < 25; ++j) {
if (prime[i] + prime[j] == n) {
meet = true;
goto judge;
} else if (prime[i] + prime[j] > n)
break;
}

judge:
if (meet == true)
continue;
else
return false;
}

return true;
}

int main() {
cout << Goldbach();
return 0;
}
86 changes: 86 additions & 0 deletions level1/p07_encrypt_decrypt/encrypt_decrypt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <ctime>
#include <iostream>

using namespace std;

using ull = unsigned long long;

const int MAX_N = 700; //不能太大

ull k, d, e, n, fn;

inline ull gcd(ull a, ull b) { return b == 0 ? a : gcd(b, a % b); } //求最大公约数

ull powMod(ull base, ull pow, ull mod) { //幂模运算
ull res = 1;

while (pow) {
if (pow & 1) res = (res * base) % mod;
pow >>= 1;
base = (base * base) % mod;
}
return res;
}

void allPrimes(int *list, int &cnt) { //寻找质数
int i, n;

for (n = 2; n < MAX_N; ++n) {
for (i = 0; i < cnt; ++i)
if (n % list[i] == 0) goto end;
list[cnt++] = n;
end:
continue;
}
}

void RSA_Initialize() { //简易RSA算法加密初始化
int Primes[MAX_N / 5];
int cnt = 0;
allPrimes(Primes, cnt);
cnt /= 2;
srand((unsigned)time(NULL));
int p = Primes[rand() % cnt + cnt], q = Primes[rand() % cnt + cnt];
n = p * q, fn = (p - 1) * (q - 1);

e = 7, k = 7;
while (gcd(e, fn) != 1) e += 777;
d = k * fn + 1;

cout << "公开的: n = " << n << " e = " << e << endl; //公开n, e
cout << "不公开的: d = " << d << endl;
}

void encrypt(wstring ws, int e, int n) { //加密
for (int i = 0; i < ws.size(); ++i) {
wcout << powMod(ws[i], e, n) << ' '; //输出密文
ws[i] = powMod(ws[i], e, n);
}
wcout << endl;
}

void decrypt(wstring ws, int d, int n) { //解密
for (int i = 0; i < ws.size(); ++i) {
wcout << powMod(ws[i], d, n) << ' '; //输出明文
ws[i] = powMod(ws[i], d, n);
}
wcout << endl;
}

int main() {
wstring wstr = L"bilibili.com/video/BV1FX4y1g7u8";

for (int i = 0; i < wstr.size(); ++i) wcout << wstr[i];
cout << endl;

// RSA
RSA_Initialize();
encrypt(wstr, e, n);
cout << endl;
decrypt(wstr, d, n);

for (int i = 0; i < wstr.size(); ++i) wcout << (wchar_t)wstr[i];
cout << endl;

return 0;
}
17 changes: 17 additions & 0 deletions level1/p08_hanoi/hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>

using namespace std;

void hanoi(int n, char A, char B, char C) { //将A塔上n块圆盘通过B塔搬运至C塔上
// A塔为被搬运塔,B塔为中间塔,C塔为目标塔
if (n > 0) {
hanoi(n - 1, A, C, B); //把n - 1块圆盘从A塔搬运至B塔
cout << "将顶层圆盘从塔" << A << "移至塔" << C << endl;
hanoi(n - 1, B, A, C); //把剩下n - 1块圆盘从B塔搬运至C塔
}
}

int main() {
hanoi(64, 'A', 'B', 'C');
return 0;
}
Loading