天天看点

UVA 543

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=484&mosmsg=Submission+received+with+ID+11313357

任何大于4的偶数都能写成sum of two odd prime number ,只要从小开始逐一判断,然后写出最大和最小的组合就可以了

View Code

1 #include<stdio.h>
 2 #include<math.h>
 3 int prime(int a)
 4 {
 5  int i,flag=1;
 6  for(i=2;i<=sqrt(a);i++)
 7     {
 8      if(a%i==0)
 9        {
10         flag=0;
11         break;
12        }
13     }
14  return flag;
15 }
16 int main()
17 {
18  int n,a,flag;
19  while(scanf("%d",&n)&&n)
20       {
21        flag=0;
22        for(a=2;a<=n/2;a++)
23           {
24            if(prime(a)&&prime(n-a))
25               {
26                flag=1;
27                break;
28               }
29           }
30        if(flag==0)
31          printf("Goldbach's conjecture is wrong.\n");
32        else 
33          printf("%d = %d + %d\n",n,a,n-a);
34       }
35  return 0;
36 }      

转载于:https://www.cnblogs.com/huzhenbo113/articles/2918262.html

php