c#查找水仙花数
水仙花数是指一个n(n>=3)位数,每一位数字的n次幂的和正好等于这个数本身。用c#编程查找一定范围内的水仙花数。...
水仙花数是指一个n(n>=3)位数,每一位数字的n次幂的和正好等于这个数本身。用c#编程查找一定范围内的水仙花数。
展开
展开全部
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个上限值:(小于" + long.MaxValue.ToString() + ")");
long MAX = Convert.ToInt64(Console.ReadLine());
long[] SXH = Find(MAX);
foreach (long i in SXH)
{
Console.Write(i+",");
}
Console.Write("……");
Console.ReadKey();
}
public static long[] Find(long Max)
{
long[] array1 = new long[Max];
long index=0;
for (long i = 100; i < Max; i++)
{
long[] array2 = divide(i);
long sum = 0;
for (long j = 0; j < array2.Length; j++)
{
sum += Convert.ToInt64(Math.Pow(array2[j], WeiShu(i)));
}
if (sum == i)
{
array1[index] = sum;
index++;
}
}
long[] array3 = new long[index];
for (long i = 0; i < index; i++)
{
array3[i] = array1[i];
}
return array3;
}
private static long[] divide(long sum)
{
long[] array1 = new long[7];
long index = 0;
while (sum != 0)
{
array1[index] = sum % 10;
sum /= 10;
index++;
}
long[] array2 = new long[index];
for (long i = 0; i < index; i++)
{
array2[i] = array1[i];
}
return array2;
}
private static long WeiShu(long sum)
{
long i = 0;
while (sum != 0)
{
i++;
sum /= 10;
}
return i;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个上限值:(小于" + long.MaxValue.ToString() + ")");
long MAX = Convert.ToInt64(Console.ReadLine());
long[] SXH = Find(MAX);
foreach (long i in SXH)
{
Console.Write(i+",");
}
Console.Write("……");
Console.ReadKey();
}
public static long[] Find(long Max)
{
long[] array1 = new long[Max];
long index=0;
for (long i = 100; i < Max; i++)
{
long[] array2 = divide(i);
long sum = 0;
for (long j = 0; j < array2.Length; j++)
{
sum += Convert.ToInt64(Math.Pow(array2[j], WeiShu(i)));
}
if (sum == i)
{
array1[index] = sum;
index++;
}
}
long[] array3 = new long[index];
for (long i = 0; i < index; i++)
{
array3[i] = array1[i];
}
return array3;
}
private static long[] divide(long sum)
{
long[] array1 = new long[7];
long index = 0;
while (sum != 0)
{
array1[index] = sum % 10;
sum /= 10;
index++;
}
long[] array2 = new long[index];
for (long i = 0; i < index; i++)
{
array2[i] = array1[i];
}
return array2;
}
private static long WeiShu(long sum)
{
long i = 0;
while (sum != 0)
{
i++;
sum /= 10;
}
return i;
}
}
}
展开全部
int a,b,c,n,i=0;
for(n=100;n<=999;n++)
{
a=n/100;
b=(n-a*100)/10;
c=(n-a*100-b*10);
if(n==a*a*a+b*b*b+c*c*c)
Console.WriteLine("{0}",n);
else continue;
i=i+1;
if(i%5==0)Console.WriteLine("")
}
for(n=100;n<=999;n++)
{
a=n/100;
b=(n-a*100)/10;
c=(n-a*100-b*10);
if(n==a*a*a+b*b*b+c*c*c)
Console.WriteLine("{0}",n);
else continue;
i=i+1;
if(i%5==0)Console.WriteLine("")
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
检举int a,b,c,n,i=0;
for(n=100;n<=999;n++)
{
a=n/100;
b=(n-a*100)/10;
c=(n-a*100-b*10);
if(n==a*a*a+b*b*b+c*c*c)
Console.WriteLine("{0}",n);
else continue;
i=i+1;
if(i%5==0)Console.WriteLine("")
}
for(n=100;n<=999;n++)
{
a=n/100;
b=(n-a*100)/10;
c=(n-a*100-b*10);
if(n==a*a*a+b*b*b+c*c*c)
Console.WriteLine("{0}",n);
else continue;
i=i+1;
if(i%5==0)Console.WriteLine("")
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最笨的方法我知道
先把那个数分解了 个位 十位 百位
然后按那个规律计算一下 看是否等于没分解前的
先把那个数分解了 个位 十位 百位
然后按那个规律计算一下 看是否等于没分解前的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询