要求:不允许使用库函数及外部库,不允许使用现成的排序算法和哈希算法函数
1、两个元素之差的最大值
{
//int[] arrys = new int[] { 3, 5, 1, 6, 9, 8, 4, 7, 2 };
int max = arrys[0];
int min = arrys[0];
for (int i = 0; i < arrys.Length; i++)
{
if (arrys[i] > max)
max = arrys[i];
if (arrys[i] < min)
min = arrys[i];
}
Console.WriteLine("Max为:{0},Min为{1},差为{2}", max, min, max - min);
}
//2、数组中出现次数最多的数
/// <summary>
/// 得到去重复的数组
/// </summary>
/// <param name="arrys"></param>
/// <returns></returns>
private static int[] GetNewArry(int[] arrys)
{
for (int i = 0; i < arrys.Length; i++)
{
for (int j = i + 1; j < arrys.Length; j++)
{
if (arrys[i] == arrys[j])
arrys[i] = 0;
}
}
List<int> list = new List<int>();
for (int k = 0; k < arrys.Length; k++)
{
if (arrys[k] != 0)
list.Add(arrys[k]);
}
int[] arryNew = new int[list.Count];
for (int n = 0; n < list.Count; n++)
{
arryNew[n] = list[n];
}
return arryNew;
}
/// <summary>
/// 得到出现最多次数的数
/// </summary>
/// <param name="arrysOld"></param>
/// <param name="arrysNew"></param>
/// <returns></returns>
private static int GetMaxCountNum(int[] arrysOld, int[] arrysNew)
{
int count = GetMaxCount(arrysOld, arrysNew);
int tmp = 0;
for (int i = 0; i < arrysNew.Length; i++)
{
for (int j = 0; j < arrysOld.Length; j++)
{
if (arrysNew[i] == arrysOld[j])
{
tmp++;
}
}
if (count == tmp)
{
return arrysNew[i];
}
tmp = 0;
}
return 0;
}
/// <summary>
/// 得到出现最多的次数
/// </summary>
/// <param name="arrysOld"></param>
/// <param name="arrysNew"></param>
/// <returns></returns>
private static int GetMaxCount(int[] arrysOld, int[] arrysNew)
{
int maxCount = 0;
int count = 0;//次数
//int[] arrysCount = new int[arrysNew.Length];
for (int i = 0; i < arrysNew.Length; i++)
{
for (int j = 0; j < arrysOld.Length; j++)
{
if (arrysNew[i] == arrysOld[j])
{
count++;
}
}
//arrysCount[i] = count;
if (count > maxCount)
{
maxCount = count;
}
count = 0;
}
return maxCount;
}
/// <summary>
/// 得到数组中的最大数
/// </summary>
/// <param name="arrys"></param>
/// <returns></returns>
private static int GetMaxNum(int[] arrys)
{
int max = arrys[0];
for (int i = 0; i < arrys.Length; i++)
{
if (arrys[i] > max)
max = arrys[i];
}
return max;
}
/// <summary>
/// 得到数组中零出现的次数
/// </summary>
/// <param name="arrys"></param>
/// <returns></returns>
private static int GetMaxCountIsZero(int[] arrys)
{
int count = 0;
for (int i = 0; i < arrys.Length; i++)
{
if (arrys[i] == 0)
count++;
}
return count;
}
/// <summary>
/// 克隆一个整型数组
/// </summary>
/// <param name="arrys"></param>
/// <returns></returns>
private static int[] CloneArry(int[] arrys)
{
int[] arrysNew = new int[arrys.Length];
for (int i = 0; i < arrys.Length; i++)
{
arrysNew[i] = arrys[i];
}
return arrysNew;
}
在招聘网上看到的题目,随便坐了下,可能还有bug,望大家指出!!