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

lvl1_1_to_8 #29

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions practices/c/level1/p01_runningLetter/lvl1_1_Running_letter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* code: Running Letter
* author: Kyrios0
* date: 2017.02.26
* state: finished
* version: 1.0.0
*/

#include<stdio.h>
#include<windows.h>

void gotoxy(int x, int y)
{
HANDLE hOutput;
COORD coo;
coo.X = x;
coo.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, coo);
}

int main(int argc, char** argv)
{
system("chcp 437");
system("color 0f");
system("mode con cols=63 lines=32");

int x = 0, y = 0, round = 0;

while(1)
{
system("cls");

gotoxy(x, y);
putchar('#');
gotoxy(x, y);

(round % 2 == 0) ? x++ : x--;
if((x == 0) || (x == 63))
{
round++;
}

Sleep(25);
}
return 0;
}
105 changes: 105 additions & 0 deletions practices/c/level1/p02_isPrime/lvl1_2_isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// isPrime.cpp
//

#include "stdafx.h"
/* code: Linked list Step0 for eratosthenes Prime (change from lvl1_5)
* author: Kyrios0
* date: 2017.02.21
* state: finished
* version: 1.0.2
*/

#include<stdio.h>
#include<stdlib.h>//malloc()
#include<string.h>
#include<Windows.h>

typedef struct linked {
int data;
linked *next;
}linked;

linked* creat(int length)
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.

实现一个简单的筛法..链表移除数据比数组方便...

Copy link
Author

Choose a reason for hiding this comment

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

这个题和Goldbach都是偷懒从先写的lvl1_5改过来的...所以看起来比较奇怪...

Copy link
Author

Choose a reason for hiding this comment

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

那段时间刚学链表。。。什么都想用链表做。现在看在反而是弄巧成拙了。。等有空了重新写一份顺便优化一下算法。。。

{
linked *head, *tail, *p;
int x;
head = tail = NULL;

for (x = 0; x < length; x++)
{
p = (linked *)malloc(sizeof(linked));
p->data = x + 2;//0 and 1 aren't prime or composite number
p->next = NULL;

if (head == NULL)
{
head = tail = p;
}
else
{
tail = tail->next = p;
}
}

return head;
}

int main(int argc, char** argv)
{
long len, userNumber, finalNumber = 2;

puts("Prime Number Detection System");
puts("Please enter the number you want to detect(It should be no less than 2)");
scanf("%ld", &userNumber);

len = userNumber;
linked *current, *head, *temp, *currenthead;
current = creat(len);
head = current;
currenthead = head;

puts("Working...");

for (long i = 0; ; i++)
{
if (i * i >= len)//Termination
{
break;
}

while (current->next != NULL)
{
temp = current->next;

while (temp->data % currenthead->data == 0)//Delete this number
{
current->next = temp->next;
temp = temp->next;
len--;
if (temp == NULL)
{
break;
}
}

if (current->next == NULL)
{
break;
}
else
{
current = current->next;
}
}

finalNumber = current->data;
currenthead = currenthead->next;
current = currenthead;
}

printf(((finalNumber == userNumber) || (userNumber == 2)) ? ("%d is a prime number\n") : ("%d is a composite number\n"), userNumber);

system("pause");

return 0;
}
4 changes: 3 additions & 1 deletion practices/c/level1/p03_Diophantus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@

年级是他的一半。

问儿子死时丢番图多大?
问儿子死时丢番图多大?

/* *just for test* */
27 changes: 27 additions & 0 deletions practices/c/level1/p03_Diophantus/lvl1_3_Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* author: Kyrios0
* date: 2017.02.23
* version: 1.0.0
* state: finished
*/

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(int argc, char *argv[]) {
system("chcp 437");
system("cls");
double age = 0.0, ans = 0.0;
while(1)
{
ans = (1.0/6.0 + 1.0/12.0 + 1.0/7.0 + 1.0/2.0) * age + 9.0;
if(ans - age < 0.1)
{
printf("He was %.0f years old at that time. \n", ans - 4);
system("pause");
return 0;
}
age += 1.0;
}
return 0;
}
2 changes: 1 addition & 1 deletion practices/c/level1/p04_ narcissus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

水仙花数:n位数的每个数位的n次方之和等于该n位数本身

例如:153=1^3+5^3+3^3
例如:153=1^3 + 5^3 + 3^3
36 changes: 36 additions & 0 deletions practices/c/level1/p04_ narcissus/lvl1_4_narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* code: Narcissus
* author: Kyrios0
* date: 2017.02.26
* state: finished
* version: 1.0.0
*/

#include "stdafx.h"
#include<stdio.h>
#include<windows.h>
#include<math.h>

int main(int argc, char** argv)
{
system("chcp 437");
system("cls");
int hun, ten, one, sum = 100;//hun = 100
puts("The following is the number of narcissus in 1000");

while (sum < 1000)
{
one = sum % 10;
ten = sum / 10 % 10;
hun = sum / 100;

if ((int)(pow(double(one), 3.0) + pow(double(ten), 3.0) + pow(double(hun), 3.0)) == sum)
{
printf("%d\n", sum);
}

sum++;
}

system("pause");
return 0;
}
115 changes: 115 additions & 0 deletions practices/c/level1/p05_allPrimes/lvl1_5_allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// allPrime.cpp
//

#include "stdafx.h"
/* code: Linked list Step0 for eratosthenes
* author: Kyrios0
* date: 2017.02.21
* state: finished
* version: 1.0.2
*/

#include<stdio.h>
#include<stdlib.h>//malloc()
#include<string.h>
#include<Windows.h>

typedef struct linked {
int data;
linked *next;
}linked;

linked* creat(int length)
{
linked *head, *tail, *p;
int x;
head = tail = NULL;

for (x = 0; x < length; x++)
{
p = (linked *)malloc(sizeof(linked));
p->data = x + 2;//0 and 1 aren't prime or composite number
p->next = NULL;

if (head == NULL)
{
head = tail = p;
}
else
{
tail = tail->next = p;
}
}

return head;
}



int main(int argc, char** argv)
{
long len = 10000000;//about 40s
linked *current, *head, *temp, *currenthead;
current = creat(len);
head = current;
currenthead = head;

puts("Working...");

for (long i = 0; ; i++)
{
if (i * i >= len)//Termination
{
break;
}

while (current->next != NULL)
{
temp = current->next;

while (temp->data % currenthead->data == 0)//Delete this number
{
current->next = temp->next;
temp = temp->next;
len--;
if (temp == NULL)
{
break;
}
}

if (current->next == NULL)
{
break;
}
else
{
current = current->next;
}
}

currenthead = currenthead->next;
current = currenthead;
}

FILE *fpt;
fpt = fopen("PrimeList.txt", "w");

puts("Start writing... Please wait a moment.");

for (int i = 0; i < len; i++)//output
{
fprintf(fpt, "%d, ", head->data);
head = head->next;
if ((i % 100000 == 0) && (i != 0))
{
printf("%d numbers has been writen: %d \n", i, head->data);
}
}

puts("DONE!");

system("pause");

return 0;
}
Loading