C#练习题答案: 简单有趣#30:弦乐建设【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

简单有趣#30:弦乐建设【难度:1级】:

答案1:

namespace myjinxin {
    public class Kata {
        public int StringsConstruction(string A, string B)
        {
            int diff = B.Length;
            
            foreach (var c in A)
            { 
              int a = A.Length - A.Replace(c.ToString(), "").Length;
              int b = B.Length - B.Replace(c.ToString(), "").Length;
                          
              if (diff > b / a)
                  diff = b / a;
            }          
            return diff;
        }
    }
}

答案2:

namespace myjinxin {
    public class Kata {
        public int StringsConstruction( string A, string B ) {
            var a = new int[26];
            foreach ( var c in A ) {
                a[ c - 'a' ] += 1;
            }
            var b = new int[26];
            foreach ( var c in B ) {
                b[ c - 'a' ] += 1;
            }
            var count = 0;
            while ( true ) {
                for ( int i = 0; i < a.Length; i++ ) {
                    b[ i ] -= a[ i ];
                    if ( b[ i ] < 0 ) {
                        return count;
                    }
                }
                count += 1;
            }
        }
    }
}

答案3:

namespace myjinxin
{
  using System.Text.RegularExpressions;
  using System.Collections.Generic;
    
  public class Kata
  {
    public int StringsConstruction(string A, string B)
    {
      string s = Regex.Replace(B,  @"[^"+A+"]", "");
      var dict = new Dictionary<char, int>();
      foreach (var i in s)
      {
        if (dict.ContainsKey(i))
          dict[i] += 1;
        else
          dict.Add(i, 1);
      }
      int count = 0;
      bool check = true;
      while (check)
      {
        foreach (var i in A)
        {
          if (dict.ContainsKey(i) &amp;&amp; dict[i] > 0)
            dict[i] -= 1;
          else
            check = false;
        }
        if (check)
          count += 1;
      }
      return count;
    }
  }
}

答案4:

namespace myjinxin
{
    using System;   
        public class Kata
    {
        public int StringsConstruction(string A, string B)
        {
            int count = 0;
            string temp = "";
            bool check = true;
            int L = B.Length;
            while(check)
            {
                for(int i = 0; i < A.Length; i++ )
                {
                    for(int j = 0; j < L; j++)
                    {
                        if(A[i] == B[j])
                        {
                            temp += B[j];
                            B = B.Substring(0, B.IndexOf(B[j])) + B.Substring(B.IndexOf(B[j]) + 1);
                            j = L;                       
                        }
                    }
                    if(A == temp){ count++;temp = ""; i = A.Length; }
                    if (L == B.Length) check = false;
                    else L--;
                }
            }
            return count;
        }
    }
}

答案5:

namespace myjinxin
{
    using System;
    using System.Linq;
    public class Kata
    {
        public int StringsConstruction(string A, string B)
        {
            var arr = A.GroupBy(x=>x).ToArray();
            var count = new int[arr.Length];
            for (int i = 0; i < B.Length; i++)
            {
                var index = Array.FindIndex(arr, x => x.Key == B[i]);
                if (index >= 0) count[index]++;
            }
            return count.Select((x, i) => x / arr[i].Count()).Min();
        }
    }
}

答案6:

namespace myjinxin
{
   using System;
    
   public class Kata
   {
      public int StringsConstruction(string A, string B)
      {
          int iRemovals = 0;
          int iReturn = 0;
          int iTempLength = 0;
          int iIterations = B.Length / A.Length + 1;
          string sTemp = B;
          
          while (iIterations > 0)
          {
             foreach (char c in A)
             {    
                iTempLength = sTemp.Length;
                sTemp = RemoveOne(c, sTemp); 
                if (iTempLength != sTemp.Length)
                   iRemovals++;
             }
             if (iRemovals == A.Length)
             {            
                iReturn++;                
             }
             iRemovals = 0;
             iIterations--;
          }
          return iReturn;
      }
      
      public string RemoveOne(char cToRemove, string sStringToShrink)
      {
         int iFirstPos = sStringToShrink.IndexOf(cToRemove.ToString());
         if (iFirstPos < 0)
         {
            return sStringToShrink;
         }       
         return sStringToShrink.Substring(0, iFirstPos) + sStringToShrink.Substring(iFirstPos + 1);
      }      
   }
}

答案7:

namespace myjinxin
{
  using System;
  using System.Linq;
    
  public class Kata
  {
    public int StringsConstruction(string A, string B)
    {
       char[] stringA = A.ToCharArray();
       int loopCount = B.Length;
       int stringACount = 0;
       int solution = 0;

       for(int i = 0; i < loopCount; i++)
       {
         if(B.Contains(stringA[stringACount]))
         {
           B = B.Remove(B.IndexOf(stringA[stringACount]), 1);  
           if(stringACount == stringA.Length - 1)
           {
             solution++;
             stringACount = -1;
           }
         }
         else return solution;
        
         if(stringACount < stringA.Length - 1) stringACount++;
        
       }
            
       return solution;
     }
   }
}

答案8:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public int StringsConstruction(string A, string B){
            int numOccurences = 0;
            int searchPoint = 0;
            while(true)
            {
                if (B.Contains(A[searchPoint]))
                {
                    int pos = B.IndexOf(A[searchPoint]);
                    B = B.Remove(pos, 1);
                    if (searchPoint == A.Length - 1)
                    {
                        numOccurences++;
                        searchPoint = 0;
                    }
                    else {
                        searchPoint++;
                    }
                }
                else
                {
                    return numOccurences;
                }
            }
        }
    }
}

答案9:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Kata
    {
        public int StringsConstruction(string A, string B){
          //coding and coding..
          int min = Int32.MaxValue;
            var hashTable = A.Select(x => new KeyValuePair<char, int>(x, A.Count(c => c == x))).Distinct();
            foreach (var c in hashTable)
            {
                var current = B.Count(x => x == c.Key) / c.Value;
                min = current  < min ? current : min;
            }

            return min;         
        }
    }
}

答案10:

namespace myjinxin
{
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
    
    public class Kata
    {
        public int StringsConstruction(string A, string B){
          //coding and coding..
          
           Dictionary<char, int> frecA = A.ToCharArray() .GroupBy(x => x)
                    .ToDictionary(x => x.Key, x => x.Count());

            Dictionary<char, int> frecB = B.ToCharArray().GroupBy(x => x)
                    .ToDictionary(x => x.Key, x => x.Count());

            int min = int.MaxValue;
            foreach (KeyValuePair<char, int> kvp in frecA)
            {
                if (!frecB.ContainsKey(kvp.Key))
                {
                    return 0;
                }
                else
                {
                    if (frecB[kvp.Key] >= kvp.Value)
                    {
                        min = Math.Min(min, frecB[kvp.Key] / kvp.Value);
                    }
                    else
                    {
                        return 0;
                    }
                }

            }
            return min;
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值