给定一个数字80,写一个方法把80随机分成4个单独的数字。
比如80 -> 25 29 10 16
分配的方式是随机。
请大家帮忙。
------解决方案--------------------------------------------------------
关注
------解决方案--------------------------------------------------------
那四个数字和80有关吗
------解决方案--------------------------------------------------------
//产生随机字符串
private string GenCode(int num)
{
string[] source ={"0","1","2","3","4","5","6","7","8","9"};
string code = "";
Random rd = new Random();
for (int i = 0; i < num; i++)
{
code += source[rd.Next(0, source.Length)];
}
return code;
}
------解决方案--------------------------------------------------------
- C# code
public int[] GetNum(int value) { bool exits = true; int[] item = new int[4]; Random rand = new Random(); while (exits) { item[0] = rand.Next(value); item[1] = rand.Next(value); item[2] = rand.Next(value); item[3] = rand.Next(value); if (item[0] + item[1] + item[2] + item[3] == value) { exits = false; } } return item; }