您的位置:首页 > 编程语言 > C#

C#编程基础 实验(1)

2016-03-11 17:04 495 查看
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();
}
}
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 编程