题目描述
定义一个类CLuckNumber,数据成员有整数(int n),指向整型数的指针(int *p)。成员函数有:
-
构造函数。
-
析构函数。
-
求不三不四喜七数的函数,即把小于n的所有既不能被3整除也不能被4整除但能被7整除找出来,放到p指向的数组中。
-
打印所有不三不四喜七数的函数。
输入
第一行输入测试次数
每次测试输入一个表示数值范围的正整数。
输出
每次测试输出一行,包括1-n范围内的所有不三不四喜七数。
输入样例1
2
10
100
输出样例1
7
7 14 35 49 70 77 91 98
代码
#include <iostream>
using namespace std;
class CLuckNumber
{
private:
int n;
int *p;
int cnt;
public:
CLuckNumber(){}
CLuckNumber(int num)
{
n = num;
p = new int[num];
cnt = 0;
}
bool check(int x)
{
if(x % 3 != 0 && x % 4 != 0 && x % 7 == 0) return true;
return false;
}
void add()
{
for(int i = 1;i < n;i ++)
{
if(check(i)) p[cnt ++] = i;
}
}
void print()
{
for(int i = 0;i < cnt;i ++)
{
if(i) cout << " ";
cout << p[i];
}
cout << endl;
}
~CLuckNumber()
{
delete []p;
}
};
int main()
{
int t;
cin >> t;
while(t --)
{
int num;
cin >> num;
CLuckNumber temp(num);
temp.add();
temp.print();
}
return 0;
}