[C#] 找出n位整数中每一位数的n次幂之和与原数相同的数。n=3时就是水仙花数 →→→→→进入此内容的聊天室

来自 , 2019-04-17, 写在 C#, 查看 120 次.
URL http://www.code666.cn/view/de594ef5
  1. using System;
  2.  
  3. namespace ConsoleApplication1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.Write("输入整数幂的次数: n = ");
  10.             int n = int.Parse(Console.ReadLine());
  11.             MyApp(n);
  12.             Console.ReadLine();
  13.         }
  14.         /// <summary>
  15.         /// n位整数的每一位数字的n次幂之和与原数相等
  16.         /// </summary>
  17.         /// <param name="n">整数的位数(2<n<9)</param>
  18.         public static void MyApp(int n)
  19.         {
  20.             int i,l = 1, m = 10;
  21.             for(i=0;i<n-1;i++)
  22.             {
  23.                 l *= 10;
  24.                 m *= 10;
  25.             }                      
  26.             int a, b, c, d, j;
  27.             for(i=l;i<m;i++)
  28.             {
  29.                 a = i;
  30.                 d = 0;
  31.                 while (a > 0)
  32.                 {
  33.                     b = a % 10;
  34.                     j = n;
  35.                     c = 1;                    
  36.                     while (j-- > 0)
  37.                         c *= b;
  38.                     d += c;
  39.                     a /= 10;
  40.                 }
  41.                 if (d == i)
  42.                     Console.WriteLine("{0,12}", i);
  43.             }
  44.         }
  45.     }
  46. }
  47.  

回复 "找出n位整数中每一位数的n次幂之和与原数相同的数。n=3时就是水仙花数"

这儿你可以回复上面这条便签

captcha