using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace student_number
{
class Program
{
static void Main(string[] args)
{
int[] height = new int[10] {156,150,167,178,180,176,173,154,155,158};
int i, sumHeight = 0, avgHeight, maxHeight = 0, minHeight = 500, overAvg = 0;
Console.WriteLine("学生身高如下:");
//显示数组内容
for (i = 0; i < 10; i++)
{
Console.Write("{0,4}",height[i]);
}
//求身高总和,最大值,最小值
for (i = 0; i < 10; i++)
{
sumHeight += height[i];
if (height[i] > maxHeight) maxHeight = height[i];
if (height[i] < minHeight) minHeight = height[i];
}
avgHeight = sumHeight / 10;
for (i = 0; i < 10; i++)
{
if (height[i] > avgHeight) overAvg++;
}
Console.WriteLine("\n平均身高={0},最高身高={1},最低身高={2}", avgHeight, maxHeight, minHeight);
Console.WriteLine("高于平均身高的学生人数={0}", overAvg);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace student_score
{
class Program
{
static void Main(string[] args)
{
int[] score = new int[10] {80,90,67,89,78,85,45,69,77,95};
int i, ANum = 0, BNum = 0, CNum = 0, DNum = 0;
Console.WriteLine("学生成绩如下:");
for (i = 0; i < 10; i++) Console.Write("{0,4}",score[i]);
//求成绩总和
for (i = 0; i < 10; i++)
{
switch (score[i] / 10)
{
case 10:
case 9:
ANum++;
break;
case 8:
BNum++;
break;
case 7:
case 6:
CNum++;
break;
default:
DNum++;
break;
}
}
Console.WriteLine("\n优分数段(90~100)的学生人数是={0},所占百分比={1:#0.##%}",ANum,ANum/10.0);
Console.WriteLine("良分数段(80~90)的学生人数是={0},所占百分比={1:#0.##%}", BNum, BNum / 10.0);
Console.WriteLine("中分数段(70~80)的学生人数是={0},所占百分比={1:#0.##%}", CNum, CNum / 10.0);
Console.WriteLine("差分数段(60~70)的学生人数是={0},所占百分比={1:#0.##%}", DNum, DNum / 10.0);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 冒泡排序
{
class Program
{
public static void DisplayMatrix(int[] A)
{
//打印矩阵
foreach(int i in A) Console.Write("{0,4}",i);
Console.WriteLine();
}
static void Main(string[] args)
{
int i, t;
int[] A = new int[10];
Random rNum = new Random();
for (i = 0; i < A.Length; i++) A[i] = rNum.Next(101);
//数组A赋值随机数
Console.WriteLine("原始数组:");
DisplayMatrix(A);
//冒泡排序
int N = A.Length;
for (int loop = 1; loop <= N - 1; loop++)//外循环进行N-1轮比较
{
for (i = 0; i <= N - 1 - loop; i++)//内循环两两比较
{
if(A[i] < A[i+1])
{
t = A[i];
A[i] = A[i + 1];
A[i + 1] = t;
}
}
}
Console.WriteLine("降序数组:");
DisplayMatrix(A);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 选择排序
{
class Program
{
public static void DisplayMatrix(int[] A)
{
//打印矩阵
foreach (int i in A) Console.Write("{0,4}", i);
Console.WriteLine();
}
static void Main(string[] args)
{
int i, t,MaxI;
int[] A = new int[10];
Random rNum = new Random();
for (i = 0; i < A.Length; i++) A[i] = rNum.Next(101);
//数组A赋值随机数
Console.WriteLine("原始数组:");
DisplayMatrix(A);
//选择排序
int N = A.Length;
for (int loop = 0; loop <= N - 2; loop++)//外循环进行N-1轮比较
{
MaxI = loop;
for (i = loop; i <= N - 1; i++)//内循环在无序数中找到最大值
{
if (A[i] > A[MaxI]) MaxI = i;
}
t = A[loop];
A[loop] = A[MaxI];
A[MaxI] = t;
}
Console.WriteLine("降序数组:");
DisplayMatrix(A);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 矩阵加减
{
class Program
{
public static void DisplayMatrix(int[,] A)
{
//打印矩阵
for (int i = 0; i < A.GetLength(0); i++)
for (int j = 0; j < A.GetLength(1); j++)
Console.Write("{0,4}",A[i,j]);
Console.WriteLine();
}
static void Main(string[] args)
{
int i, j;
int[,] A = new int[4, 4];
int[,] B = new int[4, 4];
int[,] C = new int[4, 4];
int[,] D = new int[4, 4];
Random rNum = new Random();
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
A[i, j] = rNum.Next(10,101);
Console.WriteLine("数组A的内容:"); DisplayMatrix(A);
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
B[i, j] = rNum.Next(10, 101);
Console.WriteLine("数组A的内容:"); DisplayMatrix(B);
Console.WriteLine("上三角形式显示数组A的内容:");
for (i = 0; i < 4; i++)
{
for (int k = 0; k < i * 5; k++) Console.Write(" ");
for (j = i; j < 4; j++)
Console.Write("{0,4}",A[i,j]);
Console.WriteLine();
}
Console.WriteLine("下三角形式显示数组A的内容:");
for (i = 0; i < 4; i++)
{
for (j = 0; j < i+1; j++)
Console.Write("{0,4}", B[i, j]);
Console.WriteLine();
}
for (i = 0; i < 4; i++)
for (j = 0; j < 4;j++ )
C[i, j] = A[i, j] + B[i, j];
Console.WriteLine("数组A和数组B的相加之和:");
DisplayMatrix(C);
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
D[i, j] = A[i, j] - B[i, j];
Console.WriteLine("数组A和数组B的相减之差:");
DisplayMatrix(D);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 杨辉三角
{
class Program
{
static void Main(string[] args)
{
int[,] A = new int[10,10];
int i, j;
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
A[i, j] = 1;
for (i = 0; i < 10; i++)
for (j = 1; j < i; j++)
{
A[i,j] = A[i-1,j] + A[i-1,j-1];
}
for (i = 0; i < 10; i++)
{
for (j = 0; j <= i; j++)
Console.Write("{0,5}",A[i,j]);
Console.WriteLine();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace System.Array类
{
class Program
{
static void Main(string[] args)
{
int i;
int[] A = {80,90,67,89,78,85,45,69,77,95};
Console.WriteLine("数组A的维数(秩rank):{0}",A.Rank);
Console.WriteLine("数组A的长度(元素总数):{0}", A.Length);
Console.WriteLine("数组A的内容:");
//A.Length == A.GetLength(0);
for (i = 0; i < A.GetLength(0); i++) Console.Write("{0,4}",A[i]);
Array.Sort(A);
Console.WriteLine("\n数组A排序后的内容:");
for(i = 0;i < A.Length;i++) Console.Write("{0,4}",A[i]);
Array.Reverse(A);
Console.WriteLine("\n数组A排序反转后后的内容:");
for (i = 0; i < A.Length; i++) Console.Write("{0,4}",A[i]);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 指针操作
{
unsafe class Program
{
public static void swap(int* pa, int* pb)
{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}
static void Main(string[] args)
{
int a, b, c;
int* pa, pb, pc;
Console.Write("请输入整数a:");
string s = Console.ReadLine();
a = int.Parse(s);
Console.Write("请输入整数b:");
s = Console.ReadLine();
b = int.Parse(s);
Console.Write("请输入整数c:");
s = Console.ReadLine();
c = int.Parse(s);
Console.WriteLine("原始数据:a={0},b={1},c={2}", a, b, c);
pa = &a; pb = &b; pc = &c;
if (*pa > *pb) swap(pa, pb);
if (*pa > *pc) swap(pa, pc);
if (*pb > *pc) swap(pb, pc);
Console.WriteLine("升序排序:a={0},b={1},c={2}", a, b, c);
}
}
}