zoukankan      html  css  js  c++  java
  • 水仙花数<大家帮助研究下,高位数的 怎么设计>

    水仙花数是指一个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;
            }
        }
    }

    转载请注明出处,感谢。
    作者:李宏旭
    阅罢此文,如果您觉得本文不错并有所收获,请【打赏】或【推荐】,也可【评论】留下您的问题或建议与我交流。
    你的支持是我不断创作和分享的不竭动力!
  • 相关阅读:
    C#里的async和await的使用
    解决 .NET CORE3.0 MVC视图层不即时编译
    【转】CSS实现自适应分隔线的N种方法
    iscrolljs 看API 回顾以前开发中失误
    自由了-和过去说再见
    js 性能基准测试工具-告别可能、也许、大概这样更快更省
    dom事件不求甚解,色解事件捕获和冒泡
    百度mobile UI组件GMU demo学习1-结构和初始化
    自己收集原生js-2014-2-23
    如何在电脑上测试手机网站(补充)和phonegap
  • 原文地址:https://www.cnblogs.com/bjlhx/p/2081770.html
Copyright © 2011-2022 走看看