using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1数组
{
class Program
{
static void Main(string[] args)
{
//数组:具有相同类型的若干变量按有序的形式
//组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。
//int[] 变量名 = new int [n];
//n表示开辟多少个空位
//int [] array =new int[]{1,2,3,4,5};
//array[2] = 6;//把2换成6
//Console.WriteLine(array[0]);
//string [] array1=new string[5];
//for (int i = 0; i < array1.Length; i++)
//{
// Console.Write("请输入第"+(i+1)+"个名字:");
// array1[i] = Console.ReadLine();
//}
//从控制台输入10个人的年龄放入数组,
//将十个人的年龄求总和
//int[] name = new int[10];
//int sum = 0;
//for (int i = 0; i < name.Length; i++)
//{
// Console.Write("请输入第" + (i + 1) + "个年龄:");
// name[i] = int.Parse(Console.ReadLine());
// //sum += name[i];
//}
////Console.WriteLine(sum);
////排序 冒泡排序
//for (int i = 0; i < 10; i++)
//{
// for (int j = i; j < 9; j++)
// {
// if (name[i] < name[j + 1])
// {//定义中间变量去调换两个值
// int zhong = name[i];
// name[i] = name[j + 1];
// name[j + 1] = zhong;
// }
// }
//}
//for (int i = 0; i < 10; i++)
//{
// Console.WriteLine(name[i]);
//}
//生成十个不重复的数放入数组并打印出来
int[] suiji = new int[10];
Random ran = new Random();
for (int i = 0; i < 10; i++)
{
bool b = true;
int a = ran.Next(11);
for (int j = 0; j <= i; j++)
{
if (a == suiji[j])
{
b = false;
i--;
}
}
if (b)
{
suiji[i] = a;
}
}
//遍历数组
foreach (int aa in suiji)
{
Console.WriteLine(aa);
}
//练习:
//输入班级人数,根据人数,挨个输入成绩,求平均分,求最高分,最低分
Console.Write("输入班级人数:");
int renshu = int.Parse(Console.ReadLine());
double[] fenshu = new double[renshu];//定义数组fenshu有renshu个空位
double sum = 0;
for (int i = 0; i < renshu; i++)
{
Console.Write("请输入第" + (i + 1) + "个人的分数:");
fenshu[i] = double.Parse(Console.ReadLine());
sum += fenshu[i];
}
for (int i = 0; i < renshu; i++)//冒泡排序,因为有(renshu)个人,外循环需要(renshu)次
{ //i的值为0.所以i<renshu
//sum += fenshu[i];
for (int j = i; j < renshu - 1; j++)
{
if (fenshu[i] < fenshu[j + 1])
{
double zhong = fenshu[i];//这个排大小的方法叫冒泡法,就是从第一个数开始,
fenshu[i] = fenshu[j + 1];//用每一个数去跟后面的数字做比较,如果排序不对则交换位置
fenshu[j + 1] = zhong;
}
}
}
double pj = sum / renshu;
Console.WriteLine("平均成绩是" + pj + "分");
Console.WriteLine("最高分是" + fenshu[0] + "分");
Console.WriteLine("最低分是" + fenshu[renshu - 1] + "分");
//二维数组定义方式
//int [,] array =new int [a,b];
//a表示有几个一维数组
//b表示每个一维数组中有几个元素
int[,] array = new int[3, 4]{
{2,3,4,5},
{3,4,5,6},
{5,6,7,8}};
//Console.WriteLine(array[1,1]);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
//Console.WriteLine(array[i,j]);//打印方式
array[i, j] = int.Parse(Console.ReadLine());//接收方式
}
}
//打印一个王字
string[,] wang = new string[,]
{
{" ","■","■","■","■","■"," "},
{" "," "," ","■"," "," "," "},
{" "," "," ","■"," "," "," "},
{" "," ","■","■","■"," "," "},
{" "," "," ","■"," "," "," "},
{" "," "," ","■"," "," "," "},
{"■","■","■","■","■","■","■"}
};
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
Console.Write(wang[i, j]);
}
Console.WriteLine();
}
//练习二
//输入班级人数,根据人数创建数组
//将每一个人的语文,数学,英语成绩放进去
//计算出每个人的平均分
Console.Write("请输入班级人数:");
int renshu = int.Parse(Console.ReadLine());
double[,] fenshu = new double[renshu, 3];
for (int i = 0; i < renshu; i++)
{
double sum = 0;
for (int j = 0; j < 3; j++)
{
Console.Write("请输入成绩:");
fenshu[i, j] = double.Parse(Console.ReadLine());
sum += fenshu[i, j];
}
double pj = sum / 3;
Console.WriteLine("平均成绩是" + pj + "分");
}
Console.ReadLine();
}
}
}