TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评[cpp] view plaincopy![]() ![]()
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"> /*
- 功能: 已知N是正整数, 它可拆写成三个正整数N1、N2和N3之和的形式N=N1+N2+N3。
- 请编程序打印出满足上式的全部组合,并当N1、N2和N3中至少有两个是素数时打印“YES”否则打印“NO”。
- 如N=5时输出:
- 5 = 1 + 1 + 3 NO
- 5 = 1 + 2 + 2 YES
- 5 = 1 + 3 + 1 NO
- 5 = 2 + 1 + 2 YES
- 5 = 2 + 2 + 1 YES
- 5 = 3 + 1 + 1 NO
-
- 时间:2:12 2013/10/25
- */
-
- #include<stdio.h>
- #include<stdlib.h>
- #include <math.h>
-
- int judePri(int); //判断一个数是否为素数
-
- void main()
- {
- int N;
- scanf_s("%d",&N);
-
- for(int i=1;i<N;i++)
- {
- for(int j=1;j<=N;j++)
- {
- int k=N-i-j;
- if(i+j+k==N && k>0) //找到满足条件的i,j,k
- {
- int sum=judePri(i)+judePri(j)+judePri(k); //sum保存素数的个数
- printf("%d + %d + %d = %d\t",i,j,k,N); //打印结果
- if(sum>=2) //判断素数个数是否大于等于2
- {
- printf("YES\n");
- }
- else
- {
- printf("NO\n");
- }
- }
- }
- }
- system("pause");
- }
-
- int judePri(int n)
- {
- if(n==1)
- {
- return 0;
- }
- else if (n==2||n==3)
- {
- return 1;
- }
- else
- {
- for(int i=2;i<=(int)sqrt((double)n);i++)
- {
- if(n%i==0)
- return 0;
- }
- }
- return 1;
- }</pre><br><br></pre></pre></pre>
|
|