link:http://acm.hdu.edu.cn/showproblem.php?pid=2098
Problem Description
把一个偶数拆成两个不同素数的和,有几种拆法呢?
Input
输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
Output
对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
Sample Input
30
26
0
Sample Output
3
2
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int f(int x)
{
int i,s;
for(i=2;i<=sqrt(x);i++)
if(x%i==0)
return 0;
return 1;
}
int main()
{
int a,i,n,s;
while(scanf("%d",&a),a)
{
s=0;
for(i=2;i<a-i;i++)
{
if(i!=a-i)
if(f(i)&&f(a-i))//如果i和a-i都为素数,s+1
s++;
}
printf("%d\n",s);
}
return 0;
}
本文介绍了一种算法,用于解决将偶数拆分为两个不同素数之和的问题,并提供了具体的C语言实现代码。该算法首先定义了一个判断素数的函数f,然后遍历所有可能的素数对,统计符合条件的不同素数拆分数。
1029

被折叠的 条评论
为什么被折叠?



