求水仙花数CSharp
时间: 2024-01-07 16:22:38 浏览: 99
以下是求水仙花数的C#代码示例:
```csharp
for (int i = 100; i <= 999; i++)
{
int bai = i / 100;
int shi = i % 100 / 10;
int ge = i % 10;
if (i == bai * bai * bai + shi * shi * shi + ge * ge * ge)
{
Console.WriteLine("100-999以内的水仙花数有{0}", i);
}
}
Console.ReadKey();
```
这段代码使用了一个for循环,从100到999遍历每个数。然后,通过除法和取余运算,将百位、十位和个位分别提取出来。接下来,使用立方运算符计算每个位上的立方值,并将它们相加。如果相加的结果等于当前这个百位数字,那么这个数就是水仙花数。最后,将水仙花数打印出来。
相关问题
使用CSharp中的枚举器实现从键盘输出的数以内所有的水仙花数
好的,这个问题很有趣。让我回答你。
以下是使用C#中的枚举器实现从键盘输出的数以内所有的水仙花数的代码:
```
using System;
using System.Collections;
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个数字:");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("以下是" + n + "以内的水仙花数:");
ArrayList arr = new ArrayList();
for (int i = 100; i < n; i++)
{
int a = i / 100;
int b = (i % 100) / 10;
int c = i % 10;
if ((a * a * a + b * b * b + c * c * c) == i)
{
arr.Add(i);
}
}
IEnumerator e = arr.GetEnumerator();
while (e.MoveNext())
{
Console.WriteLine(e.Current);
}
Console.ReadKey();
}
}
```
希望你能通过这个代码学到一些东西。下一个问题是什么?
使用CSharp中的迭代器实现从键盘输入的数以内所有的水仙花数
Sure, 迭代器是 CSharp 中提供的一种便捷的循环方式。为了实现从键盘输入的数以内所有的水仙花数,我们可以先用 Console.ReadLine() 读取输入的数。如果输入的不是数字,需要对输入类型进行校验并提示用户重新输入。一旦我们得到了输入的数,就可以开始循环判断每个数是否为水仙花数了。在循环过程中,我们需要计算当前数每一位数的次方和是否等于原数本身。如果相等,就是一个水仙花数,可以在迭代器中返回。
下面是实现代码:
```csharp
using System;
using System.Collections;
namespace NarcissisticNumbers
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个数字:");
int n;
while (!int.TryParse(Console.ReadLine(), out n))
{
Console.WriteLine("输入错误,请重新输入:");
}
foreach (int num in NarcissisticNumberGenerator(n))
{
Console.Write($"{num} ");
}
}
static IEnumerable NarcissisticNumberGenerator(int n)
{
for (int i = 1; i <= n; i++)
{
int num = i;
int sum = 0;
int digits = (int)Math.Log10(num) + 1;
while (num != 0)
{
int digit = num % 10;
sum += (int)Math.Pow(digit, digits);
num /= 10;
}
if (sum == i)
{
yield return i;
}
}
}
}
}
```
中文:以上是 CSharp 实现从键盘输入的数以内所有的水仙花数的代码,希望对你有所帮助♪(^∀^●)ノシ
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)