基本介紹
- 中文名:自冪數
- 外文名:Self power
- 說明:自冪數是指一個 n 位數
- n為1時:自冪數稱為獨身數
簡介
實現代碼
#include <stdlib.h>
{
int n,i,a,b,c1,c2,c3,c4,c5,c6,c7;
scanf("%d",&n);
a=pow(10,n-1);
b=pow(10,n);
if(n==3)
for(i=a; i<b; i++)
{
c1=i%10;
c2=i/10%10;
c3=i/100;
if(i==pow(c1,3)+pow(c2,3)+pow(c3,3))
printf("%d ",i);
}
else if(n==4)
for(i=a; i<b; i++)
{
c1=i%10;
c2=i/10%10;
c3=i/100%10;
c4=i/1000;
if(i==c1*c1*c1*c1+c2*c2*c2*c2+c3*c3*c3*c3+c4*c4*c4*c4)
printf("%d ",i);
}
else if(n==5)
for(i=a; i<b; i++)
{
c1=i%10;
c2=i/10%10;
c3=i/100%10;
c4=i/1000%10;
c5=i/10000;
if(i==c1*c1*c1*c1*c1+c2*c2*c2*c2*c2+c3*c3*c3*c3*c3+c4*c4*c4*c4*c4+c5*c5*c5*c5*c5)
printf("%d ",i);
}
else if(n==6)
for(i=a; i<b; i++)
{
c1=i%10;
c2=i/10%10;
c3=i/100%10;
c4=i/1000%10;
c5=i/10000%10;
c6=i/100000;
if(i==c1*c1*c1*c1*c1*c1+c2*c2*c2*c2*c2*c2+c3*c3*c3*c3*c3*c3+c4*c4*c4*c4*c4*c4+c5*c5*c5*c5*c5*c5+c6*c6*c6*c6*c6*c6)
printf("%d ",i);
}
else if(n==7)
for(i=a; i<b; i++)
{
c1=i%10;
c2=i/10%10;
c3=i/100%10;
c4=i/1000%10;
c5=i/10000%10;
c6=i/100000%10;
c7=i/1000000;
if(i==c1*c1*c1*c1*c1*c1*c1+c2*c2*c2*c2*c2*c2*c2+c3*c3*c3*c3*c3*c3*c3+c4*c4*c4*c4*c4*c4*c4+c5*c5*c5*c5*c5*c5*c5+c6*c6*c6*c6*c6*c6*c6+c7*c7*c7*c7*c7*c7*c7)
printf("%d ",i);
}
else printf("Error!");
return 0;
}
##include<iostream>#include<cmath>using namespace std;int main(){ int n; int start, end; int i; int m; int digit; int sum; cout << "計算自冪數,請輸入位數:"; cin >> n; while (n >= 1) { start = pow(10, n - 1); end = pow(10, n) - 1; cout << n << "位自冪數:" ; for (i = start; i <= end; i++) { m = i; sum = 0; while (m != 0) { digit = m % 10; sum = sum + pow(digit, n); m = m / 10; } if (i == sum) cout << i << " "; } cout << endl; cout << "求n位自冪數,請輸入位數:"; cin >> n; } cout << endl; return 0;} java參考代碼public class Exp2 { public static void main(String args[]) { Math1 m = new Math1(); int x=99999999;//遍歷10~99999999中的自冪數 int k=0; for (int i = 10; i <= x; i++){ if(i%(x/50)==0){ //列印遍歷完成進度。 k=k+2; System.out.print(k+"% "); } if (m.mishu(i) == true) System.out.println(i); } }}class Math1 { public boolean mishu(int x) { int k = 0; int[] s = chaifen(x); for (int i = 0; i < s.length; i++) { k=k+pingfang(s[i],s.length); } if(x==k) return true; else return false; } public int pingfang(int x, int y) { x = (int) Math.pow(x, y); // System.out.println(x); return x; } public int[] chaifen(int x) { int l = String.valueOf(x).length(); int[] s = new int[l]; for (int i = 0; i < l; i++) { // System.out.println("--------"+ (pingfang(10,(l-i-1)))); s[i] = x / (pingfang(10, (l - i - 1))) % 10; } return s; }}輸出結果:15337037140716348208947454748927279308454883417417252% 4% 42108186% 8% 9800817992631510% 12% 14% 16% 18% 20% 22% 24% 246780502467805126% 28% 30% 32% 34% 36% 38% 40% 42% 44% 46% 48% 50% 52% 54% 56% 58% 60% 62% 64% 66% 68% 70% 72% 74% 76% 78% 80% 82% 84% 86% 88% 8859347790% 92% 94% 96% 98% 100%