C#练习题答案: 路线减少【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这是一篇关于C#编程的练习题目,难度为3级,主要探讨如何减少路线。文章提供了10个不同的解答方案,适合C#初学者和进阶者提升技能。
摘要由CSDN通过智能技术生成

路线减少【难度:3级】:

答案1:

using System;
using System.Collections.Generic;

public class DirReduction {
    public static String[] dirReduc(String[] arr) {
        Stack<String> stack = new Stack<String>();

        foreach (String direction in arr) {
            String lastElement = stack.Count > 0 ? stack.Peek().ToString() : null;

            switch(direction) {
                case "NORTH": if ("SOUTH".Equals(lastElement)) { stack.Pop(); } else { stack.Push(direction); } break;
                case "SOUTH": if ("NORTH".Equals(lastElement)) { stack.Pop(); } else { stack.Push(direction); } break;
                case "EAST":  if ("WEST".Equals(lastElement)) { stack.Pop(); } else { stack.Push(direction); } break;
                case "WEST":  if ("EAST".Equals(lastElement)) { stack.Pop(); } else { stack.Push(direction); } break;
            }
        }
        String[] result = stack.ToArray();        
        Array.Reverse(result);
        
        return result;               
    }
}

答案2:

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

public class DirReduction {
  
    public static string[] dirReduc(String[] arr) {
        Dictionary<string, string> oppositeOf = new Dictionary<string, string>()
        {
            {"NORTH", "SOUTH"},
            {"SOUTH", "NORTH"},
            {"EAST", "WEST"},
            {"WEST", "EAST"}
        };
    
        List<string> betterDirections = new List<string>();
        foreach (var direction in arr)
        {
            if (betterDirections.LastOrDefault() == oppositeOf[direction])
            {
                betterDirections.RemoveAt(betterDirections.Count - 1);
            }
            else
            {
                betterDirections.Add(direction);
            }
        }
        return betterDirections.ToArray();
    }
}

答案3:

  using System.Collections.Generic;
  
  public class DirReduction
  {
    public static string[] dirReduc(string[] arr)
    {
      var list = new List<string>();
      list.AddRange(arr);

      for (var i = 0; i < list.Count - 1; i++)
      {
        if ((list[i] == Dir.E &amp;&amp; list[i + 1] == Dir.W)
          || (list[i] == Dir.W &amp;&amp; list[i + 1] == Dir.E)
          || (list[i] == Dir.N &amp;&amp; list[i + 1] == Dir.S)
          || (list[i] == Dir.S &amp;&amp; list[i + 1] == Dir.N))
        {
          list.RemoveRange(i, 2);
          i = -1;
        }
      }

      return list.ToArray();
    }

    private static class Dir
    {
      public static string N = "NORTH",
        E = "EAST",
        W = "WEST",
        S = "SOUTH";
    }
  }

答案4:

using System.Linq;
using System.Text.RegularExpressions;
public class DirReduction {
  
    public static string[] dirReduc(string[] arr) {
        string s=new string(arr.Select(x=>x[0]).ToArray());
        while (Regex.Match(s,"NS|EW|SN|WE").Success) s=Regex.Replace(s,"NS|EW|SN|WE","");
        return s.Select(x=>x=='N' ? "NORTH" : x=='S' ? "SOUTH" : x=='E' ? "EAST" : "WEST").ToArray();
    }
}

答案5:

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

public class DirReduction {
  
  const string NORTH = "NORTH";
        const string SOUTH = "SOUTH";
        const string EAST = "EAST";
        const string WEST = "WEST";

        public static string[] dirReduc(String[] arr)
        {
            LinkedList<string> result = new LinkedList<string>();

            foreach (string dir in arr)
            {
                if (result.Last!=null &amp;&amp; result.Last.Value.Equals(getOpposite(dir)))
                {
                    result.RemoveLast();                
                }
                else
                {
                    result.AddLast(dir);
                }
            }
            return result.ToArray();
        }
        public static string getOpposite(string dir)
        {
            switch (dir)
            {
                case NORTH: return SOUTH;
                case SOUTH: return NORTH;
                case EAST: return WEST;
                case WEST: return EAST;
            }
            return "";
        }
}

答案6:

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

public class DirReduction {
  
            public static string[] dirReduc(string[] arr)
        {
            var stack = new Stack<string>();

            foreach (var s in arr)
            {
                PushAndReduce(stack, s);
            }

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

        private static void PushAndReduce(Stack<string> stack, string s)
        {
            if (!stack.Any() || !AreOposite(stack.Peek(), s))
                stack.Push(s);
            else
                stack.Pop();
        }

        private static bool AreOposite(string peek, string s)
        {
            switch (peek)
            {
                case "NORTH":
                    return s == "SOUTH";
                case "SOUTH":
                    return s == "NORTH";
                case "EAST":
                    return s == "WEST";
                case "WEST":
                    return s == "EAST";
            }
            throw new ArgumentException();
        }


}

答案7:

using System.Collections.Generic;
using System;
using System.Linq;
public class DirReduction {
  
    public static string[] dirReduc(String[] arr) {
    
     List<string> arList = arr.ToList();
             for (int i = 1; i < arList.Count ; i++)
            {
                if (
                    (arList[i] == "SOUTH" &amp;&amp; arList[i - 1] == "NORTH")
                    ||
                    (arList[i] == "NORTH" &amp;&amp; arList[i - 1] == "SOUTH")
                    ||
                    (arList[i] == "EAST" &amp;&amp; arList[i - 1] == "WEST")
                    ||
                    (arList[i] == "WEST" &amp;&amp; arList[i - 1] == "EAST")
                    )
                {
                    arList.RemoveRange(i - 1, 2);                    
                    i = 0;
                }
            }           

            return arList.ToArray();
    }
}

答案8:

using System;
using System.Collections.Generic;
public class DirReduction 
{  
        public static string[] dirReduc(String[] arr)
        {
            int compares = 0;
            List<string> list = new List<string>(arr);
            for (int i = 0; i < list.Count; i++)
            {
                if (list.Count - compares == 1)
                    return list.ToArray();

                if (AreOpposite(list[i], list[i + 1]))
                {
                    list.RemoveAt(i);
                    list.RemoveAt(i);
                    i = -1;
                    compares = 0;
                }
                else
                    compares++;                           
            }
            return list.ToArray();
        }
        
        static bool AreOpposite(string a, string b)
        {
            return (a == "NORTH" &amp;&amp; b == "SOUTH") || (b == "NORTH" &amp;&amp; a == "SOUTH") || (a == "EAST" &amp;&amp; b == "WEST") || (a == "WEST" &amp;&amp; b == "EAST") ? true : false;
        }
}

答案9:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class DirReduction {

    private static string WEST = "WEST";
    private static string EAST = "EAST";
    private static string SOUTH = "SOUTH";
    private static string NORTH = "NORTH";
    
    public static string[] dirReduc(String[] arr) {
    List<string> reducList = new List<string>();
            for (int i = 0; i < arr.Length; i++)
            {
                if(i+1 == arr.Length)
                {
                    reducList.Add(arr[i]);
                    continue;
                }

                if ((arr[i] == WEST &amp;&amp; arr[i + 1] == EAST) || (arr[i] == EAST &amp;&amp; arr[i + 1] == WEST)
                    || (arr[i] == SOUTH &amp;&amp; arr[i + 1] == NORTH) || (arr[i] == NORTH &amp;&amp; arr[i + 1] == SOUTH))
                {
                    i++;
                }
                else
                {
                    reducList.Add(arr[i]);
                }
            }

            if (arr.Length != reducList.Count())
                return dirReduc(reducList.ToArray());

            return reducList.ToArray();
    }
}

答案10:

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

public class DirReduction 
{
    public static string[] dirReduc(string[] arr) 
    {
        var s = string.Join("-", arr);
        var l = 0;
        
        while(l != s.Length)
        {
            l = s.Length;
            s = s.Replace("NORTH-SOUTH", string.Empty)
                 .Replace("SOUTH-NORTH", string.Empty)
                 .Replace("EAST-WEST", string.Empty)
                 .Replace("WEST-EAST", string.Empty)
                 .Replace("--", "-")
                 .Trim('-');
        }
        
        return s.Split('-');
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值