-
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
c practices level1 -- 孔淳漳 #28
base: master
Are you sure you want to change the base?
Changes from 2 commits
5d10f51
eeaa78e
3512fde
db41fb2
937b5b3
0e33e38
6867fb0
c9d6af1
391c7d8
2ab9fb0
1f0fdc9
399f9d8
aa3f502
f0ffa93
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <windows.h> | ||
|
||
int main() | ||
{ | ||
char c; | ||
scanf ("%c", &c); | ||
int i,j; | ||
system("mode con cols=16 lines=9"); | ||
int width = GetSystemMetrics ( SM_CXSCREEN ); | ||
for (i = 0; i < 16; i++) | ||
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. 循环控制变量,尽可能放进循环体内定义 |
||
{ | ||
system ("cls"); | ||
for (j = 0; j < i; j++) | ||
{ | ||
printf (" "); | ||
} | ||
printf ("%c", c); | ||
Sleep (100); | ||
} | ||
for(i = 15; i >= 0; i--) | ||
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. 重复的味道!这两段循环有些相似,请消除 |
||
{ | ||
system ("cls"); | ||
for (j = 0; j < i; j++) | ||
{ | ||
printf (" "); | ||
} | ||
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. 缩进没对齐 |
||
printf ("%c", c); | ||
Sleep (100); | ||
printf ("\n"); | ||
} | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include<stdio.h> | ||
|
||
int main(void) | ||
{ | ||
int i, n; | ||
scanf ("%d", &n); | ||
for (i = 2; i < n; i++) | ||
{ | ||
if(n % i == 0) | ||
break; | ||
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. 没有缩进; |
||
} | ||
if(i == n) | ||
printf ("%d is a prime\n", n); | ||
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. 缩进+大括号 |
||
else | ||
printf ("%d isn't a prime\n", n); | ||
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. 同上 |
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include<stdio.h> | ||
|
||
int main(void) | ||
{ | ||
float i; | ||
for(i = 0; i < 150; i++) | ||
{ | ||
if((i / 6 + i / 12 + i / 7 + 5 + i / 2 + 4) == i) | ||
{ | ||
printf ("When Diophantus's son dead, Diophantus's age is %g", i - 4); | ||
break; | ||
} | ||
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. 缩进对齐 |
||
} | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include<stdio.h> | ||
#include<math.h> | ||
|
||
int main(void) | ||
{ | ||
int i; | ||
for (i=100; i < 1000; i++) | ||
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. 循环变量 |
||
{ | ||
if (pow (i / 100, 3) + pow((i % 100) / 10, 3) + pow(i % 10, 3) == i) | ||
{ | ||
printf("%d\n", i); | ||
} | ||
} | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include<stdio.h> | ||
#include<string.h> | ||
#include<time.h> | ||
#define N 1000001 | ||
|
||
int main() | ||
{ | ||
float start, finish; | ||
start = clock(); | ||
bool prime[N]; | ||
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. bool是c++的语法,在C语言里是不能用的 |
||
memset(prime, true, sizeof(prime)); | ||
prime[0] = prime[1] = false; | ||
for(int i = 2; i < N; i++) | ||
{ | ||
if(prime[i]) | ||
for(int j = i * 2; j < N; j += i) | ||
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. 貌似这个算法不对吧,这样只把各种2的倍数搞掉了啊 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. 缩进+大括号 |
||
{ | ||
prime[j] = false; | ||
} | ||
} | ||
FILE * fp; | ||
//fp = fopen("allprime.txt", "w"); | ||
freopen("allprime.txt", "w", stdout); | ||
for(int k = 0; k <N; k++) | ||
{ | ||
if(prime[k]) | ||
printf("%d ", k); | ||
//fprintf(fp, "%d ", k); | ||
} | ||
finish = clock(); | ||
printf("\n%g\n", (finish - start) / CLOCKS_PER_SEC); | ||
//fclose(fp); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include<stdio.h> | ||
|
||
int isprime(int i); | ||
int main() | ||
{ | ||
int x, y; | ||
for(x = 4; x < 101; x += 2) | ||
{ | ||
for(y = 2; y <= x / 2; y++) | ||
{ | ||
if(isprime(y) != 0) | ||
if(isprime(x - y) != 0) | ||
printf("%d = %d + %d\n", x, y, x - y); | ||
} | ||
} | ||
printf("Proof done.\n"); | ||
return 0; | ||
} | ||
|
||
int isprime(int i) | ||
{ | ||
int j; | ||
for(j = 2; j < i; j++) | ||
{ | ||
if(i % j == 0) | ||
break; | ||
} | ||
if(i == j) | ||
return 1; | ||
else | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include<stdio.h> | ||
#include<string.h> | ||
|
||
int main() | ||
{ | ||
int i, n, command; | ||
char text[256] = {'\0'}, encryption[256] = {'\0'}; | ||
printf("Plz enter your command.\n"); | ||
printf("1.Encrypt\n"); | ||
printf("2.Decrypt\n"); | ||
printf("3.Exit\n"); | ||
scanf("%d", &command); | ||
if(command == 1) | ||
{ | ||
printf("Plz enter the words you want to encrypt.\n"); | ||
scanf("%s", &text); | ||
n = strlen(text); | ||
for(i = 0; i < n; i++) | ||
encryption[i] = text[i] + i + 6; | ||
encryption[i] = '\0'; | ||
printf("Encrypt words are %s.\n", encryption); | ||
} | ||
else if(command == 2) | ||
{ | ||
printf("Plz enter the words you want to decrypt.\n"); | ||
scanf("%s", &encryption); | ||
n = strlen(encryption); | ||
for(i = 0; i < n; i++) | ||
text[i] = encryption[i] - i - 6; | ||
text[i] = '\0'; | ||
printf("Decrypt words are %s.\n", text); | ||
} | ||
else if(command == 3) | ||
return 0; | ||
else | ||
printf("Wrong command,plz try again.\n"); | ||
} |
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.
目前是C语言的作业,不能使用cpp做后缀,也不能使用c++的语法