求一个算法
已知string a= "1,2,3,4,...56 "
要求得到一个string[] b
其中b[0]= "1,2,3,...10 ";
b[1]= "11,12,13,...20 " ;
...
b[4]= "41,42,...50 ";
b[5]= "51,52,...56 ";
//总之将a里面以逗号分开的值10个10个的分开,最后组成一个sting[]
private string[] getAnswer(string a)
{
...
retrun b;
}
请教这个算法该怎么写,多谢!!!!!!!!!!!
------解决方案--------------------private string[] getAnswer(string a)
{
string[] arr = a.Split( ', ');
int group = (arr.Length + 9) / 10;
string[] result = new string[group];
for (int i = 0; i < group; i++)
{
string s = " ";
for (int j = 0; j < 10; j++)
{
int index = i * 10 + j;
if (index > = arr.Length)
break;
s += arr[index] + ", ";
}
s = s.TrimEnd( ', ');
result[i] = s;
}
return result;
}