前几天看到一个有趣的算法题,”随机产生26个数,使其总和等于301“,代码如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int[] arryRandom = new int[26];
var ran = new Random();
//add 1 301times into the arry
for (int i = 0; i < 301; i++)
{
arryRandom[ran.Next(0, 26)]++;
}
//chenck the arry
int sum = 0;
for (int j = 0; j < arryRandom.Length; j++)
{
Console.WriteLine(arryRandom[j]);
sum = sum + arryRandom[j];
}
Console.WriteLine("sum:" + sum);
Console.ReadKey();
}
}
}