日期:2014-05-19  浏览次数:20938 次

用C#实现一个数组的问题100分
给出一组int型数组如下(长度已知)
0     0     0     ……   0     2     4     5     6     8     2     ……   4     0     0     0……     0     4     3     2   ……   3     0     0     0     ……
数组的特征是每隔若干个0后就有若干个自然数   ,现在把0看成空值,现在要实现一个功能,将这个数组里的0全部剔除,将自然数保存在另外定义的数组中,我表达能力不好,举个例子说明下吧

如下整型数组   a[10]     {0   0   1   2   3   0   0   0   5   8}
我要实现的功能是处理后分成两个数组   b[3]   {1   2   3}   和c[2]   {5   8}  
同理如果数组是   {0   0   1   2   0   0   3   4   0   0   5}这样就分成3个数组   {1   2}{3   4}{5}
原始数组长度是已知的,但是不知道里面有多少个   0,也就是空值,也有可能整个数组全是0或全不是。

我不知道说明白了没有,请大家帮忙,多谢

------解决方案--------------------
static void Main(string[] args)
{
int[] a = new int[] { 0, 0, 1, 2, 0, 0, 3, 4, 0, 0, 5 };
List <int[]> results = new List <int[]> ();
int index = 0;
while (index < a.Length)
{
while (index < a.Length && a[index] == 0)
{
index++;
}
if (index < a.Length)
{
List <int> newarray = new List <int> ();
while (index < a.Length&&a[index] != 0)
{
newarray.Add(a[index]);
index++;
}
results.Add(newarray.ToArray());
}
}
foreach (int[] ia in results)
{
Console.Write( "{ ");
foreach (int var in ia)
{
Console.Write(var);
Console.Write( " ");
}
Console.WriteLine( "} ");
}
}
------解决方案--------------------
你分成二维数组不好吗?
int[] test = { 0,1,2,0,0,3,0,5,1,8,7,0,0,0,0,5,9,4};
int length = test.Length;
int[][] need = new int[length][];
for (int i = 0; i < length; i++)
{
//具体去比较实现
}
------解决方案--------------------
List <string> list = new List <string> ();
string s = "002323001230000 ";
string[] ss = s.Split( '0 ');
foreach (string s in ss)
{
if (s != null && s != string.Empty)
{
list.Add(s);
}
}

ss = list.ToArray();
return ss;
------解决方案--------------------
// 未测试

List <List <int> > result = new List <List <int> > ();
const int separator = 0;
for (int i = 0; i < source.Length; i++) {
if (source[i] != separator) {
if (i == 0 || source[i-1] == 0) {
result.Add(new List <int> ());
}
result[result.Count - 1].Add(source[i]);
}
}
------解决方案--------------------