滴水逆向联盟
标题:
基于visual Studio2013解决C语言竞赛题之1057打印加数
[打印本页]
作者:
大灰狼
时间:
2014-8-14 08:45
标题:
基于visual Studio2013解决C语言竞赛题之1057打印加数
题目
download1.png
(7 KB, 下载次数: 693)
下载附件
保存到相册
2014-8-14 08:45 上传
解决代码及点评
[cpp]
view plain
copy
<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>
欢迎光临 滴水逆向联盟 (http://dtdebug.com/)
Powered by Discuz! X3.2