题目主要是:用类找连续的合数(n个连续的自然数均为合数)
#include<iostream>
using namespace std;
class NUM
{
private:
int n;
int* p;
public:
NUM(int n1) { n = n1; p = new int[n1]; };
int yes(int x);
void print();
void fun();
~NUM();
};
int NUM::yes(int x)
{
for (int i = 0; i < x / 2; i++)
{
if (x % i == 0)
{
return 1;
}
}
return 0;
}
void NUM::fun()
{
for (int i = 3; ; i++)
{
int j = 0;
while (j < n && yes(i))
{
p[j] = i;
i++;
j++;
}
if (j == n)
{
break;
}
}
}
void NUM::print()
{
for (int i = 0; i < n; i++)
{
cout << p[i] << " ";
}
}
NUM::~NUM()
{
if(p) delete []p;//这个地方不清楚为什么要加if(p)?
}
int main()
{
NUM num(10);
num.fun();
num.print();
return 0;
}