C#求水仙花数

目录

1.何谓水仙花数

2.求三位数的水仙花数

3.在遍历中使用Math.DivRem方法再求水仙花数


1.何谓水仙花数

水仙花数(Narcissistic number)是指一个 n 位正整数,它的每个位上的数字的 n 次幂之和等于它本身。例如,153 是一个 3 位数,它的每个位上的数字的 3 次幂之和为 1^3+5^3+3^3=153,因此 153 是一个水仙花数。

水仙花数得名于希腊神话中的美少年纳西索斯(Narcissus),他因为爱上自己的倒影而化为水仙花。在数学中,水仙花数是一种具有特定性质的数字,这种性质类似于纳西索斯对自身的迷恋。

水仙花数在数学上并不常见,但在一些数学问题和谜题中可能会出现。例如,有人可能会要求找出所有三位数的水仙花数,或者找出所有 n 位数的水仙花数。这些问题可以通过编程和数学方法解决,但通常需要一定的计算和推理能力。

2.求三位数的水仙花数

cs 复制代码
// 求三位数的水仙花数
namespace _148
{
    class NarcissisticNumber
    {
        static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            for (int i = 100; i < 1000; i++)
            {
                if (IsNarcissistic(i))
                {
                    Console.WriteLine(i);
                }
            }
        }

        /// <summary>
        /// 这个水仙花数算法的实现很巧妙
        /// </summary>
        static bool IsNarcissistic(int number)
        {
            int sum = 0;
            int temp = number;
            while (temp > 0) 
            {
                int digit = temp % 10;
                sum += digit * digit * digit;
                temp /= 10;
            }
            return sum == number;
        }
    }
}
//运行结果:
/*
153
370
371
407

*/

这段代码首先使用一个循环遍历 100 到 999 之间的所有三位数。对于每个数字,它调用 IsNarcissistic 函数来检查该数字是否为水仙花数。如果是,它将该数字输出到控制台。IsNarcissistic 函数计算给定数字的每个位上的数字的三次幂的和,以检查它是否为水仙花数。如果求和结果等于原始数字,则该数字是水仙花数,函数返回 true;否则,它返回 false。

3.在遍历中使用Math.DivRem方法再求水仙花数

换个思维,在遍历中使用Math.DivRem方法再求水仙花数,然后,再把找到的水仙花数输出到ListBox控件。

cs 复制代码
// 求解水仙花数
namespace _148_1
{
    public partial class Form1 : Form
    {
        private Label? label1;
        private ListBox? listBox1;
        private Button? button1;

        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Load += Form1_Load;
        }

        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                ForeColor = SystemColors.ActiveCaptionText,
                Location = new Point(12, 9),
                Name = "label1",
                Size = new Size(43, 17),
                TabIndex = 0,
                Text = "水仙花数的算法是一个三位数,每一位数的立方相" + "\r" + "加等于该数本身。",
        };
            // 
            // listBox1
            // 
            listBox1 = new ListBox
            {
                FormattingEnabled = true,
                ItemHeight = 17,
                Location = new Point(12, 43),
                Name = "listBox1",
                Size = new Size(270, 72),
                TabIndex = 1
            };
            // 
            // button1
            // 
            button1 = new Button
            {
                ForeColor = SystemColors.ActiveCaptionText,
                Location = new Point(207, 121),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 2,
                Text = "计算",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(294, 156);
            Controls.Add(button1);
            Controls.Add(listBox1);
            Controls.Add(label1);
            ForeColor = SystemColors.ControlLightLight;
            Name = "Form1";
            Text = "求水仙花数";
        }

        private void Button1_Click(object? sender, EventArgs e)
        {
            listBox1!.Items.Clear();
            for (int i = 100; i < 1000; i++)
            {
                int a = i / 100;
                Math.DivRem(i, 100, out int b);      //获取3位数中的后两位数
                b /= 10;                             //获取3位数中的第二位数
                Math.DivRem(i, 10, out int c);       //获取3位数中的第3位数
                a = a * a * a;                       //计算第一位数的立方
                b = b * b * b;                       //计算第二位数的立方
                c = c * c * c;                       //计算第3位数的立方
                if ((a + b + c) == i)                //如果符合水仙花数
                    listBox1.Items.Add(i.ToString());//显示当前3位数
            }
        }
    }
}
相关推荐
弱冠少年23 分钟前
websockets库使用(基于Python)
开发语言·python·numpy
长天一色24 分钟前
C语言日志类库 zlog 使用指南(第五章 配置文件)
c语言·开发语言
一般清意味……36 分钟前
快速上手C语言【上】(非常详细!!!)
c语言·开发语言
自由的dream36 分钟前
0-1背包问题
算法
卑微求AC36 分钟前
(C语言贪吃蛇)16.贪吃蛇食物位置随机(完结撒花)
linux·c语言·开发语言·嵌入式·c语言贪吃蛇
2401_8572979142 分钟前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
技术无疆1 小时前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
金灰1 小时前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5
爱上语文1 小时前
Java LeetCode每日一题
java·开发语言·leetcode
Манго нектар2 小时前
JavaScript for循环语句
开发语言·前端·javascript