-
Notifications
You must be signed in to change notification settings - Fork 35
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
f0d218a
58dd98a
3e8df18
b4ab458
ed00895
b8d08b9
44a1a04
710953f
e5426ff
ffdb160
883ca23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
slides/.idea | ||
*.DS_Store |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 80应该定义一个常量 |
||
} | ||
|
||
int checkLeft(int t) { | ||
return t >= 0; | ||
} |
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; | ||
} |
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; | ||
} |
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; | ||
} |
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; | ||
} | ||
} | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 此处data是类型名,建议改为:Data There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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; | ||
} | ||
} | ||
} |
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
前面只写函数原型,函数的实现放到后面去