[题目] 求取值范围内所有满足条件的x,使得x的每位数字的立方和与本身相等。
[编程要求]
试建立一个类DIF,完成求出某个范围内的所有满足条件的x,以及x的个数。
具体要求如下:
(1)私有数据成员。
int low,high:x的取值范围的下限和上限。
int a[100]:存放满足要求的x.
int count:满足条件的x的个数。
(2)公有成员函数
DIF(int lw,int hi):构造函数,用参数lw和hi分别初始化low和high。缺省的取值范围是[10,1000]。
int isdiff(int x):判断参数x是否为水仙花数,若是返回1,若不是返回0。
void process():求出满足条件的所有x,并将结果存入数组a,同时统计x的个数。
void show():输出数组a及count。
(3)在主函数中完成对该类的测试。定义一个DIF类的对象v,使用100和999初始化其下限和上限,按上述要求处理并输出结果。
*/
#include<iostream.h>
#include<fstream.h>
class DIF
{
int low,high;
int a[100];
int count;
public:
DIF(int lw=10,int hi=1000)
{low=lw;high=hi;}
int isdiff(int x)
{
/********** Begin **********/
/********** End **********/
}
void process()
{
count=0;
for(int x=low;x<=high;x++)
{
if(isdiff(x))
a[count++]=x;
}
}
void show()
{ for(int i=0;i<count;i++)
cout<<a[i]<<'\t';
cout<<'\n';
cout<<"count="<<count;
//此处将结果输出到文件"bc02.in",请勿改动,否则影响判分
ofstream outf("bc02.in");
for( i=0;i<count;i++)
outf<<a[i]<<'\t';
outf<<'\n';
outf<<"count="<<count;
outf.close();
}
};
void main()
{ DIF v(100,999);
v.process();
v.show();
}