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

level1 2-5 #26

Open
wants to merge 2 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
34 changes: 34 additions & 0 deletions P02_isprime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
#include<stdlib.h>
int IS(int); //判断是否为素数
int main() {
int x;
printf("输入一个正整数,程序判断它是否为素数(q to quit):");
while (scanf_s("%d", &x))
{
if (x == 1 || x == 2) {
puts("是素数");
}
else if (IS(x))
puts("是素数.");
else puts("不是素数.");
printf("输入一个正整数,程序判断它是否为素数(q to quit):");
}
system("pause");
return 0;
}
int IS(int x)
{
int i;
int mark = 0; //创建标记
for (i = 2; i < x; i++)
{
if (x%i == 0)
{
mark = 1; //若被整除,标记mark为1
break;
}
}if (mark == 1)
return 0;
else return 1;
}
14 changes: 14 additions & 0 deletions p03_Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<stdio.h>
#include<stdlib.h>

void main()
{
int i;
for (i = 1; 28 * i != 21 * i + 4 * i + 252; i++)
{
;
}
printf("丢番图的年纪是%d\n", i);
system("pause");
}

19 changes: 19 additions & 0 deletions p04_ narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a, b, c;
for (a = 0; a <= 9; a++)
{
for (b = 0; b <= 9; b++)
{
for (c = 0; c <= 9; c++)
{
if (a*a*a + b*b*b + c*c*c == a + 10 * b + 100 * c && !( b == 0 && c == 0)) //排除非三位数(0开头)的情况
printf("%d%d%d\n", c, b, a);
}
}
}
system("pause");
return 0;
}
35 changes: 35 additions & 0 deletions p05_allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int isprinme(int a);
int IS(int);
int main()
{
clock_t t1, t2;
t1 = clock();
for(int i=2;i<=1000;i++)
{
if (isprinme(i))
printf("%5d", i);
}
t2 = clock();
printf("打印时间:%f秒",(double)(t2-t1)/CLOCKS_PER_SEC);
system("pause");
return 0;
}
int isprinme(int a)
{
int mark = 0;
for (int i = 2; i < a; i++)
{
if (a%i == 0) //用mark标记a已被整除
{
mark = 1;
break;
}

}
if (mark == 1) //检查标记
return 0;
else return 1;
}
54 changes: 54 additions & 0 deletions p06_Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int loadPrime(int* a);
int isPrime(int x);
int main()
{
int prime[100] = {0};
int sum;
int a = 4;
sum=loadPrime(prime);
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < sum; j++)
{
if (a == prime[i] + prime[j]) //遍历两个100以内的素数组合之和,与a作比较
{
printf("%3d满足哥德巴赫猜想\n", a);
a += 2; //判断下一个偶数
}

}
}
system("pause");
return 0;
}
int isPrime(int x)
{
int i;
int mark = 0; //创建标记
for (i = 2; i < x; i++)
{
if (x%i == 0)
{
mark = 1; //若被整除,标记mark为1
break;
}
}if (mark == 1)
return 0;
else return 1;
}
int loadPrime(int* a)
{
int x = 0;
for (int i = 2; i < 100; i++)
{
if (isPrime(i))
{
a[x] = i; //把素数赋值给数组
x++;
}
}
return x; //返回100以内的素数个数
}