面试题对平时编程习惯的考验

这道题都不难,在面试的时候就随便写了下,后来想想有很多边界没有控制,结构不清晰,写的没有扩展可言,后来方法改良如下:  欢迎拍砖和指教

题目1:是获取1-n之间不重复的随机数

/// <summary>
/// 获得不重复的随机数数组
/// </summary>
/// <param name="min">取值范围:开始值</param>
/// <param name="max">取值范围:结束值</param>
/// <param name="count">数组大小</param>
/// <returns></returns>
public static int[] GetRandomDistinctIntArray(int minValue, int maxValue, int count)
{

int length = (maxValue - minValue) + 1; //取值个数

#region 参数验证
if (minValue <= 0 || maxValue < 2)
{
throw new ArgumentException("输入开始值大于0且结束值大于1!");
}

if ((maxValue - minValue) <= 0)
{
throw new ArgumentException("输入开始值和结束值之间的取值个数应大于1!");
}

if (minValue > maxValue)
{
throw new ArgumentException("输入开始值必须小于结束值!");
}

if (count > length)
{
throw new ArgumentException("数组大小必须小于或等于取值个数!");
}
#endregion

int[] intList = new int[length]; //临时数组

for (int i = 0; i < length; i++) //初始化一个顺序数组
{
intList[i]
= i + minValue;
}

int[] intRet = new int[count]; //用于存储结果的数组

int n = length;

Random rand
= new Random();

#region 交换元素

for (int i = 0; i < count; i++) //交换元素
{

int randomNumber = rand.Next(minValue, maxValue + 1);

int index = randomNumber - minValue; //索引值

intRet[i]
= intList[index]; //把当前随机数的值放在存储结果的数组里
intList[index] = intList[--n]; //将当前随机数放到临时数组的最后面

maxValue
--; //缩小取值范围
}


#endregion

return intRet;
}

查找指定出现次数的第一个字符

/// <summary>
/// 查找指定出现次数的第一个字符
/// </summary>
/// <param name="str">需要查找的字符</param>
/// <param name="charFindCount">查找次数</param>
/// <returns></returns>
public static string FindFirstCountChar(string str, uint charFindCount)
{
if (str == null || str == string.Empty)
{
throw new ArgumentException("请输入需要查找的字符!");
}
if (charFindCount<1)
{
throw new ArgumentException("查找字符出现的次数应大于0!");
}

int count = 0; //字符出现的次数

for (int i = 0; i < str.Length; i++)
{
char currentChar = str[i]; //需要查找的字符

for (int j = 0; j < str.Length; j++) //在string数组中循环查找
{
if (currentChar == str[j])
{
count
+= 1;
}
}

if (count == charFindCount)
{
return currentChar.ToString();
}
count
= 0;
}
return null;
}

数组排序

/// <summary>
/// int数组排序
/// </summary>
/// <param name="array">需要排序的int集合</param>
/// <param name="sortOrder">排序规则:true 升序,false降序</param>
public static int[] IntArraySort(int[] array, bool isSortAsc)
{
if (array == null || array.Length < 2)
{
throw new ArgumentException("数组必须不为空且数组长度大于2否则无法排序!");
}
int temp; //临时变量,保存最大,小值
for (int j = 0; j < array.Length; j++)
{
for (int i = 0; i < array.Length - j - 1; i++)
{
if (isSortAsc)
{
#region 升序开始交换
if (array[i] > array[i + 1]) // 如果 array[i] > array[i+1] ,则 array[i] 上浮一位,为升序,否则下浮一位为降序
{
temp
= array[i]; //开始交换
array[i] = array[i + 1];
array[i
+ 1] = temp;
}
#endregion
}
else
{
#region 降序开始交换
if (array[i] < array[i + 1])
{
temp
= array[i];
array[i]
= array[i + 1];
array[i
+ 1] = temp;
}
#endregion
}
}
}
return array;
}

最后打印输出

/// <summary>
/// 打印输出
/// </summary>
private static void OutPrint()
{
#region 随机数组打印
try
{
int[] intArray = GetRandomDistinctIntArray(1, 15, 5);

if (intArray == null || intArray.Length <= 0)
{

Console.WriteLine(
"生成随机数组失败!");
}
else
{
foreach (var item in intArray)
{

Console.Write(item
+ ",");
}
}
}
catch (Exception ex)
{

Console.WriteLine(ex.Message);
}

#endregion

#region 数组排序打印

try
{
//int[] intInputArray = new int[] { 5 };
//int[] intOutArray = IntArraySort(intInputArray, false);

//if (intOutArray == null || intOutArray.Length <= 0)
//{
// Console.WriteLine("数组排序失败!");
//}
//else
//{
// foreach (var item in intOutArray)
// {
// Console.Write(item + ",");
// }
//}

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion

#region 查找字符打印
//try
//{
// string str = FindFirstCountChar("abbcbcc",0);
// if (str == null || str == string.Empty)
// {
// Console.Write("无法找到");
// }
// else
// {
// Console.Write(str);
// }
//}
//catch (Exception ex)
//{

// Console.WriteLine(ex.Message);
//}
#endregion
}

posted @ 2011-05-13 16:27  逍然  阅读(2933)  评论(21编辑  收藏  举报