int[ ] clusterCompression(int[ ] a)
{
    List lst=new List<int>();
    int cur=a[0];
    lst.add(cur);
    foreach(int v in a)
    {
       if(v!=cur){lst.add(v);cur=v;}
    }
    return lst.ToArry();
}

解决方案 »

  1.   

    楼上的解答是一个方法。我的要求是,创建一个子数组,int[ ] result = new int[numClusters];
    要用到这个句子。该怎么办?
      

  2.   

    using System;
    /*
    if a is and m is and n is Return reason
    {3, 9, -3, 1, 0, 3, 10} 0 3 {3, 1, 0, 3} those are the elements x such that m <= x <= n. Furthermore, they are in the same order as they were in the original array.
    {18, 0, 15, -5} 5 8 { } there are no elements whose values are in the given range. I.E. there are no elements that are greater than or equal to 5 and less than or equal to 8.
    {3, 2, 1, 4, 6, 2, 10} 0 100 {3, 2, 1, 4, 6, 2, 10} all elements in the array are in the given range. 
    {2, 1, 2, 2, 1, 2} 1 1 {1, 1} only the 1-valued elements are in the given range
    {} 5 100 { } no elements are in the given range.
    {1, 2, 4, 8, 3} 4 3 null m must be less than or equal to n
    {9, 3, 1, -9} 100 120 { } there are no elements in the given range. */
    class SquareApp
    {
        public static void Main()
        {
            int[] arr = getSubarray(new int[] { 3, 9, -3, 1, 0, 3, 10 },0,3);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("{0} ", arr[i]);
            }
            Console.ReadKey();
        }
        static int[] getSubarray(int[] a, int m, int n)
        {        int[] result = new int[5]; //这里的result如何能定义为动态数组,自动改变大小呢?        int i = 0,j=0,count=0;
            
            if (m > n || a==null)
            {
                return null;
            }
            for (i = 0; i < a.Length; i++)
            {
                if (a[i] >= m && a[i] <= n)
                {
                   result[j++] = a[i];
                }
            }
            
            
            return result;
        }
    }
      

  3.   


    using System;
    /*
    if a is and m is and n is Return reason
    {3, 9, -3, 1, 0, 3, 10} 0 3 {3, 1, 0, 3} those are the elements x such that m <= x <= n. Furthermore, they are in the same order as they were in the original array.
    {18, 0, 15, -5} 5 8 { } there are no elements whose values are in the given range. I.E. there are no elements that are greater than or equal to 5 and less than or equal to 8.
    {3, 2, 1, 4, 6, 2, 10} 0 100 {3, 2, 1, 4, 6, 2, 10} all elements in the array are in the given range. 
    {2, 1, 2, 2, 1, 2} 1 1 {1, 1} only the 1-valued elements are in the given range
    {} 5 100 { } no elements are in the given range.
    {1, 2, 4, 8, 3} 4 3 null m must be less than or equal to n
    {9, 3, 1, -9} 100 120 { } there are no elements in the given range. */
    class SquareApp
    {
        public static void Main()
        {
            int[] arr = getSubarray(new int[] { 3, 9, -3, 1, 0, 3, 10 },0,3);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("{0} ", arr[i]);
            }
            Console.ReadKey();
        }
        static int[] getSubarray(int[] a, int m, int n)
        {
            int[] result = new int[5];//这里的result如何能定义为动态数组,自动改变大小呢?        int i = 0,j=0,count=0;
            
            if (m > n || a==null)
            {
                return null;
            }
            for (i = 0; i < a.Length; i++)
            {
                if (a[i] >= m && a[i] <= n)
                {
                   result[j++] = a[i];
                }
            }
            
            
            return result;
        }
    }
      

  4.   

    我是不羁的风啊,不好意思,刚忙完回来。如下程序能在C# 2.0下跑通,我已经调过了。你不用给积分给我啦,给其他兄弟吧
    using System;
    using System.Collections.Generic;
    class SquareApp
    {
        public static void Main()
        {
            int[] arr = clusterCompression(new int[] { 0, 0, 0, 2, 0, 2, 0, 2, 1, 0 });
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write("{0} ", arr[i]);
            }
            Console.ReadKey();
        }
        static int[] clusterCompression(int[] a)
        {
            List<int> lst = new List<int>();
            int cur = a[0];
            lst.Add(cur);
            foreach (int v in a)
            {
                if (v != cur) { lst.Add(v); cur = v; }
            }
            return lst.ToArray();
        }
    }
      

  5.   

    Grandson had made the questions
    Son is invigilating me
    Because I'm your father,so I don't konw