C#控制结构


1.输出100-999之间的水仙花数。水仙花数是指其各位数字立方和等于该数本身的数,如153=1^3+5^3+3^3。

代码如下:

<span style="font-family:SimSun;font-size:18px;">using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace 水?仙¨¦花¡§数ºy
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c, x,s;
            for (x = 100; x <= 999; x++)
            {
                a = x / 100;
                b = x / 10 % 10;
                c = x % 10;
                s = a * a * a + b * b * b + c * c * c;
                if (s == x)
                {
                    Console.WriteLine(x);
                }
            }
            Console.ReadLine();
        }
    }
}</span>

运行结果:


 




2.找出1000以内的完全数。完全数:该数的因子(不包含本身)之和等于其本身。如28是完全数,因为28=1+2+4+7+14

代码如下:

<span style="font-family:SimSun;font-size:18px;">using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Wanshu
{
    class Program
    {
        static void Main(string[] args)
        {
            int s = 0;
            int a = 0;
            int i, j;
            Console.WriteLine("1000以°?内¨²的Ì?完ª¨º数ºy为a:êo");
            for (i = 1; i <= 1000; i++)
            {
                s = 0;
                for (j = 1; j <= i/2; j++)
                {
                    if (i % j == 0 && i != j)
                    {
                        s += j;
                    }
                }
                if (s == i)
                {
                    Console.WriteLine("{0}\t", i);
                }
                
            }
            Console.ReadLine();
        }
    }
}</span>

运行结果:






3.找出2~999之间所有的同构数。所谓同构数是指该数出现在它的平方数的右边。如5、6、25等。


代码入下:

<span style="font-family:SimSun;font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 同ª?构1数ºy
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, i,k = 10;
            for (i = 2; i <= 999; i++)
            {
                x = i * i;
                if (i == k)
                {
                    k = k * 10;

                }
                if (x % k == i)
                {
                    Console.WriteLine("\t"+i);
                }
            }
            Console.ReadLine();
        }
    }
}</span>

运行结果:






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值