C++自守数
来源:互联网 发布:算法 benchmark 编辑:程序博客网 时间:2024/06/08 11:10
/***********************************************//* 求指定范围的自守数 *//* *//**//**//***********************************************/#include <iostream>#include <stdlib.h>using namespace std;void getArray(int a,int Array[]);class SELF{ public: SELF(); SELF(int,int); void getRange(); void getNumber(); void display(); private: int m; int n; int a[20]; int p;};SELF::SELF(){ n = 0; m = 0; p = 0;}SELF::SELF(int a,int b){ n = a; m = b; p = 0;}void SELF::getRange(){ cout<<"请输入所需范围的下线:"; cin >>n; cout<<endl<<"请输入所需范围的上线:"; cin >>m; cout<<endl;}void SELF::getNumber(){ int i,j,k,o=0; int b[] = {0},c[] = {0}; for(i=n;i<=m;i++) { getArray(i,b); getArray(i*i,c); k = 1; for(j=0;j<sizeof(b)/sizeof(int);j++) { if(b[j]!=c[j]) { k = 0; } } if(k==1) { p++; a[o] = i; o++; } }}void SELF::display(){ int i; cout<<p<<endl; cout<<"在"<<n<<"~"<<m<<"间的自守数为:"<<endl; for(i = 0;i<p;i++) if(a[i]!=0) cout<<a[i]<<" ";}int main(){ SELF a; a.getRange(); a.getNumber(); a.display(); return 0;}void getArray(int a,int Array[]){ int i = 0; do { Array[i] = a%10; a = a/10;// cout<<Array[i]<<" "; i++; } while(a>0);}