自然数集合中找合数

来源:百度知道 编辑:UC知道 时间:2024/05/26 06:09:33
试定义一个类NOPRIME,从3开始向上试探找出n个连续的自然数,且它们都是合数。当找到第一组连续合数后,即停止查找
#include<iostream>
using namespace std;
int count=0;
class NOPRIME
{
public:
int n;
int p[10000];
NOPRIME(int n1)//连续的个数
{
n=n1;
}
int yes(int x)//判断是否是合数
{
for(int i=2;i<x/2;i++)
if(x%i==0)
return 1;
return 0;
}
void Search();
};
void NOPRIME::Search ()
{
for (int i=3;i<1000;i++)
{
p[i-2]=i;
if (yes(i))
{
count++;
}
else
count=0;
if (count==n)
{
for (int j=i-n-1; j<i-1 ; j++)
cout<<p[j]<<'\t';
return ;}
}
}

int main()
{
NOPRIME num(10);
num.Search();
return 0;
}
(1)将以上以数组为数据结构的程序实现改写为以单向链表为数据结构重新实现。
(2)首先生成结点数n的链表,然后将每次求出的合数依次存放到链表中,直到寻找到满足条件的连续合数后,将链表结点内容输出,在析构函数中依次将链表结点删除。
请帮忙修改一下程序,使程序满

#include<iostream>
using namespace std;
int count=0;
class NOPRIME
{
public:
int n;
int p[10000];
NOPRIME(int n1)//连续的个数
{
n=n1;
}
int yes(int x)//判断是否是合数
{
for(int i=2;i<x/2;i++)
if(x%i==0)
return 1;
return 0;
}
void Search();
};
void NOPRIME::Search ()
{
for (int i=3;i<1000;i++)
{
p[i-2]=i;
if (yes(i))
{
count++;
}
else
count=0;
if (count==n)
{
for (int j=i-n-1; j<i-1 ; j++)
cout<<p[j]<<'\t';
return ;}
}
}

int main()
{
NOPRIME num(10);
num.Search();
return 0;
}

那种语言???

4 6 8 10 12 14 15 16 18 20 21 22 24 26 25 27 28 30 32 33 34 35 36 38 39 40 42 44 45 46 48 49 50 51 52 54 55 56 57 58 60 62 63 64 65 66 68 69 70 72 74 75 76 77 78 80 81 82 84 85 86 87 88