0

我有一个程序使用如下所示的数组:

1,2,3,4,5

我有一个数字“10”,我想做的是从数组中减去这个数字。

很难解释,但我在减法中使用了数组的多个元素。这将不得不发生:

   10 - 5 = 5 (array looks now like this => 1,2,3,4)
   5  - 4 = 1 (array looks now like this => 1,2,3)
   3  - 1 = 2 (array looks now like this => 1,2,2)

总结一下:

这:1,2,3,4,5 减去 10 会给我 1,2,2

但是我该怎么做呢?编程语言是 C#

编辑 :

输入 =

1. 一个以 5 个整数作为元素的数组: 1, 2 ,3 ,4 和 5 2 。数字 10(整数)

过程 =

  1. 从最后一个值中减去 10,然后从数组的下一个元素中减去结果数,直到没有 '10' 为止。

输出 =

而不是 1,2,3,4,5 => 1,2,2 (逗号分隔数组中的元素)

4

2 回答 2

2

你的意思是这样的:

public static int[] Subtract(int[] array, int value) {
  if (Object.ReferenceEquals(null, array))
    throw new ArgumentNullException("array");

  if (value < 0) 
    return Addition(array, -value);

  int s = 0;

  int index = 0;
  int delta = 0;

  for (int i = array.GetLength(0) - 1; i >= 0; --i) {
    s += array[i];

    if (s > value) {
      index = i;
      delta = s - value;

      break;
    }
  }

  // Too big a value is subtracted, let's return an empty array
  if ((index <= 0) && (delta <= 0)) // <- (delta <= 0) to prevent [x, ... z, 0] answers
    return new int[0];

  int[] result = new int[index + 1];

  for (int i = 0; i < index; ++i)
    result[i] = array[i];

  result[index] = delta;

  return result;
}

// Maximum possible electrons within orbital; 0 - s1, 1 - s1, 2 - p1, 3 - s2 etc.
// Double arithmetic progression's here (main quantum number - level, orbital quantum number - s, p, d...)
private static int MaxElectronsCount(int value) {
  Double n = (-1 + Math.Sqrt(1 + 8.0 * (value + 1))) / 2;

  int group = (int)(n + 1.0 - 1.0e-8); // <- round up to high limit
  int shift = group - (group * (group + 1) / 2 - value);

  return 2 + shift * 4;
}

// Electrons addition 
public static int[] Addition(int[] array, int value) {
  if (Object.ReferenceEquals(null, array))
    throw new ArgumentNullException("array");

  if (value < 0)
    return Subtraction(array, -value);

  List<int> result = new List<int>();

  for (int i = 0; i < array.GetLength(0); ++i)
    result.Add(array[i]);

  int level = 0;

  while (value > 0) {
    if (result.Count <= level)
      result.Add(0);

    int max = MaxElectronsCount(level);
    int delta = max - result[level];

    if (delta > value)
      delta = value;

    if (delta > 0) {
      result[level] = result[level] + delta;
      value -= delta;
    }

    level += 1;
  }

  return result.ToArray();
}
....  

int[] test1 = Subtract(new int[] { 1, 2, 3, 4, 5 }, 1); // <- [1, 2, 3, 4, 4] 
int[] test2 = Subtract(new int[] { 1, 2, 3, 4, 5 }, 5); // <- [1, 2, 3, 4] 
int[] test3 = Subtract(new int[] { 1, 2, 3, 4, 5 }, 6); // <- [1, 2, 3, 3] 
int[] test4 = Subtract(new int[] { 1, 2, 3, 4, 5 }, 10); // <- [1, 2, 2]
int[] test5 = Subtract(new int[] { 1, 2, 3, 4, 5 }, 1000); // <- [] 
int[] test6 = Subtract(new int[] { 1, 2, 3, 4, 5 }, -1); // <- [2, 2, 3, 4, 5]
int[] test7 = Subtract(new int[] { 2, 2 }, 2); // <- [2]

int[] test8 = Addition(new int[] {2, 1}, 16); // <- [2, 2, 6, 2, 6, 1]
于 2013-08-05T12:47:10.023 回答
0

我不确定我是否理解你,但试试这个:

private void SubstractNumberFromArray(List<int> array, int number)
{
    //array.Sort();
    for (int i = array.Count - 1; i >= 0; i--)
    {
        int toSubstract = Math.Min(number, array[i]);
        array[i] -= toSubstract;
        if (array[i] == 0)
            array.RemoveAt(i);
        number -= toSubstract;

        if (number == 0)
            break;
    }

    if (number != 0)
        throw new Exception("Can't substract!");

}
于 2013-08-05T12:49:11.553 回答