1.编写一控制台应用程序,定义变量“int a=3,b=4,c=5”,并求表达式(++a-1)&b+c/2的值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace anli3_3
{
class Program
{
static void Main(string[] args)
{
int a = 3, b = 4, c = 5;
Console.WriteLine((++a - 1) & b + c / 2);
}
}
}
运行结果截图:
2.编写一控制台应用程序,声明一个学生结构类型Stud,包含学号、姓名和出生日期成员,定义Stud结构的两个学生变量s1和s2并赋值,求他们出生在星期几以及他们出生相差的天数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace liti2
{
class Program
{
enum Week { 星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期日 }
struct Stud
{
public int xh; //学号
public string xm; //姓名
public DateTime Day;//出生日期
}
static void Main(string[] args)
{
Stud s1, s2;
s1.xh = 101; s1.xm = "李明";
s1.Day = new DateTime(2000, 1, 1);
s2.xh = 108; s2.xm = "王华";
s2.Day = new DateTime(2000, 1, 2);
int i = (int)s1.Day.DayOfWeek;
int j = (int)s2.Day.DayOfWeek;
Console.WriteLine("学号:{0},姓名:{1},出生日期:{2}", s1.xh, s1.xm, s1.Day);
Console.WriteLine("学号:{0},姓名:{1},出生日期:{2}", s2.xh, s2.xm, s2.Day);
Console.WriteLine("{0}出生在{1}", s1.xm, (Week)i);
Console.WriteLine("{0}出生在{1}", s2.xm, (Week)j);
Console.WriteLine("{0}和{1}相差{2}天", s1.xm, s2.xm, (s2.Day - s1.Day).Days);
Console.ReadLine();
}
}
}
运行结果截图:
3.编写一控制台应用程序,输入正整数n,计算s=1+(1+2)+(1+2+3)+…+(1+2+…+n)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace shiyan2_3
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个大于或等于1的整数:n=");
var s = Console.ReadLine();
int n;
if (int.TryParse(s, out n))
{
Console.WriteLine(string.Format("计算结果:s={0}", calculate(n)));
}
else
{
Console.WriteLine("您的输入有误!");
}
Console.Read();
}
public static int calculate(int n)
{
int sum = 0, all = 0;
for (int i = 1; i <= n; i++)
{
sum += i; all += sum;
}
return all;
}
}
}
运行结果截图:
4.编写一控制台应用程序,输出所有满足下面条件的三位数:三位数本身等于其每位数字的立方和。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace shiyan2_4
{
class Program
{
static void Main(string[] args)
{
int n, a, b, c;
Console.WriteLine("The number satisfied condition:\n");
for (n = 100; n < 1000; n++)
{
a = n / 100; /*分别取出三位数的百位,十位和各位*/
b = (n / 10) % 10;
c = n % 10;
if (n ==a*a*a + b*b*b +c*c*c) /*判断符合条件,输出结果*/
{
Console.WriteLine("{0} ", n);
}
}
}
}
}
5.编写一控制台应用程序,用一个二维数组存放5个考生4门功课的考试成绩,求每位考生的平均成绩。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace shiyan2_5
{
class Program
{
static void Main(string[] args)
{
//初始化二维数组,存放五位学生的四门成绩
double[,] scoreArrays = new double[5,4]{{89,65,96,72},{62,94,68,76},{89,58,63,74},{98,95,84,75},{97,65,80,91}};
//声明一维数组,用于存放平均数
double[] averageScore = new double[5];
//求平均值
for (int i = 0; i < 5; i++)
{
double sumScore = 0.0;
for (int j = 0; j < 4; j++)
{
sumScore += scoreArrays[i, j];
}
averageScore[i] = sumScore / 4.0;
}
//打印结果
foreach (double average in averageScore)
{
Console.Write("{0:N2} ", average);
}
Console.ReadLine();
}
}
}
运行结果截图:
6.编写一控制台应用程序,用两个一维数组分别存放5个学生的学号和姓名,分别按学号和姓名进行排序,并输出排序后的结果。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace shiyan2_6
{
class Program
{
static void Main(string[] args)
{
int[] sNo = new int[5] {50, 30, 100, 159,99};
string[] sName = new string[5]{"ybj1","ybj2", "ybj3", "ybj4", "ybj5"};
int i;
Array.Sort(sNo, sName);
for (i=0; i < 5;i++ )
{
Console.WriteLine("{0}, {1}",sNo[i],sName[i]);
}
Array.Sort (sName, sNo) ;
for(i=0;i<5;i++)
{
Console.WriteLine("{0}, {1}",sName[i],sNo[i]);
}
Console.ReadLine();
}
}
}