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

使Deadfish游泳【难度:2级】:

答案1:

using System.Collections.Generic;

public class Deadfish
{
  public static int[] Parse(string program)
  {
    var output = new List<int>();
    
    int value = 0;
    
    foreach (var instruction in program)
    {
      switch (instruction)
      {
        case 'i':
          ++value;
          break;
        case 'd':
          --value;
          break;
        case 's':
          value *= value;
          break;
        case 'o':
          output.Add(value);
          break;
      }
    }
    
    return output.ToArray();
  }
}

答案2:

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

public class Deadfish
{
    public static int[] Parse(string data)
    {
        int outputIndex = 0, accumulator = 0;
        var outputArray = new int[data.Count(c => c == 'o')];
        Dictionary<char, Action> rules = new Dictionary<char, Action> {
            { 'i' , () => accumulator++ },
            { 'd' , () => accumulator-- },
            { 's' , () => accumulator*=accumulator },
            { 'o' , () => outputArray[outputIndex++] = accumulator }
        };
        foreach (var item in data) rules[item]();
        return outputArray;
    }
}

答案3:

using System.Collections.Generic;

public class Deadfish
{
  public static int[] Parse(string data)
  {
    int i = 0;
    List<int> result = new List<int>();
    
    foreach(char c in data)
    {
      if(c == 'i') i++;
      else if(c == 'd') i--;
      else if(c == 's') i*= i;
      else if(c == 'o') result.Add(i);      
    }
    return result.ToArray();
  }
}

答案4:

using System;
using System.Collections.Generic;

public class Deadfish
{
  public static int[] Parse(string data)
  {
  int total = 0;
    List<int> res = new List<int>();
    for(int i = 0; i< data.Length; i++)
    {
     if(data[i] == 'i'){ total += 1; }
     else if(data[i] == 'd'){ total -= 1;}
     else if(data[i] == 's'){ total = (int)Math.Pow(total,2);}
     else if(data[i] == 'o') {res.Add(total);}     
     else continue;
    }  
    return res.ToArray();
  }
}

答案5:

using System.Collections.Generic;

public class Deadfish
{
  public static int[] Parse(string data)
  {
    List<int> values = new List<int>();
    int value = 0;

    for (int i = 0; i < data.Length; i++)
    {
          char letter = data[i];                
          switch (letter)
          {
              case 'i':
                  value = value + 1;
                  break;
              case 'd':
                  value = value - 1;
                  break;
              case 's':
                  value = value * value;
                  break;                    
              case 'o':
                  values.Add(value);
                  break;
          }
      }            
      return values.ToArray();
    }
}

答案6:

using System.Collections.Generic;

public class Deadfish
{
  public static int[] Parse(string data)
  {
    List<int> values = new List<int>();
    
    int value1 = 0;
    
    foreach(char c in data)
    {
      switch(c)
      {
        case 'i' :
          // Increment the value
          value1++;
          break;
        case 'd' :
          // Decrement the value
          value1--;
          break;
        case 's' :
          // Square the value
          value1 *= value1;
          break;
        case 'o' :
          // Add value to array
          values.Add(value1);
          break;
        default:
          break;
      }
    }
    return values.ToArray();
  }
}

答案7:

public class Deadfish
{
  public static int[] Parse(string data)
        {
            int index = 0;
            int occ = 0;
            int iteration = 0;
            foreach (char c in data)
            {
                if(c == 'o')
                {
                    index++;
                }
            }
            int[] res = new int[index];

            foreach(char d in data)
            {
                if (d == 'i')
                {
                    occ++;
                }
                else if (d == 'd')
                {
                    occ--;
                }
                else if (d == 's')
                {
                    occ = occ * occ;
                }
                else if (d == 'o')
                {
                    res[iteration] = occ;
                    iteration++;
                }
            }

            return res;
        }
}

答案8:

using System;
using System.Collections.Generic;

public class Deadfish
{
  public static int[] Parse(string data){
    var arr = new List<int>();
    var r = 0;
    foreach(var x in data){
      switch(x){
        case 'i':
            r++;
          break;
        case 'd':
            r--;
          break;
        case 's':
            r = r * r;
          break;
        case 'o':
            arr.Add(r);
          break;
        default:
          break;
      }    
    }
    return arr.ToArray();
  }
}

答案9:

using System.Collections.Generic;
public class Deadfish
{
  public static int[] Parse(string data)
  {
    // Return the output array, and ignore all non-op characters
    
            
            List<int> res = new List<int>();
            bool add = false;
            int value = 0;
            foreach (char val in data)
            {
                switch (val)
                {
                    case 'i':
                        value++;
                        break;
                    case 'd':
                        value--;
                        break;
                    case 's':
                        value *= value;
                        break;
                    case 'o':
                        res.Add(value);
                        break;
                }
            }

            return res.ToArray();
  }
}

答案10:

using System;
using System.Collections.Generic;

public class Deadfish
{
  public static int[] Parse(string data)
        {
            char[] dataCommands = data.ToCharArray();
            int number = 0;
            List<int> output = new List<int>();
            
            for (int i=0; i<dataCommands.Length; i++)
            {
                if (dataCommands[i] == 'i') number++;
                else if (dataCommands[i] == 'd') number--;
                else if (dataCommands[i] == 's') number = number * number;
                else if (dataCommands[i] == 'o') output.Add(number);
            }
            
            return output.ToArray();
        }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值