C#练习题答案: 总和连续【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

总和连续【难度:2级】:

答案1:

using System;
using System.Collections.Generic;

public class Consecutives
{
  public static List<int> SumConsecutives(List<int> s) 
  {
      List<int> ret = new List<int>();
      int last = s[0], sum = 0;
      foreach (int i in s) {
          if (i == last) sum += last;
          else {
              ret.Add(sum);
              sum = last = i;
          }
      }
      ret.Add(sum);
      return ret;
  }

}

答案2:

using System.Collections.Generic;
using System.Linq;

public class Consecutives
{
  public static List<int> SumConsecutives(List<int> s) 
  {
      return s.Select((v, i) => (i > 0 &amp;&amp; s[i - 1] == v) ? (int?)null : s.Skip(i).TakeWhile(vi => vi == v).Sum()).Where(r => r.HasValue).Select(r => r.Value).ToList();
  }

}

答案3:

using System.Linq;
using System.Collections.Generic;
public class Consecutives
{
  public static List<int> SumConsecutives(List<int> s) 
  {
      var rs = new List<int>();
      for(int i=0;i<s.Count;i++){
        var tmp=new List<int>(new int[]{s[i]});
        while (i!=s.Count-1&amp;&amp;s[i]==s[i+1]){
          tmp.Add(s[i]);
          i++;
        }
        rs.Add(tmp.Sum());
      }
      return rs;
  }

}

答案4:

using System.Linq;
using System.Collections.Generic;

public class Consecutives
{
    public static List<int> SumConsecutives(List<int> s)
    {
        var stack = new Stack<int>(new[] { s[0] });

        for (int i = 1; i < s.Count; i++)
        {
            var sum = s[i];
            if (s[i] == s[i - 1])
            {
                sum += stack.Pop();
            }

            stack.Push(sum);
        }

        return stack.Reverse().ToList();
    }
}

答案5:

using System;
using System.Collections.Generic;

public class Consecutives
    {
        public static List<int> SumConsecutives(List<int> s)
        {
       
            List<int> Sumlist= s;
            for (int i = 0; i < Sumlist.Count; i++)
            {
                
                int k=0;
                bool flag = false;
                try
                {
                    while (Sumlist[i] == Sumlist[i + k + 1])
                    {
                        k++;
                        flag = true;
                        if ((i + k + 1) >= Sumlist.Count)
                            break;
                    }
                }
                catch
                { };
                if (flag)
                {
                    Sumlist[i] = Sumlist[i] * (k + 1);
                    Sumlist.RemoveRange(i+1,k);
                }
            }

            return Sumlist;
        }

    }

答案6:

using System.Collections.Generic;
public class Consecutives
{
  public static List<int> SumConsecutives(List<int> s)
  {
      if(s.Count == 0 || s.Count == 1) return s;
      List<int> ret = new List<int>();
      int cont = s[0]; int ant = s[0];
      for (int i = 1; i < s.Count; i++)
      {
          if(s[i]!=ant){
              ret.Add(cont);
              ant = s[i];
              cont = 0;
          }
          cont += s[i];
      }
      ret.Add(cont);
      return ret;
  }

}

答案7:

using System.Collections.Generic;

public class Consecutives
{
    public static List<int> SumConsecutives(List<int> s)
    {
        List<int> r = new List<int>(s.Count);
        int sum = s[0];
        for (int i = 1, currentElement = sum; i < s.Count; i++)
            if (currentElement == s[i])
                sum += currentElement;
            else
            {
                r.Add(sum);
                currentElement = s[i];
                sum = currentElement;
            }
        r.Add(sum);
        return r;
    }
}

答案8:

using System.Collections.Generic;
using System.Linq;

public class Consecutives
{
  private static List<int> _result;
  private static List<int> _temporaryItems;
  public static List<int> SumConsecutives(List<int> s) 
  {
    _result = new List<int>();
    _temporaryItems = new List<int>() { s.FirstOrDefault() };
    
    for (int i = 1; i < s.Count; i++)
    {
        if (IsSameAsLast(s[i]))
            AddItemToList(s[i]);
        else
            SaveItem(s[i]);
    }
    _result.Add(_temporaryItems.Sum());

    return _result;
  }
  
  private static bool IsSameAsLast(int value)
  {
    return _temporaryItems.FirstOrDefault() == value;
  }
  
  private static void AddItemToList(int value)
  {
    _temporaryItems.Add(value);
  }
  
  private static void SaveItem(int value)
  {
    _result.Add(_temporaryItems.Sum());
    _temporaryItems = new List<int>() {value};
  }
  
}

答案9:

using System;
using System.Collections.Generic;

public class Consecutives
{
  public static List<int> SumConsecutives(List<int> s) 
  {
      var listOfSame = new List<int>();
            int temp = s[0];
            for(var i = 0; i < s.Count - 1; i++)
            {

                if (s[i] == s[i + 1])
                    temp += s[i];
                if (s[i] != s[i + 1])
                {
                    listOfSame.Add(temp);
                    temp = s[i+1];
                }
                
                    
            }
            listOfSame.Add(temp);

            return listOfSame;
  }

}

答案10:

using System;
using System.Collections.Generic;

public class Consecutives 
{  //hey... the function has one (long) line;-)
  public static List<int> SumConsecutives(List<int> s) { List<int> n=new List<int>(); int a=0; s.Add(-111); for (int i=0;i<s.Count-1;i++) if (s[i]==s[i+1]) a+=s[i]; else { n.Add(a+s[i]); a=0; }; return n;}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值