遭遇这样一道面试题:
52张扑克牌随机分给4个人,每个人得13张,设计一下发牌程序。
手头没有编译工具,在记事本里用javascript实现了一下。
<script>
var m,n,temp;
var count = new Array();
for(var i=0; i<3; i++)
{
count[i] = 0;
}
var Rect= new Array(
[0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0]
);
for (var i=0; i<3; i++)
{
while(count[i]<13)
{
m = Math.floor(Math.random()*4); //0-3的随机数
n = Math.floor(Math.random()*14) ;
if(Rect[m][n] == 0)
{
Rect[m][n] = i+1;
count[i]++;
}
}
}
for(var i = 0; i<4; i++)
{
for(var j = 0; j<13; j++)
{
document.write(Rect[i][j])
}
document.write("<br>");
}
for(var i = 0; i<4; i++)
{
document.write("第" + i + "个人的牌:")
for(var m = 0; m<4; m++)
{
for(var n = 0; n<13; n++)
{
if(Rect[m][n] == i)
{
switch(m)
{
case 0:
document.write("红桃");
break;
case 1:
document.write("方块");
break;
case 2:
document.write("草花");
break;
case 3:
document.write("黑桃");
}
var temp = n+1;
switch(temp)
{
case 1:
document.write("A");
break;
case 11:
document.write("J");
break;
case 12:
document.write("Q");
break;
case 13:
document.write("K");
break;
default:
document.write(temp);
}
document.write(";");
}
}
}
document.write("<br>");
}
</script>