English 中文(简体)
A. 如何设置阵容尺寸上限
原标题:
  • 时间:2009-05-12 19:14:24
  •  标签:

我有两个阵列。

string[] input; //user input could be any size
string[] output; //a copy of user input but, should not be larger than 50

如果投入长度和带;=50,那么产出就是投入的确切复制件。

如果投入阵列长度大;50个则只复制50个投入内容

www.un.org/Depts/DGACM/index_french.htm

这样做的最有效方式是什么?

UPDATE say input[] has 98 elements. then you would take the first and last elements and then divide the rest by 2 to get 50 elements

98-2=96
96/2=48

2+48=50
问题回答
for (float i = 0, int count = 0; count < 50; i+= arraySize / 50.0f, count++)
{
output[count] = input[(int)i];
}

类似:

public static T[] CopyEvenly<T>(T[] from, int size)
{
    if (from.Length <= size)
    {
        return (T[]) from.Clone();
    }
    T[] ret = new T[size];
    for (int i=0; i < size; i++)
    {
        ret[i] = from[(i * (from.Length + size - 1)) / size];
    }
    return ret;
}

如果您进入了“<>t<>int>>的多重复性超支的阶段,就会失败。

我认为,在你获得指数之前,你面临一个近似问题,涉及秘密分裂。

static T[] CopyEvenly<T>(T[] source, int size)
{
    if (size >= source.Length)
        // or copy it to a new one if you prefer
        return source;

    T[] ret = new T[size];
    // keep everything in doubles
    double factor = (double)(source.Length - 1) / (double)(size - 1);
    for (int i = 0; i < ret.Length; i++)
    {
        // cast to int just now
        int inputIndex = (int)((double)i * factor);

        ret[i] = source[inputIndex];
    }
    return ret;
}

我希望我正确理解你的回答。

这可能是徒劳的,但我认为,尝试是徒劳的。 我希望我不会使事情更加混淆。

static T[] CopyEvenly<T>(T[] srcArray, int size)
{
   int factor=srcArray.Length/size; //this will be the "step" size
   T[] retArray=new T[size];

   int counter = 0;

   //add element 0 and every [factor] ith element until 1 less than size
   while (counter < size - 1 && counter<srcArray.Length)
   {
      retArray[counter] = srcArray[counter * factor];
      counter++;
   }

   //add the last element
   retArray[size] = srcArray[srcArray.Length - 1];

   return retArray;
}




相关问题
热门标签