using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1122
{
class Program
{
//随机生成一个数列。
static int[] randomArray(int x, int y)
{
Random rand = new Random();
int[] array = new int[x];
for (int i = 1; i <= array.Length; i++)
{
array[i - 1] = rand.Next(y);
}
return array;
}
//根据用户输入的数字生成数列。
static int[] exchangeToArray(string str)
{
string[] strArr = str.Split(' ');
int[] array = new int[strArr.Length];
for (int i = 1; i <= strArr.Length; i++)
{
array[i-1]=Convert.ToInt32(strArr[i-1]);
}
return array;
}
//得出用户输入的数列的长度
static int arrayLengthFromCustomer(string str)
{
int[] array = exchangeToArray(str);
int length = array.Length;
return length;
}
//冒泡排序
static int[] Bubbles(int[] array)
{
for (int i = 1; i <= array.Length - 1; i++)
{
for (int j = 1; j <= array.Length - i; j++)
{
if (array[j] < array[j - 1])
{
int temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
//输出数列
static int[] outPut(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0}\t", array[i]);
}
return array;
}
//二分法查找数字
static int Search(int[] array, int goal)
{
Bubbles(array);
int max = array.Length - 1;
int min = 0;
int mid;
while (true)
{
mid = (max + min) / 2;
if (min > max)
{
Console.WriteLine("木找到!");
break;
}
if (array[mid] > goal)
{
max = mid - 1;
}
if (array[mid] < goal)
{
min = mid + 1;
}
if (array[mid] == goal)
{
Console.WriteLine("Rock On!在第{0}个下标上!", mid);
break;
}
}
return mid;
}
//随机提取N个数字
static int[] Lucky(int[] array, int num)
{
int[] cho = new int[num];
Random rand = new Random();
int limit = array.Length;
for (int i = 1; i <= cho.Length; i++)
{
int id = rand.Next(array.Length - 1);
cho[i - 1] = array[id];
int temp = array[id];
array[id] = array[limit-1];
array[limit-1] = temp;
limit--;
}
return cho;
}
//求和
static int Sum(int[] array)
{
int sum = 0;
for (int i = 1; i <= array.Length; i++)
{
sum += array[i - 1];
}
return sum;
}
//求平均数
static double Avg(int[] array)
{
double avg = Sum(array) * 1.0 / array.Length;
return avg;
}
//找出最大值
static int maxValue(int[] array)
{
Bubbles(array);
int max = array[array.Length - 1];
return max;
}
//找质数……
static List<int> primeNumber(int[] array)
{
//List<int> list = new List<int>();
//list.Add(2);
//for (int a = 0; a < list.Count; a++)
//{
// Console.WriteLine(list[a]);
//}
//foreach (int b in list)
//{
// Console.WriteLine(b);
//}
List<int> list = new List<int>();
for (int i = 0; i < array.Length; i++)
{
int count = 0;
for (int j = 1; j <= array[i]; j++)
{
if (array[i] % j == 0)
{
count++;
}
}
if (count == 2)
{
list.Add(array[i]);
}
}
return list;
}
//输出list
static List<int> outPutList(List<int> list)
{
for (int i = 1; i <= list.Count; i++)
{
Console.WriteLine(list[i - 1]);
}
return list;
}
static void Main1(string[] args)
{
Console.WriteLine("请输入一个数列,如果不想输入,请输入R以及你想要的数列的长度和生成区间,以空格隔开,我们会随机生成一组数字。");
string cho = Console.ReadLine();
int[] array = null;
if (cho.Contains("R"))
{
string[] ranArr = cho.Split(' ');
int length = Convert.ToInt32(ranArr[1]);
int maxValue = Convert.ToInt32(ranArr[2]);
array = randomArray(length, maxValue);
}
else
{
array = exchangeToArray(cho);
}
Console.WriteLine("下面做一些AMAZING的事情吧!请根据提示输入数字,程序会做出相应的操作:");
Console.WriteLine("1:冒泡排序,2:二分法找出数字,3:随机在数列中随机提取N个数字,4:求和,5:求平均数,6:找出最大值,7:找出里面的质数……");
Console.WriteLine("键入Q退出");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
outPut(Bubbles(array));
break;
case "2":
Console.WriteLine("请输入你想要查找的数字:");
int target = Convert.ToInt32(Console.ReadLine());
Search(array, target);
break;
case "3":
Console.WriteLine("随机提取多少个数字?");
int howMany = Convert.ToInt32(Console.ReadLine());
outPut ( Lucky(array, howMany));
break;
case "4":
Console.WriteLine("这一堆数字的和是{0}", Sum(array));
break;
case "5":
Console.WriteLine("这一堆数字的平均数是{0}", Avg(array).ToString("#.00"));
break;
case "6":
Console.WriteLine("这一堆数字的最大值是{0}", maxValue(array));
break;
case "7":
outPutList(primeNumber(array));
break;
case "Q":
break;
}
}
}
}
这个小程序是我昨天练习的时候做的,里面大概综合了以前的所有知识点,我把他们都做成了函数,每一条我都加了注释,希望大家能看明白。