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

模数系统【难度:2级】:

答案1:

using System;
using System.Linq;
public class ModSystem {
  public static string fromNb2Str(int n, int[] sys) {
    return   sys.Aggregate((a,b)=>a*b)<=n  ||   Enumerable.Range(2,sys.Max()-2).Count(x=>sys.Count(y=>y%x==0)>1)>0 
             ?       "Not applicable"      :    string.Concat(sys.Select(x=>"-"+n%x+"-"));
  }
}

答案2:

using System;
using System.Numerics;
public class ModSystem {
  
  public static String fromNb2Str(int num, int[] mArr) {
      string result = "-";
      BigInteger prod = 1;
      bool fCoprime = true;

      for(int i=0; i<mArr.Length; i++)
      {
          prod *= mArr[i];
          result += (num % mArr[i] + "--");
          for(int j=i+1; j<mArr.Length; j++)
          {
              if(Factorize(mArr[i],mArr[j]) != 1)
              {
                  fCoprime = false;
                  result = "Not applicable";
                  break;
              }
          }
          if(!fCoprime)
          {
              break;
          }
      }
      result = prod < num ? "Not applicable" : result.Substring(0,result.Length-1);
      return result;  
  }

 public static int Factorize(int a, int b)
  {
      int g = a == 0 ? b : a;
      return a == 0 || b == 0 ? g : Factorize(b, a % b);
  }


}

答案3:

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

public class ModSystem {
  
  public static String fromNb2Str(int n, int[] sys) {
    var result = "Not applicable";
    
    if (sys.OfType<int>().ToList().Aggregate((a,b)=>a*b) <= n) return result;
    
    for (var i = 0; i < sys.Length; i++)
      for (var j = i +1; j < sys.Length; j++)
        if (GetGCDByModulus(sys[i], sys[j]) > 1)
          return result;

    result = String.Join("", sys.OfType<int>().ToList().Select(x=>String.Format("-{0}-", n%x)));
    
    return result;
  }
  
  public static int GetGCDByModulus(int value1, int value2)
  {
    while (value1 != 0 &amp;&amp; value2 != 0)
    {
        if (value1 > value2)
            value1 %= value2;
        else
            value2 %= value1;
    }
    return Math.Max(value1, value2);
  }
  
}

答案4:

using System;

public class ModSystem { 
  private static bool coprime(int a, int b) {
     if (b == 0) {
       return a == 1; 
    } else {
      return coprime(b, a % b);  
    }
  }
  
  public static String fromNb2Str(int n, int[] sys) {
    int p = 1;
    foreach (int v in sys) {
        p *= v;
    }
    if (p <= n) {
        return "Not applicable";
    }
    for( int i = 0; i < sys.Length; i++ ) {
       for ( int j = i+1; j < sys.Length; j++) {
         if (!coprime(sys[i],sys[j]) ) {
             return "Not applicable";
        }
      }
    }
    String res = "";
    for( int i = 0; i < sys.Length; i++ ) {
        res = res + "-" + n % sys[i] + "-";
    }
    return res;
  }
}

答案5:

using System.Linq;

public class ModSystem
{
    private static readonly string NA = "Not applicable";
    private static int GCF(int x, int y) => (y == 0) ? x : GCF(y, x % y);
    public static string fromNb2Str(int n, int[] sys)
    {
        // Throw out invalid input
        if (n < 0 || sys == null || sys.Length == 0) return NA;
        if (sys.Aggregate(1, (acc, cur) => acc * cur) < n) return NA;
        for (var i = 0; i < sys.Length; i++)
            for (var j = i + 1; j < sys.Length; j++)
                if (GCF(sys[i], sys[j]) > 1) return NA;
                
        return string.Concat(sys.Select(num => $"-{n % num}-"));
    }
    
}

答案6:

using System;
using System.Linq;
public class ModSystem 
{
      public static String fromNb2Str(int n, int[] sys)
        {
            int modRazem = 1;
            for (int i = 0; i < sys.Length; i++) modRazem *= sys[i];
            if (modRazem < n) return "Not applicable";
            int mini = sys.Min();
            for(int i = mini; i > 1; i--)
            {
                int check = 0;
                for(int j = 0; j < sys.Length; j++)
                {
                    if (sys[j] % i == 0) check++;
                }
                if(check > 1) return "Not applicable";
            }
            int[] rns = sys.Select(x => n % x).ToArray();
            string res = "-";
            res += string.Join("--", rns.Select(x => "" + x));
            return res + "-";
        }
}

答案7:

public class ModSystem {
  public static string fromNb2Str(int n, int[] sys)
  {
      bool isGreaterN = false;
      bool isPairwiseCoPrime = false;
      int temp = 1;

      foreach(int cur in sys) { temp *= cur; }
      isGreaterN = temp < n ? false : true;
      if (!isGreaterN) return "Not applicable";

      for (int i = 0; i < sys.Length; i++)
          for (int j = i + 1; j < sys.Length; j++)
              if (GCD(sys[i], sys[j]) == 1) isPairwiseCoPrime = true;
              else return "Not applicable";
      if (!isPairwiseCoPrime) return "Not applicable";

      string res = "-";

      for(int i = 0; i < sys.Length; i++)
          res += n % sys[i] + "--";

      return res.Remove(res.Length - 1);
  }
  public static int GCD(int leftNum, int rightNum)
  {
      while (rightNum != 0)
      {
          int tmp = leftNum % rightNum;
          leftNum = rightNum;
          rightNum = tmp;
      }
      return leftNum;
  } 
}

答案8:

using System;
using System.Linq;

public class ModSystem {
  
        public static String fromNb2Str(int n, int[] sys)
        {
            if (!IsValidSystem(n, sys))
                return "Not applicable";

            int[] modulos = new int[sys.Length];
            for (int x = 0; x < sys.Length; x++)
                modulos[x] = n % sys[x];

            return string.Join("", modulos.Select(i => $"-{i}-"));
        }

        public static bool IsValidSystem(int n, int[] sys)
        {
            var product = sys.Aggregate((x, y) => x * y);
            if (n >= product)
                return false;

            for (int indexx = 0; indexx < sys.Length; indexx++)
                for (int indexy = indexx + 1; indexy < sys.Length; indexy++)
                    if (GCD(sys[indexx], sys[indexy]) != 1) return false;
            return true;
        }

        public static int GCD(int a, int b)
        {
            while (a != 0 &amp;&amp; b != 0)
            {
                if (a > b)
                {
                    a %= b;
                }
                else
                {
                    b %= a;
                }
            }
            return a == 0 ? b : a;
        }
}

答案9:

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

public class ModSystem {
  
  public static String fromNb2Str(int n, int[] sys) {
      StringBuilder sb = new StringBuilder();
      if(!IsPairwiseCoprimeSystem(sys) || Product(sys) <= n)
      {
        return "Not applicable";
      }
      else 
      {
        var strParts = sys.Select(m => $"-{n%m}-");
        foreach(string part in strParts)
        {
          sb.Append(part);
        }
      }
      return sb.ToString();
  }
  
  public static bool IsPairwiseCoprimeSystem(int[] sys)
  {
    HashSet<int> moduliCheckedForCoprime = new HashSet<int>();
    foreach(int currentModuli in sys)
    {
      moduliCheckedForCoprime.Add(currentModuli);
      
      var otherModuli = sys.Where(m => !moduliCheckedForCoprime.Contains(m));
      if(otherModuli.Select(m => AreCoprimes(currentModuli,m)).Where(cp => !cp).Any())
      {
        return false;
      }
    }
    return true;
  }
  
  public static bool AreCoprimes(int num1,int num2)
  {
    int largest = num1 > num2 ? num1 : num2;
    for(int i = 2; i <= (largest/2)+1; i++)
    {
      if((num1 % i) == 0 &amp;&amp; (num2 % i) == 0)
      {
        return false;
      }
    }
    return true;
  }
  
  public static int Product(int[] sys)
  {
    return sys.Aggregate((a,b) => a*b);
  }
}

答案10:

using System;
using System.Collections.Generic;
using System.Linq;
public class ModSystem {
  
  public static String fromNb2Str(int n, int[] sys)
        {
            string output = "";
            int check1 = sys[0];
            for(int i = 1;i<sys.Length;i++)
            {
                check1 *= sys[i];
            }
            if(n >= check1)
            {
                return "Not applicable";
            }
            List<int> CoPrime = new List<int>();
            foreach(int i in sys)
            {
                for (int j = 2; j <= i; j++)
                {
                    if(i % j == 0)
                    {
                        CoPrime.Add(j);
                    }
                }
            }
            int check2 = CoPrime.Count;
            CoPrime = CoPrime.Distinct().ToList();
            int check3 = CoPrime.Count;
            if(check2!=check3)
            {
                return "Not applicable";
            }
            foreach(int i in sys)
            {
                output += "-" + n%i + "-";
            }
            return output;
        }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值