1、编写一控制台应用程序,定义变量“int a=3,b=4,c=5”,并求表达式(++a-1)&b+c/2的值。
using System;
namespace Test2_1
{
class Program
{
static void Main(string[] args)
{
int a = 3, b = 4, c = 5;
Console.WriteLine("{0}", (++a - 1) & b + c / 2 );
Console.ReadLine();
}
}
}
2、编写一控制台应用程序,声明一个学生结构类型Stud,包含学号、姓名和出生日期成员,定义Stud结构的两个学生变量s1和s2并赋值,求他们出生在星期几以及他们出生相差的天数。
using System;
namespace Test2_2
{
enum Week { 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六 };
class Program
{
struct Stud
{
public int sno; //学号
public string name; //姓名
public DateTime birthday;//出生日期
}
static void Main(string[] args)
{
Stud s1, s2;
s1.sno = 101; s1.name = "李明";
s1.birthday = new DateTime(1998, 8, 14);
s2.sno = 108; s2.name = "王华";
s2.birthday = new DateTime(1998, 9, 15);
int i = (int)s1.birthday.DayOfWeek;
int j = (int)s2.birthday.DayOfWeek;
Console.WriteLine("学号:{0},姓名:{1},出生日期:{2}", s1.sno, s1.name, s1.birthday);
Console.WriteLine("学号:{0},姓名:{1},出生日期:{2}", s2.sno, s2.name, s2.birthday);
Console.WriteLine("{0}出生在{1}", s1.name, (Week)i);
Console.WriteLine("{0}出生在{1}", s2.name, (Week)j);
Console.WriteLine("{0}和{1}相差{2}天", s1.name, s2.name, (s2.birthday - s1.birthday).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;
namespace Test2_3
{
class Program
{
static void Main(string[] args)
{
int n,i,j, s = 0;
Console.WriteLine("请输入一个正整数:");
n = int.Parse(Console.ReadLine());
for (i = 1; i <= n; i++)
for (j = 1; j <= i;j++ )
{
s += j;
}
Console.WriteLine("s={0}", s);
}
}
}
4、编写一控制台应用程序,输出所有满足下面条件的三位数:三位数本身等于其每位数字的立方和。
using System;
namespace Test2_4
{
class Program
{
static void Main(string[] args)
{
int i, n, a, b, c;
for (i = 100; i <= 999; i++)
{
n = i;
a = n / 100;
b = (n - a * 100) / 10;
c = n % 10;
if (n == a * a * a + b * b * b + c * c * c)
{
Console.WriteLine("{0}", n);
}
}
}
}
}
5、编写一控制台应用程序,用一个二维数组存放5个考生4门功课的考试成绩,求每位考生的平均成绩。
using System;
namespace Test2_5
{
class Program
{
static void Main(string[] args)
{
int[,] score = new int[5,4]{{4,3,5,2},{1,4,5,6},{4,5,6,4},{5,6,5,5},{4,6,5,7}};
int i, j, sum = 0;
double avg;
for (i = 0; i < 5; i++)
{
sum = 0;
for (j = 0; j < 4; j++)
{
sum += score[i, j];
}
avg = sum / 4.0;
Console.WriteLine("{0}", avg);
}
Console.ReadLine();
}
}
}
6、编写一控制台应用程序,用两个一维数组分别存放5个学生的学号和姓名,分别按学号和姓名进行排序,并输出排序后的结果。
using System;
using System.Collections.Generic;
using System.Text;
namespace Test2_6
{
class Program
{
static void Main(string[] args)
{
int[] sNo = new int[5] { 108, 102, 105, 100, 106 };
string[] sName = new string[5] { "Tom", "lucy", "Amy", "kangkang", "Mary" };
int i;
//按学号排序
Array.Sort(sNo, sName);
Console.WriteLine("按学号排序:");
for (i = 0; i < 5; i++)
{
Console.WriteLine("{0},{1}", sNo[i], sName[i]);
}
Console.WriteLine("\n");
//按姓名排序
Array.Sort(sName, sNo);
Console.WriteLine("按姓名排序:");
for (i = 0; i < 5; i++)
{
Console.WriteLine("{0},{1}", sName[i], sNo[i]);
}
Console.ReadLine();
}
}
}