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

王益龙 #17

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
66 changes: 66 additions & 0 deletions level1/p01_runningLetter/runningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <string.h>
//constants
enum{ N = 1000 , sleep_time = 50};
Copy link
Owner

Choose a reason for hiding this comment

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

习惯上,常量用全大写+下划线

Copy link
Author

Choose a reason for hiding this comment

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

已修改

char space[N];
int i;
//func
int GetWidth();
int abs(int x);
//entertainment
void DrawLine(int width);
void ChangeColor(int cnt);

int main(int argc, char const *argv[])
{
printf("Please enter several letters within a line:\n");
//Init
char str[N]; scanf("%s",str);
int len = strlen(str);
int width = GetWidth();
int pos;
//Play
int cnt = 0;
while (1) {
/*calculate pos*/
pos = width - abs(width - ( cnt++ % (2 * width) ) );
/*prevent crossing the border*/
if (pos + len > width) continue;

ChangeColor(cnt);
system("cls");
DrawLine(width);
/*Output*/
for (i = 0; i < width; ++ i)
space[i] = i < pos || i >= pos + len ? ' ' : str[i - pos];
space[i] = '\0';
printf("%s",space);

DrawLine(width);
/*Sleep a while, let the user watch.*/
Sleep(sleep_time);
}
return 0;
}
int GetWidth()
{
CONSOLE_SCREEN_BUFFER_INFO scr;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hOut, &scr);
return scr.dwMaximumWindowSize.X;
}
int abs(int x)
{
return x >= 0 ? x : -x;
}
void DrawLine(int width)
{
for (i = 0; i < width; ++ i) space[i] = '*'; space[i] = '\0';
printf("%s\n",space);
}
void ChangeColor(int cnt) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),cnt % 16);
// SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),(cnt % 16) * 16 + 16 - (cnt % 16));
}
17 changes: 17 additions & 0 deletions level1/p02_isPrime/isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>
int main(){
int n,i;
scanf("%d",&n);
if (n <= 1) {
printf("error");
return 0;
}
for (i = 2; i <= n / i; ++ i) {
if (n % i == 0) {
printf("Not prime.");
return 0;
}
}
printf("Is prime.");
return 0;
}
12 changes: 12 additions & 0 deletions level1/p03_Diophantus/Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
int main(){
double t;
for (t = 5; ; ++ t) {
if (t / 6 + t / 12 + t / 7 + 5 + t / 2 + 4 == t) {
printf("%g\n",t - 4);
break;
}
}
system("pause");
return 0;
}
17 changes: 17 additions & 0 deletions level1/p04_ narcissus/narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>
int is_narcissus(int x){
int a = x / 100;
int c = x % 10;
int b = (x - a * 100 - c) / 10;
if (x == a * a * a + b * b * b + c * c * c)
return 1;
else return 0;
}
int main(){
int i;
for (i = 100; i <= 999; ++ i) {
if (is_narcissus(i)) printf("%d\n",i);
}
system("pause");
return 0;
}
32 changes: 32 additions & 0 deletions level1/p05_allPrimes/allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <time.h>
enum {N = 1001};
int primes[N], cnt;
int sieved[N];

void get_primes(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.

漂亮的函数原型!

int main(int argc, char const *argv[])
{
clock_t timer = clock();
get_primes(1000);
int i;
for (i = 0; i < cnt; ++ i) {
printf("%d\t",primes[i]);
}
printf("\nTime:%dms\n",clock() - timer);
system("pause");
return 0;
}
void get_primes(int n)
{
int i,j;
for (i = 2; i <= n; i ++ )
{
if (!sieved[i]) primes[cnt ++ ] = i;
for (j = 0; primes[j] <= n / i; j ++ )
{
sieved[primes[j] * i] = 1;
if (i % primes[j] == 0) break;
}
}
}
33 changes: 33 additions & 0 deletions level1/p06_Goldbach/Goldback.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
enum {N = 100};
int sieved[N + 1];
int primes[N + 1],cnt;
int i,j,k,l;
void init_primes();
int main(){
init_primes();
for (i = 6; i <= N; ++ i){
for (j = 0; j < cnt; ++ j)
for (k = 0; k < cnt; ++ k)
for (l = 0; l < cnt; ++ l)
if (primes[j] + primes[k] + primes[l] == i) {
printf("%d == %d + %d + %d\n",i,primes[j],primes[k],primes[l]);
goto flag;
}
printf("It is wrong within 100!");
flag:
;
}
printf("It is verified that Goldbach conjecture is right within 100.\n");
system("pause");
return 0;
}
void init_primes(){
for (i = 2; i <= N; ++ i) {
if (!sieved[i]) {
primes[cnt ++] = i;
for (j = i + i; j <= N; j += i)
sieved[j] = 1;
}
}
}
74 changes: 74 additions & 0 deletions level1/p07_encrypt_decrypt/encrypt_decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdlib.h>
char book[] = "18381048095";
typedef struct string {
char * str;
int length;
int capacity;
} string;
void get_str(string * str);
void init_str(string * str);
void update(string * str);
void encrypt(string * str);
void decrypt(string * str);
void showStr(string * str);
int main(int argc, char const *argv[])
{
printf("Please enter the letters:\n");
string * str = (string *)malloc(sizeof(string));
get_str(str);
char ch;
printf("Enter 'e' to encrypt it, 'd' to decrypt it and 'q' to quit.\n");
while (ch = getchar()) {
if (ch == 'q') break;
else if (ch == 'e') encrypt(str);
else if (ch == 'd') decrypt(str);
else if (ch == '\n') continue;
else {
printf("Invalid input!\n");
continue;
}
showStr(str);
}
return 0;
}
void showStr(string * str) {
int i;
for (i = 0; i < str->length; ++ i){
putchar(str->str[i]);
}
putchar('\n');
}
void encrypt(string * str) {
int i;
for (i = 0; i < str -> length; ++ i) str -> str[i] += book[i % 11] - '0';
}
void decrypt(string * str) {
int i;
for (i = 0; i < str -> length; ++ i) str -> str[i] -= book[i % 11] - '0';
}
void get_str(string * str) {
init_str(str);
char ch;
while (ch = getchar()) {
if (ch == '\n') break;
update(str);
str->str[str->length++] = ch;
}
}
void init_str(string * str) {
str -> length = 0;
str -> capacity = 10;
str -> str = (char*)malloc(sizeof(char) * str -> capacity);
}
void update(string * str) {
if (str -> length > str -> capacity - 3) {
str -> capacity += str -> capacity / 2;

char * p = (char*)malloc(sizeof(char) * str -> capacity);
int i;
for (i = 0; i < str -> length; ++ i)
p[i] = str -> str[i];
str -> str = p;
}
}
15 changes: 15 additions & 0 deletions level1/p08_hanoi/hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>
void hanoi(int n,char origin,char medium,char dest) {
if (n == 1) {
printf("%c -> %c\n", origin, dest);
return;
}
hanoi(n-1,origin,dest,medium);
printf("%c -> %c\n", origin, dest);
hanoi(n-1,medium,origin,dest);
}
int main() {
int n = 3;
hanoi(n,'A','B','C');
return 0;
}
Loading