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

作业 #15

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
40 changes: 40 additions & 0 deletions level1/p01_runningLetter/runningLetter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <windows.h>

int GetWidth()
{
CONSOLE_SCREEN_BUFFER_INFO scr;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hOut, &scr);
return scr.dwMaximumWindowSize.X;
}

int main()
{
int i = 0, width = GetWidth();
while (1)
{
while (i <= width-2)
{
for (int j = 0; j < i; ++j)
{
printf(" ");
}
printf("p");
Sleep(20);
system("cls");
++i;
}
while (i >= 0)
{
for (int j = 0; j < i; ++j)
Copy link
Owner

Choose a reason for hiding this comment

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

重复的味道,试着消除吧

{
printf(" ");
}
printf("p");
Sleep(20);
system("cls");
--i;
}
}
}
31 changes: 31 additions & 0 deletions level1/p02_isPrime/isPrime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>

int main()
{
int n = 0;
scanf_s("%d", &n);
int Isprime = 1;
for (int j = 2; j <= n / 2; j++)
Copy link
Owner

Choose a reason for hiding this comment

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

提成一个函数更舒服一点

{
if (n % j == 0)
{
Isprime = 0;
break;
}
}
if (n == 1)
{
printf("%d��������", n);
}
else
{
if (Isprime == 1)
Copy link
Owner

Choose a reason for hiding this comment

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

改为 if (IsPrime) 更像老手一点

{
printf("%d������", n);
}
if (Isprime == 0)
Copy link
Owner

Choose a reason for hiding this comment

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

可以改为 else

{
printf("%d��������", n);
}
}
}
16 changes: 16 additions & 0 deletions level1/p03_Diophantus/Diophantus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include<stdio.h>

int main()
{
float Diophantus_age = 1;
for (Diophantus_age = 1; 1; Diophantus_age++)
{

if (Diophantus_age / 12 + Diophantus_age / 6 + Diophantus_age / 7 + 5 + Diophantus_age / 2 + 4 == Diophantus_age)
{
break;
}
}
printf("%d", (int)Diophantus_age-4);
return 0;
}
2 changes: 1 addition & 1 deletion level1/p03_Diophantus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

儿子比父亲先死四年,

年级是他的一半
年纪是他的一半

问儿子死时丢番图多大?
19 changes: 19 additions & 0 deletions level1/p04_ narcissus/narcissus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<stdio.h>

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

int main()
{
int i = 2;
int IsPrime = 1;
clock_t start, finish;
double Total_time;
start = clock();
for (i = 2; i <= 1000; i++)
{
IsPrime = 1;
for (int j = 2; j <= i / 2; j++)
{
if (i % j == 0)
{
IsPrime = 0;
break;
}
}
if (IsPrime == 1)
{
printf("%d\n", i);
}
}
finish = clock();
Total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("%f seconds\n", Total_time);
}
59 changes: 59 additions & 0 deletions level1/p06_Goldbach/Goldbach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include<stdio.h>

int main()
{
int i = 2;
int IsPrime = 1;
int a[50] = { 0 };
int k = 0;
for (i = 2; i <= 100; i++)
{
IsPrime = 1;
for (int j = 2; j <= i / 2; j++)
{
if (i % j == 0)
{
IsPrime = 0;
break;
}
}
if (IsPrime == 1)
{
a[k] = i;
k++;
}
}
int sum = 0;
int h = 4;
for (h = 4; h <= 100; h = h + 2)
{
int s = 1;
for (int x = 0; x < k; x++)
{
for (int y = 0; y <= x; y++)
{
if (a[x] + a[y] == h)
{
s = 0;
break;
}
}
if (s == 0)
{
break;
}
}
if (s == 0)
{
sum++;
}
}
if (sum == 49)
{
printf("��°ͺղ�����100��Χ������ȷ��");
}
else
{
printf("��°ͺղ�����100��Χ�ڲ�����ȷ��");
}
}
46 changes: 46 additions & 0 deletions level1/p07_encrypt_decrypt/encrypt_decrypt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <string.h>
#include <iostream>

void encrypt(char* source, char* encrypted)
{
while (*source != '\0')
{
*encrypted = 158 - *source;
source++;
encrypted++;
}
}

void decrypt(char* encrypted, char* source)
{
while (*encrypted != '\0')
{
*source = 158 - *encrypted;
source++;
encrypted++;
}
}

int main()
{
printf("��������Ҫ�����ַ����ij��ȣ�");
int n = 0;
std::cin >> n;
char* a = new char[n + 1]();
char* b = new char[n + 1]();
scanf("%s", a);
encrypt(a, b);
printf("���ܺ���ַ���Ϊ��");
printf("%s\n", b);
printf("��������Ҫ���ܵ��ַ����ij��ȣ�");
int s = 0;
scanf("%d", &s);
char* c = new char[n + 1]();
char* d = new char[n + 1]();
printf("��Ҫ���ܵ��ַ���Ϊ��");
scanf("%s", c);
decrypt(c, d);
printf("���ܺ���ַ���Ϊ��");
printf("%s", d);
}
22 changes: 22 additions & 0 deletions level1/p08_hanoi/hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>

void hanoi(int n, char a, char b, char c)
{
if (n == 1)
{
printf("��Բ��%d��%c�Ƶ�%c", n, a, c);
}
else
{
hanoi(n - 1, a, c, b);
printf("��Բ��%d��%c�Ƶ�%c", n, a, c);
hanoi(n - 1, b, a, c);
}
}
int main()
{
char A = 'A';
char B = 'B';
char C = 'C';
hanoi(64, A, B, C);
}