文档库 最新最全的文档下载
当前位置:文档库 › 求解自幂数c++实现

求解自幂数c++实现

#include
#include
#include
using namespace std;

clock_t start__;
#define tic start__=clock()
#define toc cout<<(clock()-start__)*1000/CLOCKS_PER_SEC<<"ms\n"

int main()
{
void zms(int);
int x;
while(cout<<"请输入要计算几位自幂数:",cin>>x)
{
if(x<1||x>10)
{
cout << "输入非法,请重输\n";
continue;
}
zms(x);
cout << endl;
}
return 0;
}

void zms(int x)
{// 输入参数值只能为[1,10]的整数
int c = 0; // 统计解的个数
int i,j,k,l,m,n,o,p,q,r;// 各个位的数字枚举变量
__int64 t,a[10][10]; //10位自幂数需要用int64,与int差别是9位自幂数计算5秒变8秒

tic;
switch(x)
{
case 1: cout << "一位自幂数(独身数):" << endl; break;
case 2: cout << "二位自幂数(无):" << endl; break;
case 3: cout << "三位自幂数(水仙花数):" << endl; break;
case 4: cout << "四位自幂数(四叶玫瑰数):" << endl; break;
case 5: cout << "五位自幂数(五角星数):" << endl; break;
case 6: cout << "六位自幂数(六合数):" << endl; break;
case 7: cout << "七位自幂数(北斗七星数):" << endl; break;
case 8: cout << "八位自幂数(八仙数):" << endl; break;
case 9: cout << "九位自幂数(九九重阳数):" << endl; break;
case 10: cout << "十位自幂数(十全十美数):" << endl; break;
}

for(i=0;ia[i][j] = j*pow(10,x-i-1) - pow(j,x);

switch(x)
{
case 1:
printf("%2d: 0\n",++c);
for(i=1;i<10;i++) { t = a[0][i];
if(!t) printf("%2d: ",++c),cout<} break;
case 2:
for(i=1;i<10;i++) { t = a[0][i];
for(j=0;j<10;j++) { t += a[1][j];
if(!t) printf("%2d: ",++c),cout<t -= a[1][j]; }} break;
case 3:
for(i=1;i<10;i++) { t = a[0][i];
for(j=0;j<10;j++) { t += a[1][j]; // if(t<0){t-=a[1][j];continue;}
for(k=0;k<10;k++) { t += a[2][k];
if(!t) printf("%2d: ",++c),cout<t -= a[2][k]; }
t -= a[1][j]; }} break;
case 4:
for(i=1;i<10;i++) { t = a[0][i];
for(j=0;j<10;j++) { t += a[1][j];
for(k=0;k<10;k++) { t += a[2][k]; if(t<0){t-=a[2][k];continue;}
for(l=0;l<10;l++) { t += a[3][l];
if(!t) printf("%2d: ",++c),cout<t -= a[3][l]; }
t -= a[2][k]; }
t -= a[1][j]; }} break;
case 5:
for(i=1;i<10;i++) { t = a[0][i];
for(j=0;j<10;j++) { t += a[1][j];
for(k=0;k<10;k++) { t += a[2][k];
for(l=0;l<

10;l++) { t += a[3][l]; if(t<0){t-=a[3][l];continue;}
for(m=0;m<10;m++) { t += a[4][m];
if(!t) printf("%2d: ",++c),cout<t -= a[4][m]; }
t -= a[3][l]; }
t -= a[2][k]; }
t -= a[1][j]; }} break;
case 6:
for(i=1;i<10;i++) { t = a[0][i];
for(j=0;j<10;j++) { t += a[1][j];
for(k=0;k<10;k++) { t += a[2][k];
for(l=0;l<10;l++) { t += a[3][l];
for(m=0;m<10;m++) { t += a[4][m]; if(t<0){t-=a[4][m];continue;}
for(n=0;n<10;n++) { t += a[5][n];
if(!t) printf("%2d: ",++c),cout<t -= a[5][n]; }
t -= a[4][m]; }
t -= a[3][l]; }
t -= a[2][k]; }
t -= a[1][j]; }} break;
case 7:
for(i=1;i<10;i++) { t = a[0][i];
for(j=0;j<10;j++) { t += a[1][j];
for(k=0;k<10;k++) { t += a[2][k];
for(l=0;l<10;l++) { t += a[3][l];
for(m=0;m<10;m++) { t += a[4][m];
for(n=0;n<10;n++) { t += a[5][n]; if(t<0){t-=a[5][n];continue;}
for(o=0;o<10;o++) { t += a[6][o];
if(!t) printf("%2d: ",++c),cout<t -= a[6][o]; }
t -= a[5][n]; }
t -= a[4][m]; }
t -= a[3][l]; }
t -= a[2][k]; }
t -= a[1][j]; }} break;
case 8:
for(i=1;i<10;i++) { t = a[0][i];
for(j=0;j<10;j++) { t += a[1][j];
for(k=0;k<10;k++) { t += a[2][k];
for(l=0;l<10;l++) { t += a[3][l];
for(m=0;m<10;m++) { t += a[4][m];
for(n=0;n<10;n++) { t += a[5][n];
for(o=0;o<10;o++) { t += a[6][o]; if(t<0){t-=a[6][o];continue;} // 这里加if,800ms+变500ms+
for(p=0;p<10;p++) { t += a[7][p];
if(!t) printf("%2d: ",++c),cout<t -= a[7][p]; }
t -= a[6][o]; }
t -= a[5][n]; }
t -= a[4][m]; }
t -= a[3][l]; }
t -= a[2][k]; }
t -= a[1][j]; }} break;
case 9:
for(i=1;i<10;i++) { t = a[0][i];
for(j=0;j<10;j++) { t += a[1][j];
for(k=0;k<10;k++) { t += a[2][k];
for(l=0;l<10;l++) { t += a[3][l];
for(m=0;m<10;m++) { t += a[4][m];
for(n=0;n<10;n++) { t += a[5][n];
for(o=0;o<10;o++) { t += a[6][o];
for(p=0;p<10;p++) { t += a[7][p]; if(t<0){t-=a[7][p];continue;} // 这里加if,8秒变5秒
for(q=0;q<10;q++) { t += a[8][q];
if(!t) printf("%2d: ",++c),cout<t -= a[8][q]; }
t -= a[7][p]; }

t -= a[6][o]; }
t -= a[5][n]; }
t -= a[4][m]; }
t -= a[3][l]; }
t -= a[2][k]; }
t -= a[1][j]; }} break;
case 10:
for(i=4;i<5;i++) { t = a[0][i];
for(j=0;j<10;j++) { t += a[1][j];
for(k=0;k<10;k++) { t += a[2][k];
for(l=0;l<10;l++) { t += a[3][l];
for(m=0;m<10;m++) { t += a[4][m];
for(n=0;n<10;n++) { t += a[5][n];
for(o=0;o<10;o++) { t += a[6][o];
for(p=0;p<10;p++) { t += a[7][p];
for(q=0;q<10;q++) { t += a[8][q]; if(t<0){t-=a[8][q];continue;} // 我擦,这里加个负值判断,80秒变成6秒!
for(r=0;r<10;r++) { t += a[9][r];
if(!t) printf("%2d: ",++c),cout<t -= a[9][r]; }
t -= a[8][q]; }
t -= a[7][p]; }
t -= a[6][o]; }
t -= a[5][n]; }
t -= a[4][m]; }
t -= a[3][l]; }
t -= a[2][k]; }
t -= a[1][j]; }} break;
}
cout<<"求解完毕!共用时"; toc;
}

相关文档
相关文档 最新文档