C#程序设计教程编程题(一)

(1)设计一个程序,输出所有的水仙花数。所谓水仙花数,是指一个三位整数,其各位数字的立方等于该数的本身。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {

            int a,b,c;
            for (int num = 100; num <= 999; num++)
            {
                a = num / 100;
                b = num / 10 % 10;
                c = num % 10;
                if (a*a*a+b*b*b+c*c*c==num)
                    Console.WriteLine(num);
            }
               
            Console.ReadKey();
        }
    }
}


运行结果:

 

(2)判断s所指的字符串是否是“回文”

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            
            string  s= Console.ReadLine();
            char[] b = s.ToCharArray();
            int len=s.Length;
            int n = 0;
            for (int i = 0; i <= len / 2; i++)
            {
                if (b[i] != b[len - 1 - i])
                {
                    Console.WriteLine("字符串"+s+"不是回文");
                    n = 1;
                    break;
                }
            }
           if (n==0)
               Console.WriteLine("字符串" + s + "是回文");
            Console.ReadKey();
        }
    }
}


运行结果:

 

(3)设计一个程序,输入10个数存入数组中,求最大值、最小值和平均值。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            
            string[] a = Console.ReadLine().Split(' '); 
            int[] b = new int[10];
            for (int i = 0; i < a.Length; i++)
            {
                b[i] = int.Parse(a[i]);
            }
            int sum = 0, max = b[0], min = b[0];
            for (int i = 0; i < a.Length; i++)
            {
                sum += b[i];
                if (max < b[i])
                {
                    max = b[i];
                }
                if (min > b[i])
                {
                    min = b[i];
                }
            }
            double avg = (double)sum / b.Length;
            Console.WriteLine("最大值、最小值、平均分依次是{0},{1},{2}",max,min, avg);
            Console.ReadKey();
        }
    }
}



运行结果:

 

(4)编写程序,输入一个整数,将其各位数字颠倒顺序后输出。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            string s=Console.ReadLine();
            int num = Convert.ToInt32(s);
            int[] a = new int[s.Length];
            int i = 0;
            while (num!=0)
            {
                a[i++] = num % 10;
                num /= 10;
            }
            for (i = 0; i < s.Length; i++)
            {
                Console.Write(a[i]);
            }
            Console.ReadKey();
        }
    }
}


运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值