实验1 C#编程基础
【实验目的】
1.熟悉Visual Studio 2010集成开发环境。
2.掌握控制台输入/输出方法的使用。
3.掌握C#的数据类型、六大类运算符及优先级。
4.掌握数据类型的转换。
5.掌握条件语句、循环语句及跳转语句的使用。
【实验内容】
1、3421 请输出样例所示内容。
Hello, world!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("**********");
Console.WriteLine("Hello,world!");
Console.WriteLine("**********");
Console.ReadKey();
}
}
}
2、3423 判断闰年。使用C#编写一个控制台应用。输入-一个年份,判断是否润年(被4整除,且不被100整除,或者被400整除)。是闰年输出yes,不是输出no。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
int year = Convert.ToInt32(Console.ReadLine());
if ((year % 4 == 0) && (year % 100 != 0) || year % 400 == 0)
{
Console.WriteLine("yes");
}
else
{
Console.WriteLine("no");
}
Console.ReadKey();
}
}
}
3、3482 一个数列的规则如下:1、1、2、3、5、8、13、21、34…。采用递归算法求第n位数是多少?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
int n = int.Parse(s);
Console.WriteLine(jisuan(n));
Console.ReadKey();
}
static int jisuan(int n)
{
if (n < 0)
return 0;
else if (n == 1)
return 1;
else
return jisuan(n - 1) + jisuan(n - 2);
}
}
}
4、3422 一青年歌手参加比赛。使用C#编写-一个控制台应用,输入10位评委打分(分值只能为正整数),计算并输出歌手的平均分(去掉一个最高分和一个最低分)。平均分以double数据类型输出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
int[] nums = new int[10];
string[] numsStr = input.Split(' ');
for (int i = 0; i < 10; i++)
{
nums[i] = Convert.ToInt32(numsStr[i]);
}
System.Array.Sort(nums);
double sum = 0;
for (int i = 1; i < 9; i++)
{
sum += nums[i];
}
Console.Write(sum / 8);
Console.ReadKey();
}
}
}
5、3483 创建一个C#控制台应用程序,使用冒泡排序算法对一维数组的元素从小到大进行排序。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[10];
for (int i = 0; i < 10; ++i)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
sort(array);
for (int i = 0; i < 10; ++i)
{
Console.WriteLine(array[i]);
}
Console.ReadKey();
}
static void sort(int[] nums)
{
int temp = 0;
for (int i = 0; i < nums.Length - 1; ++i)
{
for (int j = 0; j < nums.Length - 1 - i; ++j)
{
if (nums[j] > nums[j + 1])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
}
}
6.水仙花数:春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的: “水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+3^3。 现在要求输出所有在m和n范围内的水仙花数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
double[] num = new double[10];
for (int i = 0; i < 10; i++)
{
num[i] = double.Parse(Console.ReadLine());
}
double max1 = max(num);
Console.WriteLine(max1);
double min1 = min(num);
Console.WriteLine(min1);
double ave1 = ave(num);
Console.Write(ave1);
Console.ReadKey();
}
static double max(double[] num)
{
double max = num[0];
for (int i = 0; i < 10; i++)
{
if (num[i] > max)
{
max = num[i];
}
}
return max;
}
static double min(double[] num)
{
double min = num[0];
for (int i = 0; i < 10; i++)
{
if (num[i] < min)
{
min = num[i];
}
}
return min;
}
static double ave(double[] num)
{
double sum = 0;
double ave = 0;
for (int i = 0; i < 10; i++)
{
sum += num[i];
}
ave = sum / 10;
return ave;
}
}
}