求一小程序 急!!!!

MQL007 2007-04-28 12:20:47
从键盘输入一个正整数limit(limit>=6),验证从6-n之间的所有偶数都可以分解为两个素数之和
...全文
308 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
MQL007 2007-06-13
  • 打赏
  • 举报
回复
谢了
jacknes009 2007-04-29
  • 打赏
  • 举报
回复
顶下。
chenyi3315 2007-04-29
  • 打赏
  • 举报
回复
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
int limit;
int p1,p2;
int i,flag=0;
cout<<"Please input the number(number>=6):";
cin>>limit;
while(limit < 6)
{
cout<<"input error!"<<endl;
cout<<"Please input the number(number>=6):";
cin>>limit;
}
for(p1=3; p1<limit/2; p1++)
{
for(i=2; i<=sqrt(p1); i++)
{
if(p1%i ==0)
break;
}
if(i>sqrt(p1)) //第一个素数
{
p2 = limit - p1;
for(i=2; i<= sqrt(p2); i++)
{
if(p2%i == 0)
break;
}
if(i > sqrt(p2)) //第二个素数
{
cout<<"DONE"<<endl;
cout<<limit<<" = "<<p1<<" + "<<p2<<endl;
flag = 1;
break;
}
}
}
if(flag == 0) //不能分解为素数之和
cout<<"Cannot be divide into two primes!"<<endl;
system("pause");
return 0;
}
分号 2007-04-29
  • 打赏
  • 举报
回复
printf() 是C的标准输出,即将字符串格式化后输出到屏幕显示,scanf()是输入,从屏幕接收字符.bargio_susie写的printf("%d=%d+%d\n",a,b,d);是输出一句话,如果a=3,b=1,c=2,那么在屏幕上就会输出3=1+2
"\n"是换行.这些在C语言里都有讲解的,LZ可以先打打基础.
MQL007 2007-04-29
  • 打赏
  • 举报
回复
printf("Please input a number:");
scanf("%d",&a);

printf("%d=%d+%d\n",a,b,d);
什么意思呀?
bargio_susie 2007-04-28
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <math.h>
void main()
{
int a,b,c,d;
printf("Please input a number:");
scanf("%d",&a);
for(b=3;b<=a/2;b+=2)
{
for(c=2;c<=sqrt(b);c++)
if(b%c==0)
break;
if(c>sqrt(b))
{
d=a-b;
for(c=2;c<=sqrt(d);c++)
if(d%c==0)
break;
if(c>sqrt(d))
{
printf("%d=%d+%d\n",a,b,d);
break;
}
}
}

}

把输入输出换成C++的方式就OK了。。。
MQL007 2007-04-28
  • 打赏
  • 举报
回复
顶一下

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧