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

樊捷明 cpp1~9 + 打飞机游戏 #32

Open
wants to merge 11 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
slides/.idea
*.DS_Store
43 changes: 43 additions & 0 deletions practices/c/level1/p01_runningLetter/p01_runningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define N 20

void runLetter(int n);
int checkRight(int t);
int checkLeft(int t);

char ch[N];

int main() {
printf("Input the word or letter:\n");
gets(ch);
while (1) {
int t = 0;
while (checkRight(t)) {
runLetter(t++);
}
while (checkLeft(--t)) {
runLetter(t);
}
}
return 0;
}

void runLetter(int n) {
system("clear");
for (int i = 0; i < n; i++) {
printf(" ");
}
puts(ch);
usleep(50000);
}

int checkRight(int t) {
return t <= 80 - strlen(ch);
}

int checkLeft(int t) {
return t >= 0;
}
20 changes: 20 additions & 0 deletions practices/c/level1/p02_isPrime/p02_isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>
#include <math.h>

int isPrime(int n);

int main () {
int n;
scanf("%d",&n);
if (isPrime(n)) printf("Yes!");
else printf("No!");
return 0;
}

int isPrime(int n) {
if (n == 1) return 0;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return 0;
}
return 1;
}
17 changes: 17 additions & 0 deletions practices/c/level1/p03_Diophantus/p03_Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>

int check(int n);

int main() {
for (int i = 1; i <= 1000; i++) {
if (check(i)) printf("%d ",i - 4);
}
return 0;
}

int check(int n) {
if (n%6 != 0 || n%12 != 0 || n%7 !=0) return 0;
int x = n/6 + n/12 + n/7 + 5;
int ageSon = n - x - 4;
return n == ageSon * 2;
}
24 changes: 24 additions & 0 deletions practices/c/level1/p04_ narcissus/p04_narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>

int Power(int x);
int narcissusChange(int n);

int main() {
for (int i = 100; i < 1000; i++) {
if (i == narcissusChange(i)) printf("%d\t",i);
}
return 0;
}

int Power(int x) {
return x*x*x;
}

int narcissusChange(int n) {
int ans = 0;
while (n) {
ans += Power(n%10);
n = n/10;
}
return ans;
}
26 changes: 26 additions & 0 deletions practices/c/level1/p05_allPrimes/p05_allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <string.h>
#define N 1001

void init();

int isPrime[N];

int main() {
init();
for (int i = 2; i < N; i++) {
if (isPrime[i]) printf("%d\t",i);
}
return 0;
}

void init() {
memset(isPrime,1,sizeof(isPrime));
for (int i = 2; i < N; i++) {
if (isPrime[i]) {
for (int j = 2; i*j < N; j++) {
isPrime[i*j] = 0;
}
}
}
}
67 changes: 67 additions & 0 deletions practices/c/level1/p06_Goldbach/p06_Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <stdio.h>
#include <math.h>
#define N 101

int isPrime(int x);
void init();

struct Data {
int a,b,c;
}a[N];

int primes[20];
int numberOfPrimes;

int main() {
init();
for (int i = 0; i < numberOfPrimes; i++) {
for (int j = i; j < numberOfPrimes; j++) {
int k = primes[i] + primes[j];
if (k > 100) {
break;
}
a[k].a = primes[i];
a[k].b = primes[j];
}
}

for (int i = 0; i < numberOfPrimes; i++) {
for (int j = i; j < numberOfPrimes; j++) {
for (int k = j; k < numberOfPrimes; k++) {
int x = primes[i] + primes[j] + primes[k];
if (x > 100) {
break;
}
a[x].a = primes[i];
a[x].b = primes[j];
a[x].c = primes[k];
}
}
}

printf("不小于6的偶数都是两个素数之和:\n");
for (int i = 6; i <= 100; i+=2) {
printf("%d = %d + %d\n",i,a[i].a,a[i].b);
}
printf("不小于9的偶数都是三个素数之和:\n");
for (int i = 9; i < 100; i+=2) {
printf("%d = %d + %d + %d\n",i,a[i].a,a[i].b,a[i].c);
}
return 0;
}

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

void init() {
numberOfPrimes = 0;
for (int i = 3; i < 100; i++) {
if (isPrime(i)) {
primes[numberOfPrimes++] = i;
}
}
}
108 changes: 108 additions & 0 deletions practices/c/level1/p07_encrypt_decrypt/p07_encrypt_decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//丑陋的加密算法
//只考虑了大小写字母与数字
//加密只能通过改变Hash值改变
//总之这个算法特别蠢就是了

#include <stdio.h>
#include <string.h>
#define Hash 10007
#define N 100

char numToChar(int x);
char enToDe(char c);
char deToEn(char c);
void encrypt();
void decrypt();
void init();

int enIntoDe[62];
int deIntoEn[62];
char ch[N];

int main() {
init();
while (1) {
int k;
printf("输入要进行的操作序号,1:加密,2:解密,3:退出 : ");
scanf("%d",&k);
if (k == 1) {
encrypt();
continue;
}
if (k == 2) {
decrypt();
continue;
}
if (k == 3) {
break;
}
}
return 0;
}

char numToChar(int x) {
if (x < 10) return (char)(48+x);
if (x < 36) return (char)(55+x);
return (char)(61+x);
}

char enToDe(char c) {
if ((int)(c) > 47 && (int)(c) < 58) {
return numToChar(enIntoDe[(int)(c) - 48]);
}
if ((int)(c) > 64 && (int)(c) < 91) {
return numToChar(enIntoDe[(int)(c) - 55]);
}
return numToChar(enIntoDe[(int)(c) - 61]);
}

char deToEn(char c) {
if ((int)(c) > 47 && (int)(c) < 58) {
return numToChar(deIntoEn[(int)(c) - 48]);
}
if ((int)(c) > 64 && (int)(c) < 91) {
return numToChar(deIntoEn[(int)(c) - 55]);
}
return numToChar(deIntoEn[(int)(c) - 61]);
}

void encrypt() {
printf("输入需要加密的字符串:\n");
scanf("%s",&ch);
for (int i = 0; i < strlen(ch); i++) {
ch[i] = enToDe(ch[i]);
}
printf("加密后的字符串为:%s\n",ch);
}

void decrypt() {
printf("输入需要解密的字符串:\n");
scanf("%s",&ch);
for (int i = 0; i < strlen(ch); i++) {
ch[i] = deToEn(ch[i]);
}
printf("解密后的字符串为:%s\n",ch);
}

void init() {
memset(enIntoDe,-1,sizeof(enIntoDe));
memset(deIntoEn,-1,sizeof(deIntoEn));
for (int i = 0; i < 62; i++) {
int x = (i * Hash) % 62;
if (deIntoEn[x] == -1) {
enIntoDe[i] = x;
deIntoEn[x] = i;
}
}
for (int i = 0; i < 62; i++) {
if (enIntoDe[i] == -1) {
for (int j = 0; j < 62; j++) {
if (deIntoEn[j] == -1) {
enIntoDe[i] = j;
deIntoEn[j] = i;
break;
}
}
}
}
}
28 changes: 28 additions & 0 deletions practices/c/level1/p08_hanoi/p08_hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>

void printMove(int x, char A, char B);
void hanoi(int x, char A, char B, char C);

int tot = 0;

int main() {
int n;
scanf("%d",&n);
hanoi(n, 'A', 'B', 'C');
return 0;
}

void printMove(int x, char A, char B) {
printf("%d: %d %c -> %c\n",++tot, x, A, B);
}

void hanoi(int x,char A, char B, char C) {
if (x == 1) {
printMove(x, A, C);
}
else {
hanoi(x - 1, A, C, B);
printMove(x, A, C);
hanoi(x - 1, B, A, C);
}
}
Loading