水仙花数<大家帮助研究下,高位数的 怎么设计>
2011-06-15 20:17
302 查看
水仙花数是指一个n(n>=3)位数,每一位数字的n次幂的和正好等于这个数本身。用c#编程查找一定范围内的水仙花数
using System;
using System.Collections.Generic;
using System.Text;
namespace ShuiXianHuaShu{
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;
using System.Collections.Generic;
using System.Text;
namespace ShuiXianHuaShu{
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;
}
}
}
相关文章推荐
- 从一段代码说开,谈学习<<C++ Propertiest>>之所得,帮助大家学习开发c++代码框架
- 概说概要设计怎么做<转>
- list<Map>集合的研究
- 分享一下我记忆23种设计模式的方法 <转。。>
- 记录我开发鞋服行业供应链软件的历程<设计代码生成工具>
- 学习笔记——JAVA设计模式<3>建造者模式
- 课程设计<3>---电子词典
- 出现“entry index is larger than available symbols” <index ,total symbols> 的研究
- xml字符串中的变成了&lt;和&gt;,这怎么办呢
- <C++ 实现设计模式> 观察者模式
- 怎么分别javascript写在<head>里还是<body>里面?
- .net集合类的研究-哈希表(一)--Hashtable,Dictionary<TKey,TValue>
- 怎么设在html中无序列表<li>之间的距离
- 浅学设计模式之观察者<Observer>模式及在android中的应用
- 怎么给从数据库得到list<>追加一条记录(下拉框添加全部)
- 10.3 设计一个类模板 store <T>用于存储某一类型的数据,并以整数和字符串进行实 例化
- Kafka组件研究<二>----Kafka部署
- GL音乐播放器<二>----界面设计
- php+js+mysql设计的仿webQQ-<3>月份天数随月份改变
- 浅学设计模式之迭代器<Iterator>模式