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

Homework 1-6 by LuXiaoya #16

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@
### 其他

[免费的计算机编程类中文书籍](https://github.com/wwj718/free-programming-books-zh_CN)

github打不开

打开CMD运行如下命令 ipconfig /flushdns
48 changes: 48 additions & 0 deletions level1/p01_runningLetter/level1.1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>

int main()
{
system("mode con cols=50 lines=25 ");
char a[100]={0};
int i,j,p;
gets(a);
for(i=0;i<50-strlen(a);i++)
{
for(j=0;j<i;j++)
{
printf(" ");
};
puts(a);
Sleep(200);
system("cls");
}
for(int p=i;p>0;p--)
{
for(int j=0;j<p;j++)
{
printf(" ");
};
puts(a);
Sleep(200);
system("cls");
}



}
//#include<stdio.h>
//#include<sys/types.h>
//#include<sys/ioctl.h>
//#include<unistd.h>
//#include<termios.h>
//int main()
//{
//struct winsize size;
//ioctl(STDIN_FILENO,TIOCGWINSZ,&size);
//printf("%d\n",size.ws_col);
//printf("%d\n",size.ws_row);
//return 0;
//}
15 changes: 15 additions & 0 deletions level1/p02_isPrime/level1.2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>
int main()
{
int n, i;
scanf_s("%d", &n);
for (i = 2;i < n;i++)
{
if (0==n%i)break;
}
if (i == n)
printf(" %d 是素数", n);
else
printf(" %d 不是素数", n);
return 0;
}
6 changes: 5 additions & 1 deletion level1/p03_Diophantus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@

年级是他的一半。

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



算法问题:怎么解方程,年龄是整数还要考虑相除后取整带来的误差
16 changes: 16 additions & 0 deletions level1/p03_Diophantus/level1.3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>

int main()
{
int m=0,i=0,k;
do
{
i++;
if(i%12==0&&i%7==0&&(i/6+i/12+i/7+5+i/2+4)==i)//ֱ����ѭ���ⷽ�̳���ȡ�����õ�����������Ҫ��֤������6,12,7�Ĺ�����
{
m=i;
}
}
while(m!=i);
printf("������ʱ����ͼ��������%d",m-4);
}
24 changes: 24 additions & 0 deletions level1/p04_ narcissus/level1.4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <math.h>
int main()
{
int m,s,t=0,k,i;
printf("��λ��ˮ�ɻ����У�");
for(m=100;m<1000;m++)
{
k=m;
s=0;//s��i��k��Ҫ��ʼ����ע���ʼ����λ��Ӧ����ѭ���ڻ�����
for(i=0;k>0;i++)
{
t=k%10;
s+=pow(t,3);
k=k/10;
}
if(m==s)
{
printf("%d\t",m);
}
}

return 0;
}
89 changes: 89 additions & 0 deletions level1/p05_allPrimes/level1.5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*节省时间:
- break;
- 只用奇数,排除偶数,n+=2
- i<sqrt(n)就可以结束了
- 用数组记录质数,然后直接循环除质数就可以了,不用把所有的数循环一遍
*/

#include <stdio.h>
#include <time.h>
#include <math.h>

void isprime_1();
void isprime_2();

int main()
{
clock_t start_t, finish_t;
double total_t;

start_t = clock();
isprime_1();
finish_t = clock();
total_t = (double)(finish_t - start_t) / CLOCKS_PER_SEC;
printf("\n质数数组法运行总时间是%f 秒\n", total_t);

start_t = clock();
isprime_2();
finish_t = clock();
total_t = (double)(finish_t - start_t) / CLOCKS_PER_SEC;
printf("\n直接循环取余程序运行总时间是%f 秒\n", total_t);
return 0;
}

void isprime_1()
{
int x = 0, isprime, n = 3, a[1000] = { 2,0 };

while (n < 1000)
{
isprime = 1;
for (int i = 0;a[i] < sqrt(n);i++)
{
if (n % a[i]==0)
{
isprime = 0;
break;
}
}
if (isprime)
{
a[x + 1] = n;
x += 1;
}
n += 2;
}


for (int i = 0;i <= x;i++)
{
printf("%d\t", a[i]);
if ((i + 1) % 10 == 0)
{
printf("\n");
}
}
printf("\n");

}

void isprime_2()
{
int n = 0, i, j, isprime;
for (i = 2;i <= 1000; i++)
{
isprime = 1;
for (j = 2; j <= sqrt(i); j++)
if (i % j == 0)
{
isprime = 0;
break;
}
if (isprime)
{
n++;
if (n % 10 == 0) printf("%d\n", i);
else printf("%d\t", i);
}
}
}
70 changes: 70 additions & 0 deletions level1/p06_Goldbach/level1.6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//��°ͺղ��룺��һ����2��ż������д����������֮��
#include <stdio.h>
#include <math.h>

void prime(int a[]);//����һ�� ������100���ڵ�����
int isprime(int q,int a[]);//���������ȶ��ж��Dz�������

//n��ȥһ�������ж��Ƿ��в�ֵΪ���������У����֤
int main()
{
int t,n=4;
int p[100]={0};
prime(p);
while(n<=100)
{
for(int i=0;p[i]>0;i++)
{
if(isprime(n-p[i],p))
{
printf("%d=%d+%d\n",n,p[i],n-p[i]);
break;
}
if(p[i]==0)
{
printf("Error");
}
}
n+=2;
}
printf("If there is no error,Goldbach conjecture has been proved.")
return 0;

}

void prime(int a[])
{
int x=0,isprime,n=3;
while(n<100)
{
isprime=1;
a[0]=2;
for(int i=0;a[i]<sqrt(n);i++)
{
if(n%a[i]==0)
{
isprime=0;
break;
}
}
if(isprime)
{
a[x+1]=n;
x+=1;
}
n+=2;
}
}

int isprime(int q,int a[])
{
int isprime=0;
for(int i=0;a[i]>0;i++)
{
if(q==a[i])
{
isprime=1;
}
}
return isprime;
}
Binary file not shown.
Loading