Skip to content
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
21 changes: 21 additions & 0 deletions level1/p01_runningLetter/RunningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#define length 80

int main() {
int i;
for (i = 0; i >= 0; ++i) {
int a = i % length;
int b = (i / length)%2;
int c = abs(length*b - a);
int j;
for (j = 0; j <= c; ++j) {
printf(" ");
}
printf("S");
Sleep(40);
system("cls");
}
}
34 changes: 34 additions & 0 deletions level1/p01_runningLetter/RunningLetters.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>

int main() {


HANDLE handle_out;
CONSOLE_SCREEN_BUFFER_INFO screen_info;
COORD pos = { 0, 0 };
handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(handle_out, &screen_info);


int length = screen_info.dwSize.X - 1;
int i;
for (i = 0; i >= 0; ++i) {
int a = i % length;
int b = (i / length) % 2;
int c = abs(length*b - a);
int j;
for (j = 0; j <= c; ++j) {
printf(" ");
}
printf("S");
Sleep(40);
system("cls");
}


CloseHandle(handle_out);

}
24 changes: 24 additions & 0 deletions level1/p02_isPrime/isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main() {
long long i;
long long number;
scanf_s("%lld", &number);
if (number <= 2) {
printf("Please enter an integer greater than 2");
}
else {
for (i = 2; i <= sqrt(number); ++i) {
if (number %i == 0) {
printf("This is not a prime number");
break;
}
}
if (i > sqrt(number)) {
printf("This is a prime number");
}
}
system("pause");
}
22 changes: 22 additions & 0 deletions level1/p03_Diophantus/Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>

int main() {

for (int i = 0; i <= 100; ++i) {
int lt, lq, ld, t1, t2;
t1 = 5;
t2 = 4;
if (i % 6 != 0 || i % 12 != 0 || i % 7 != 0) {
continue;
}
lt = i / 6;
lq = i / 12;
ld = i / 7;
int l1 = lt + lq + ld;
if (l1+t1+i/2+4==i) {
printf("%d\n", i - 4);
}
}
system("pause");
}
15 changes: 15 additions & 0 deletions level1/p04_ narcissus/narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>

int main() {
for (int i = 1; i <= 9; ++i) {
for (int j = 0; j <= 9; ++j) {
for (int k = 0; k <= 9; ++k) {
if (i*i*i + j*j*j + k*k*k == i * 100 + j * 10 + k) {
printf("%d\n", i * 100 + j * 10 + k);
}
}
}
}
system("pause");
}
26 changes: 26 additions & 0 deletions level1/p05_allPrimes/allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>


int main() {
int begin, end;
begin = clock();
int n=0;
for (int i = 2; i <= 100; ++i) {
int j;
for (j = 2; j <= sqrt(i); ++j) {
if (i%j == 0) {
break;
}
}
if (j>sqrt(i)) {
n = n + 1;
}
}
end = clock();
printf("%d", n);
printf("��ʱ�� %d ms", end - begin);
system("pause");
}
41 changes: 41 additions & 0 deletions level1/p06_Goldbach/Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int isPrime(int n);

int main() {
for (int i = 4; i <= 100; i += 2) {
for (int j = 2; j <= i; ++j) {
int p1 = isPrime(j);
int flag=0;
for (int k = 2; k <= i; ++k) {
int p2 = isPrime(k);
if (p1+p2==i)
{
printf("%d=%d+%d\n", i, p1 ,p2);
flag = 1;
break;
}
}
if (flag == 1)
{
break;
}
}
}
system("pause");
}

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

int main() {
int n;
scanf_s("%d", &n);
char column1 = 65;
char column2 = 66;
char column3 = 67;
move(n,column1,column2,column3);
system("pause");
}

int move(int n,char c1,char c2,char c3) {
if (n >= 2) {
n = n - 1;
move(n, c1, c3, c2);
putchar(c1); printf("-->"); putchar(c3); printf("\n");
move(n, c2, c1, c3);
}
else {
putchar(c1); printf("-->"); putchar(c3); printf("\n");
}
return 0;
}
72 changes: 72 additions & 0 deletions level1/p09_maze/maze.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

void wall();
int path(int x, int y);

#define MaxLength 50
#define xLength 11
#define yLength 11
char coordinate[MaxLength + 2][MaxLength + 2] = { 0 };
int c1, c2;

int main() {

wall();

for (int i = 0; i <= xLength * 2 + 2; ++i) {
printf("\n");
for (int j = 0; j <= yLength * 2 + 2; ++j) {
if (coordinate[i][j] == 1) {
printf(" ");
}
else {
printf("* ");
}
}
}

system("pause");

}

void wall() {

for (c1 = 0, c2 = yLength * 2 + 2; c1 <= xLength * 2 + 2; ++c1) {
coordinate[c1][0] = 1;
coordinate[c1][c2] = 1;
}
for (c1 = xLength * 2 + 2, c2 = 0; c2 <= yLength * 2 + 2; ++c2) {
coordinate[0][c2] = 1;
coordinate[c1][c2] = 1;
}

coordinate[2][1] = 1;
coordinate[xLength * 2][yLength * 2 + 1] = 1;

srand((unsigned)time(NULL));
path(rand() % xLength + 1, rand() % yLength + 1);
}

int path(int x, int y) {
static int direction[4][2] = { 0,1,1,0,0,-1,-1,0 };
int cx = x * 2;
int cy = y * 2;
coordinate[cx][cy] = 1;
int turn = rand() % 2 ? 1 : 3;
int next, i;
for (i = 0, next = rand() % 4; i < 4; ++i, next = (next + turn) % 4) {
int x1 = cx + direction[next][0] * 2;
int y1 = cy + direction[next][1] * 2;
int x2 = cx + direction[next][0];
int y2 = cy + direction[next][1];
int x3 = x + direction[next][0];
int y3 = y + direction[next][1];
if (coordinate[x1][y1] == 0) {
coordinate[x2][y2] = 1;
path(x3, y3);
}
}
return 0;
}
92 changes: 92 additions & 0 deletions level1/p11_linkedList/linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct linkedlist
{
int data;
struct linkedlist *next;
} node;

node *create(int n) {
node *head, *p, *pNext;
head = (node *)malloc(sizeof(node));
head->next = NULL;
pNext = head;
for (int i = 1; i <= n; ++i) {
printf("�������%d������:", i);
p = (node *)malloc(sizeof(node));
scanf_s("%d", &p->data);
p->next = NULL;
pNext->next = p;
pNext = p;
}
return head;
}

void show(node *head) {
node *tail;
tail = head->next;
while (tail != NULL)
{
if (tail->next != NULL)
{
printf("%d ", tail->data);
}
else
{
printf("%d\n", tail->data);
}
tail = tail->next;
}
}

node *reverse(node *head) {
node *current, *pnext, *prev;
if (head == NULL || head->next == NULL)
return head;
current = head->next;
pnext = current->next;
current->next = NULL;
while (pnext)
{
prev = pnext->next;
pnext->next = current;
current = pnext;
pnext = prev;
}
head->next = current;
return head;
}

void search(node *head,int n) {
node *current;
current = head;
int count = 0;
int flag = 0;
while (current) {
if (current->data == n) {
printf("%d ", count);
flag = 1;
}
count++;
current = current->next;
}
if (flag = 0) {
printf("-1");
}
}

int main() {
printf("������ڵ����:");
int n;
scanf_s("%d", &n);
node *head;
head = create(n);
show(head);
reverse(head);
show(head);
printf("������Ҫ���ҵ���:");
scanf_s("%d",&n);
search(head, n);
system("pause");
}