C#写水仙花数--用到递归
来源:互联网 发布:如何在excel筛选数据 编辑:程序博客网 时间:2024/05/21 13:09
</pre><pre name="code" class="csharp">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace NarcissisticNumber{ class NarNumber//建立水仙数的类,在Main中调用此类 { public void NarcisNumber() { Console.WriteLine("*****************输出100~999的水仙花数***************"); Console .Write("\n\t100~999之间的水仙花数为:"); for (int i = 100; i <= 999; i++) { if (judge(i)) { Console.Write("{0} ",i); } } Console.WriteLine("\n\n****************************************************\n\n"); } public void Narcinumber()//判断四位数是否为水仙数的函数 { Console.WriteLine("*****************输出1000~9999的水仙花数************"); Console.Write("\n\t1000~9999的水仙花数为:"); for (int i = 1000; i <= 9999; i++) { if (Judge(i)) { Console.Write("{0} ", i); } } Console.WriteLine("\n\n****************************************************\n\n"); } public bool judge(int num) //判断三位水仙花数的语句 { int a, b, c; a = num / 100; b = (num - a * 100) / 10; c = num % 10; if (num == Recurison(a,3) + Recurison(b,3) + Recurison(c,3)) { return true; } else { return false; } } public bool Judge(int num) //判断四位数为水仙数的语句 { int a, b, c, d; a = num / 1000; b = num % 1000 / 100; c = num % 100 / 10; d = num % 10; if (num == Recurison(a,4) + Recurison(b,4) + Recurison(c,4) + Recurison(d,4)) { return true; } else { return false; } } public int Recurison(int num, int n) //利用递归计算 a^4 { if (n == 1) { return num; } else { return num * Recurison(num,n-1); } } } class Program { static void Main(string[] args) { NarNumber a = new NarNumber();//构造新的NarNumber函数 //判断三位数的调用的函数类 a.NarcisNumber(); //判断四位数的调用的函数类 a.Narcinumber(); Console.ReadKey();//防止程序闪退 } }}
</pre><pre name="code" class="csharp">在其中用到递归计算次方,例如:a^3,对自己来说回顾了一下新的算法!
</pre><pre name="code" class="csharp">首先建立了一个名为NarNumber的类,在主函数Main中通过 new 重新构造函数 NarNumber a = new NarNumber();
//构造新的NarNumber函数
并通过 a 来调用NarNumber中的类
0 0
- C#写水仙花数--用到递归
- c# 之 水仙花数
- C# 输出水仙花数
- C#水仙花数
- C#求水仙花数!
- C# 水仙花数
- 0611 js写水仙花数
- C#趣味程序---水仙花数
- 水仙花数和递归调用举例
- Algorithm JAVA写算法 水仙花数
- C#程序设计(一)---水仙花数
- c# { 1 } 1000以内的水仙花数
- c# 水仙花数 两种方法
- C#求三位数的水仙花数
- C#打印水仙花数(3位数)
- 写一个函数参数是一个int数值要求倒着输出这个数,要求用到递归算法。
- 水仙花数
- 水仙花数
- Java编程算法基础---小数循环节
- poj 1204 Word Puzzles(字典树)
- 用户定位(gps,network)
- C++:sprintf()的用法
- C++中引用(&)的用法和应用实例(转自博客园文章)
- C#写水仙花数--用到递归
- 栈和队列
- 科技人才应具备的8个“杀手锏”级软技能
- 2014 Asia AnShan Regional Contest 题解
- IOS下点击按钮后退到上一个界面
- 【掌沃WiFi商盟】如何创建砸金蛋活动
- Java程序如何生成Jar、exe及安装文件
- POJ 3692 Kindergarten(最大独立集)
- 绘制球赛贝尔图