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 1 commit
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
38 changes: 38 additions & 0 deletions practices/c/level1/p01_runningLetter/p01_runningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define N 20

char ch[N];

void runLetter(int n) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前面只写函数原型,函数的实现放到后面去

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进对齐

}

int checkRight(int t) {
return t <= 80 - strlen(ch);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

80应该定义一个常量

}

int checkLeft(int t) {
return t >= 0;
}

int main() {
printf("Input the word or letter:\n");
gets(ch);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进对齐

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

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;
}

int main () {
int n;
scanf("%d",&n);
if (isPrime(n)) printf("Yes!");
else printf("No!");
return 0;
}
15 changes: 15 additions & 0 deletions practices/c/level1/p03_Diophantus/p03_Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>

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;
}

int main() {
for (int i = 1; i <= 1000; i++) {
if (check(i)) printf("%d ",i - 4);
}
return 0;
}
21 changes: 21 additions & 0 deletions practices/c/level1/p04_ narcissus/p04_narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>

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;
}

int main() {
for (int i = 100; i < 1000; i++) {
if (i == narcissusChange(i)) printf("%d\t",i);
}
return 0;
}
24 changes: 24 additions & 0 deletions practices/c/level1/p05_allPrimes/p05_allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <string.h>
#define N 1001

int isPrime[N];

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;
}
}
}
}

int main() {
init();
for (int i = 2; i < N; i++) {
if (isPrime[i]) printf("%d\t",i);
}
return 0;
}
64 changes: 64 additions & 0 deletions practices/c/level1/p06_Goldbach/p06_Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>
#include <math.h>
#define N 101

int primes[20];
int numberOfPrimes;
struct data {
int a,b,c;
}a[N];

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;
}
}
}

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;
}