一道C#练习题

【题目要求】

        从控制台输入一个数字m,100<= m <= 999。依次判断m是否为水仙花数、迷人的数、完数。是则输出1,否则输出0.

【备注】

水仙花数:如果数字m是一个三位数,并且它的每个数位上的数字的3次幂之和等于它本身,m为水仙花数。例如:153 = 1*1*1 + 5*5*5 + 3*3*3,153为水仙花数。

迷人的数:如果m与2*m和3*m拼接后得到一个由1~9这九个不同的数字组成的9位数(1~9都包括且只出现一次),不包括0,则m是迷人的数。例如:192 -> '192'+'384'+'576' = 192384576,192为迷人的数。 

完数:如果数字m等于除了它本身以外所有因子的和,m为完数。例如:6 = 1+2+3,6为完数。

【输出格式举例】 

6

0 0 1

【代码】

using System;
namespace HomeWork
{
    class Program
    {
        public void CheckNum(int m)
        {
            // 判断是否是水仙花数
            int a = m % 10;
            int b = m / 100;
            int c = m / 10 - b * 10;
            if (m == a * a * a + b * b * b + c * c * c)
                Console.Write("1 ");
            else
                Console.Write("0 ");

            // 判断是否是迷人的数
            string s0 = m.ToString();
            int n = 2 * m;
            string s1 = n.ToString();
            n = 3 * m;
            string s2 = n.ToString();
            string s3 = s0 + s1 + s2;

            int n3 = int.Parse(s3);
            if (n3 >= 123456789 && n3 <= 987654321)
            {
                char[] arr = s3.ToCharArray();
                bool[] arr1 = new bool[9];

                // 初始化
                for (int i = 0; i < 9; i++)
                    arr1[i] = false;

                // 判断是否有1~9
                for (int i = 1; i <= 9; i++)
                {
                    for(int j=0; j < 9; j++)
                    {
                        int number = (int)arr[j] - (int)'0';   
                        // 这里将字符类型显示转换为整数类型
                        // arr[j]处的字符与字符'0'对应的UTF-16编码值之差被赋值给number
                        if (number == i)
                        {
                            arr1[i - 1] = true;
                        }
                    }
                }

                bool tag1 = true;
                for(int i = 1; i <= 9; i++)
                {
                    if (arr1[i - 1] == false)
                        tag1 = false;
                }
                if (tag1 == true)
                    Console.Write("1 ");
                else
                    Console.Write("0 ");
            }
            else
            {
                Console.Write("0 ");
            }

            // 判断是否是完数
            int count = 0;
            for(int i = 1; i < m; i++)
            {
                if(m % i == 0)
                {
                    count += i;
                }
            }
            if(count == m)
            {
                Console.Write("1");
            }
            else
            {
                Console.Write("0");
            }
        }

        static void Main(string[] args)
        {
            Program n = new Program();
            string s = Console.ReadLine();
            int m = int.Parse(s); // 输入一个正整数m,且100<=m<1000
            n.CheckNum(m);

            Console.ReadKey();
        }
    }
}

总结:

1.   对字符类型进行强制转换(显式转换),是把字符转换为其对应的编码值,然后进行赋值或其他操作。所以,要从字符‘1’得到数字1,需要把字符‘1’强制转换后的编码值减去'0'的编码值,来得到字符字面量。

2.   对于求得一个整数的所有因子不够熟练。整数a除以整数b (b≠0) 的商正好是整数而没有余数 ,则称b是a的因子

3.   在C#中,字符串是以Unicode编码存储的,而字符字面量(如’0’)是以UTF-16编码存储的。把一个字符转换为一个整数时,实际上是获取了该字符的UTF-16编码值。如果想获取字符的ASCII编码值,可以使用Encoding.ASCII.GetBytes方法。

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值