当前位置:编程学习 > C#/ASP.NET >>

国外大学入取考试题(英文)--求答案。

2. Define a cluster in an integer array to be a maximum sequence of elements that are all the same value. For example, in the array {3, 3, 3, 4, 4, 3, 2, 2, 2, 2, 4} there are 5 clusters, {3, 3, 3}, {4, 4}, {3}, {2, 2, 2, 2} and {4}. A cluster-compression of an array replaces each cluster with the number that is repeated in the cluster. So, the cluster compression of the previous array would be {3, 4, 3, 2, 4}. The first cluster {3, 3, 3} is replaced by a single 3, and so on.

Write a function named clusterCompression with the following signature
If you are programming in Java or C#, the function signature is
int[ ] clusterCompression(int[ ] a)

If you are programming in C++ or C, the function signature is
int *clusterCompression(int a[ ], int len) where len is the length of the array. 

The function returns the cluster compression of the array a. The length of the returned array must be equal to the number of clusters in the original array! This means that someplace in your answer you must dynamically allocate the returned array.
In Java or C# you can use 
int[ ] result = new int[numClusters];
In C or C++ you can use 
int *result = (int *)calloc(numClusters, sizeof(int));
Examples
  
a is {0, 0, 0, 2, 0, 2, 0, 2, 0, 0}then function returns{0, 2, 0, 2, 0, 2, 0}
a is {18} then function returns {18}
a is {} then function returns {}
a is {-5, -5, -5, -5, -5} then function returns {-5}
a is {1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1} then function returns {1, 2, 1}
a is {8, 8, 6, 6, -2, -2, -2} then function returns {8, 6, -2} --------------------编程问答-------------------- 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();
}


--------------------编程问答-------------------- 楼上的解答是一个方法。

我的要求是,创建一个子数组,int[ ] result = new int[numClusters];
要用到这个句子。

该怎么办? --------------------编程问答--------------------
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;
    }
} --------------------编程问答--------------------

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;
    }
}
--------------------编程问答-------------------- 我是不羁的风啊,不好意思,刚忙完回来。

如下程序能在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();
    }
}

--------------------编程问答-------------------- Grandson had made the questions
Son is invigilating me
Because I'm your father,so I don't konw --------------------编程问答-------------------- 洋鬼子!
膜拜!
--------------------编程问答--------------------
引用 6 楼 soaringsnake 的回复:
Grandson had made the questions
Son is invigilating me
Because I'm your father,so I don't konw

fck u ! --------------------编程问答-------------------- 学习,帮顶
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,