c#基础编程题第五题:求出1-n之间的所有水仙花数。

输入一个整数n,求出1-n之间的所有水仙花数

输入描述

                      输入一个正整数n0.

输出描述

                      1-n之间的所有水仙花数

样例输入

          1000

样例输出

       153    370   371   407

执行代码如下所示:

using System;

namespace 第五题
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            Console.Write("请输入数字:\t");
            n = Convert.ToInt32(Console.ReadLine());
            IsNarcissistic(n);
            printN(n);

        }
        public static bool IsNarcissistic( int n)
        {
            int a, b = 0, c, sum = 0, d;
            a = n;
            d = n;
            while (a != 0)
            {
                a /= 10;
                b++;
            }
            while (n != 0)
            {
                c = n % 10;
                sum += (int)Math.Pow(c, b);
                n /= 10;
            }
            if (sum == d) return true;
            else return false;
        }
        public static void printN( int n)
        {
            for (int i = 10; i < n; i++)
            {
                if (IsNarcissistic(i))
                    Console.WriteLine(i);
            }

        }
    }
}

调试结果如图所示:

在这里插入图片描述
我的解题思路:
      编写两个函数,一个用于判断一个整数是否水仙花数,另一个按从小到大的顺序打印出从一到n内所有的水仙花数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值