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.求三位数的水仙花数 

// 求三位数的水仙花数
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控件。

// 求解水仙花数
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位数
            }
        }
    }
}

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wenchm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值