C#练习题答案: 简单有趣#64:几乎递增序列【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客介绍了C#编程中的一个二级难度题目——几乎递增序列,提供了8种不同的解答方案,适合C#学习者进行基础练习和提升。
摘要由CSDN通过智能技术生成

简单有趣#64:几乎递增序列【难度:2级】:

答案1:

namespace myjinxin
{
    using System;  
     public class Kata
    {
        public bool AlmostIncreasingSequence(int[] s)
        {
          if (s.Length < 3) return true;
           for(int i = 0; i < s.Length - 1; i++)
            {
                if(s[i + 1] <= s[i])
                {
                     if (i > 0)
                    {
                        if (s[i + 1] <= s[i - 1])
                        {
                            if (i + 1 == s.Length - 1) return true;
                            else return false;
                        }
                    }
                    for(int j = i + 1; j < s.Length - 1; j++) if (s[j + 1] <= s[j]) return false;
                }
            }
            return true;
        }
    }
}

答案2:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    public class Kata
    {
        public bool AlmostIncreasingSequence(int[] sequence){
          Console.WriteLine($"{sequence[0]} {sequence[1]}");
          var flag = true;
          var index = 0; 
          for(int i=1;i<sequence.Length;i++)
             if(sequence[i]-sequence[i-1] <=0)
               if(flag) index = i!=sequence.Length-1?i-1:i;
          var arr =new List<int>();
          for(int i=0;i<sequence.Length;i++)
            if(i!=index) arr.Add(sequence[i]);
          for(int i=1;i<arr.Count;i++)
             if(arr[i]-arr[i-1] <=0) return false;
          return true;
        }
    }
}

答案3:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Kata
    {
        public bool AlmostIncreasingSequence(int[] sequence){
            
            bool answer = true;
            List<Int32> numbersList = sequence.ToList();

            for (int i = 0; i < numbersList.Count; i++)
            {

                numbersList.RemoveAt(i);

                for (int j = 1; j < numbersList.Count; j++)
                {

                    if (numbersList[j - 1] >= numbersList[j])
                    {
                        answer = false;
                        numbersList = sequence.ToList();
                        break;

                    }
                    else
                    {
                        answer = true;
                    }
                }

                if (answer == true)
                {
                    break;
                }             
            }

            return answer;
          
        }
    }
}

答案4:

namespace myjinxin
{
    using System;
    using System.Linq;
    public class Kata
    {
        public bool AlmostIncreasingSequence(int[] sequence){
          //coding and coding..
         int count = 0;
            // while (count <= 1) {
            for (int i = 0; i < sequence.Length - 1; i++)
            {
                if (i > 1 &amp;&amp; sequence[i + 1] <= sequence[i])
                {
                    if (sequence[i + 1] <= sequence[i-1])
                    {
                        var lst = sequence.ToList();//
                        lst.RemoveAt(i+1);
                        i = 0;
                        sequence = lst.ToArray();
                        count++;
                    }
                    else
                    {
                        var lst = sequence.ToList();//
                        lst.RemoveAt(i);
                        i = 0;
                        sequence = lst.ToArray();
                        count++;

                    }
                }

                if (sequence[i + 1] <= sequence[i])
                {
                    var lst = sequence.ToList();//
                    lst.RemoveAt(i);
                    i = -1;
                    sequence = lst.ToArray();
                    count++;
                }

            }
            //}
            if (count > 1)
                return false;
            else
                return true;
        }
    }
}

答案5:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Kata
    {
        public bool AlmostIncreasingSequence(int[] sequence)
        {
            return Enumerable.Range(0, sequence.Length).Any(i => IsIncreasing(RemoveAtIndex(sequence, i)));
        }

        IEnumerable<int> RemoveAtIndex(IEnumerable<int> sequence, int index)
        {
            return sequence.Where((x, i) => i != index);
        }

        private bool IsIncreasing(IEnumerable<int> sequence)
        {
            return sequence.Aggregate(
                new { Previous = int.MinValue, IsIncreasing = true },
                (previous, x) => { return new { Previous = x, IsIncreasing = previous.IsIncreasing &amp;&amp; x > previous.Previous }; }).IsIncreasing;
        }
    }
}

答案6:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public bool AlmostIncreasingSequence(int[] sequence){
             bool foundOne = false;

    for (int i = -1, j = 0, k = 1; k < sequence.Length; k++)
    {
        bool deleteCurrent = false;

        if (sequence[j] >= sequence[k])
        {
            if (foundOne)
            {
                return false;
            }
            foundOne = true;

            if (k > 1 &amp;&amp; sequence[i] >= sequence[k])
            {
                deleteCurrent = true;
            }
        }

        if (!foundOne)
        {
            i = j;
        }

        if (!deleteCurrent)
        {
            j = k;
        }
    }

    return true;
          
          
        }
    }
}

答案7:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    public class Kata
    {
public bool AlmostIncreasingSequence(int[] sequence){
  var seq=sequence.Select(x=>x).ToList();
  for(var i=0;i<seq.Count-1;i++) if(seq[i]>=seq[i+1]&amp;&amp;i<seq.Count-2){
    seq.RemoveAt(i);
    return seq.Where((x,j)=>j==seq.Count-1||x<seq[j+1]).Count()==seq.Count;
  }
  return true;
}  

    }
}

答案8:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public bool AlmostIncreasingSequence(int[] sequence)
        {
          var prev = sequence[0];
          var firstIncorrectFound = false;
          
          for (int i = 1; i < sequence.Length; i++)
          {
            var current = sequence[i]; 
            if (current - prev <= 0)
            {
              if (firstIncorrectFound) return false;
              else
              {
                firstIncorrectFound = true;
 
                if (i == 1) prev = current;
                else if (i > 1) 
                {
                  var beforePrev = sequence[i - 2]; 
                  
                  if (current > beforePrev) prev = current;
                  else prev = prev;
                }
              }
            }
            else
            {
              prev = current;
            }
          }
          
          return true;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值