C# 从数组中减去数字

C# 从数组中减去数字,c#,arrays,subtraction,C#,Arrays,Subtraction,我有一个程序,它使用的数组如下所示: 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,

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

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#

编辑:

输入=

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

进程=

  • 从最后一个值中减去10,然后从数组的下一个元素中减去得到的数字,直到“10”中没有剩余值
  • 输出=

    而不是1,2,3,4,5=>1,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]
    
    公共静态int[]减法(int[]数组,int值){
    if(Object.ReferenceEquals(null,数组))
    抛出新的ArgumentNullException(“数组”);
    如果(值<0)
    返回加法(数组,-值);
    int s=0;
    int指数=0;
    int delta=0;
    for(int i=array.GetLength(0)-1;i>=0;--i){
    s+=数组[i];
    如果(s>值){
    指数=i;
    δ=s值;
    打破
    }
    }
    //减去的值太大,让我们返回一个空数组
    
    如果((索引我不确定是否理解您的意思,但请尝试以下方法:

    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!");
    
    }
    
    private void SubstractNumberFromArray(列表数组,整数)
    {
    //array.Sort();
    对于(int i=array.Count-1;i>=0;i--)
    {
    int-toSubstract=Math.Min(数字,数组[i]);
    数组[i]=toSubstract;
    if(数组[i]==0)
    array.RemoveAt(i);
    数量-=到子域;
    如果(数字==0)
    打破
    }
    如果(数字!=0)
    抛出新异常(“不能减法!”);
    }
    
    我不明白这个问题:-s,也看不出为什么数组==在操作后它们会做什么您应该从最后一个索引循环到第一个索引,并删除/更改每个值,所以为了从最后一个索引循环,…我应该像这样使用for循环:for(int I==5;I=0;I--){;}?你的意思是说有一个数组有10个元素。当减去5时,它的值只会小于得到的结果?是的,但在问题中,我用“,”来分隔数组中的不同值,10是一个整数-我试过了;它工作正常,但正好当我有一个数组({2,2})时然后,当我减去“2”时,它返回0。我如何解决这个问题?您是否也可以给出一个示例,说明如何将数组相加(而不是减法)?与减法不同的是,加法IMHO可能非常简单(请参见我的编辑),在一个单词中,加法(数组,值)=减法(数组,-值);好的-如果有特定的加法规则(我试图模拟电子对电子配置的加法),我会怎么做,因为我不想使用太多的if/else语句。例如,第一个元素最多可以包含2个值,第二个=2,第三个=6,第四个=2;第五个=6;…(s,p,d,f,…轨道)