C#编程基础 实验(1)

本文介绍了C#编程的基础实验,包括数列求第30位数的算法,判断闰年的条件,计算歌手平均分的逻辑,以及检测字符串是否为回文的实现。通过具体的代码示例展示了C#在解决实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.一数列的规则如下:1、1、2、3、5、8、13、21、34、······求第30位数是多少?

代码如下:

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

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[30];
            a[0] = 1;
            a[1] = 1;
            for (int i = 2; i < 30; i++)
            {
                a[i] = a[i - 1] + a[i - 2];
            }
            Console.WriteLine("第三十个数是{0}", a[29]);
            Console.ReadKey();
        }
    }
}


运行结果:

 

 

2.输入一个年份,判断是否闰年(被4整除,且不被100整除,或者被400整除)

代码如下:

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

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int year;
            Console.Write("请输入一个年份:");
            year = Convert.ToInt32(Console.ReadLine());
            if ((year % 4 == 0 && year % 100 != 0 )|| year % 400 == 0)
                Console.WriteLine(year+"是闰年");
            else
                Console.WriteLine(year+"不是闰年");
            Console.ReadKey();
        }
    }
}


运行结果:

 

3.一青年歌手参加比赛,有10位评委打分(分值只能为正整形数字),计算并输出歌手的平均分(去掉一个最高分和一个最低分)。平均分以double数据类型输出。

代码如下:

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

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum=0,max=0,min=100;
            string  []a= Console.ReadLine().Split(' ');;
            int []b=new int[10];
            for (int i=0;i<a.Length;i++)
            {
                b[i] = int.Parse(a[i]);
                sum += b[i];
                if (max<b[i])
                {
                    max=b[i];
                }
                if (min>b[i])
                {
                    min=b[i];
                }
            }
            double avg=(double)(sum-max-min)/8;
            Console.WriteLine("平均分是{0}", 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  a= Console.ReadLine();
            char[] b = a.ToCharArray();
            int len=a.Length;
            int n = 0;
            for (int i = 0; i <= len / 2; i++)
            {
                if (b[i] != b[len - 1 - i])
                {
                    Console.WriteLine("字符串"+a+"不是回文");
                    n = 1;
                    break;
                }
            }
           if (n==0)
               Console.WriteLine("字符串" + a + "是回文");
            Console.ReadKey();
        }
    }
}


运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值