日期:2014-05-18  浏览次数:20406 次

今天面试题 没搞出来 出丑了
一:题目大意
54张牌   要求随机   顺序会发生改变   今天面试的时候没搞出来   晕   脑子一片空白   现在为了安慰自己   回家写了一个   大家参考一下  
看看大家有没有什么简便的写法

private   string   CreateRandomCode(int   codeCount)
{
string   allChar   =   " ";
string   randomCode   =   " ";
int   temp   =   -1;

for(int   i=1;i <55;i++)
{
allchar+=i.ToString()+ ", "
}

string[]   allCharArray   =   allChar.Split( ', ');

Random   rand   =   new   Random();
for(int   i   =   0;   i   <   codeCount;   i++)
{
if(temp   !=   -1)
{
rand   =   new   Random(i*temp*((int)DateTime.Now.Ticks));
}
int   t   =   rand.Next(54);
if(temp   ==   t)
{
return   CreateRandomCode(codeCount);
}
temp   =   t;
randomCode   +=   allCharArray[t];
}
return   randomCode;
}

调用CreateRandomCode(54)

顺便回顾一下随机数的生成   面试的时候   忘了Random这个函数   晕倒   不知道该咋写了   顺便求一个另外简便的写法

第二题题目大概   一个表   id,title,content,date1     写出插入,更新,删除的三个存储过程     这个题略过不讨论   一般人都知道


------解决方案--------------------
晕啊,这么简单的问题还用递归?
你不妨模仿一下洗牌的过程,不就几张牌翻来倒去吗?

先假定54张牌是按序排放的, 然后随机产生两张牌的位置,并交换之. 循环N次.想洗乱一些N就大些.算法复杂度是O(N)级的,而你的是N平方级的
------解决方案--------------------
很久没写console程序我也写个练下

using System;
using System.Collections;

namespace RandomCode
{
/// <summary>
/// 随机发54张牌
/// </summary>
class MySolution
{
class AppSolution
{
const int MAX = 54;
ArrayList arrResoult;
ArrayList arrResoultNum;

public AppSolution()
{
arrResoult = new ArrayList(MAX);
arrResoultNum = new ArrayList(MAX);

for(int i = 0;i < MAX;i++)
{
arrResoult.Add((int)0);
arrResoultNum.Add((int)0);
}
}

public void Run()
{
int Count = 0;
int arrNum = 0;
Random seed = new Random(DateTime.Now.Millisecond);
do
{
arrNum = seed.Next(MAX);
if((int)arrResoult[arrNum] < 1)
{
arrResoult[arrNum] = 1;
arrResoultNum[Count] = arrNum;
Count++;
}
}while(Count != MAX);
}

public void Display()
{
foreach(int arrNum in arrResoultNum)
{
Console.Write( "{0}\t ",arrNum);
}
Console.WriteLine();
}

public void Sort()
{
arrResoultNum.Sort();
}
}

[STAThread]
static void Main(string[] args)
{
AppSolution app = new AppSolution();
app.Display();
app.Run();
app.Display();
app.Sort();
app.Display();
string cmdInput = Console.ReadLine();
}
}
}