C#练习题答案: 简单有趣#142:莫比乌斯功能【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客围绕C#编程练习题目——莫比乌斯功能展开,提供了10种不同的解答方案,适合C#初学者及进阶者提升技能。难度等级为2级,适合准备面试或自我提升的程序员挑战。
摘要由CSDN通过智能技术生成

简单有趣#142:莫比乌斯功能【难度:2级】:

答案1:

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

namespace myjinxin
{
public class Kata
{    
    public int Mobius(double n)
    {
      MobiusFunc mf = new MobiusFunc(n);
      return (!mf.IsSqrFreeValue(n)) ? 0
        : (mf.PrimeFactorsCount(n) % 2 == 0) ? 1 : -1;
    }
}
public class MobiusFunc
  {
    double n;
    public MobiusFunc(double n)
    {
      this.n = n;
    }
    public int PrimeFactorsCount(double n)
    {
      int count = 0;
      for (int b = 2; n > 1; b++)
        if (n % b == 0) {
          count++;
          while (n % b == 0)
            n /= b;
        }
      return count;
    }
    public bool IsSqrFreeValue(double n)
    {
      bool isSquareFree = true;
      for (double j = 2; j <= Math.Ceiling(Math.Sqrt(n)); j++)
        if (n % (j * j) == 0) {
          isSquareFree = false;
          break;
        }
      return isSquareFree;
    }
  }
}

答案2:

namespace myjinxin
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public class Kata
    {

        private static BitArray sieve = new BitArray(1000010, true);

        private static int NextPrime(int prev)
        {

            int next;
            if (prev == 2)
                next = 3;
            else
            {
                int pos = prev;
                do pos += 2; while (!sieve[pos]);
                next = pos;
            }

            if (next * 2 < sieve.Count &amp;&amp; sieve[next * 2])
            {
                for (int i = next * 2; i < sieve.Count; i += next)
                    sieve[i] = false;
            }
            return next;
        }

        static Kata()
        {
            sieve[0] = false;
            sieve[1] = false;
        }

        public int Mobius(double d)
        {
            long n = (long)d;
            long limit = (long)Math.Sqrt(n);
            long counts = 0;
            for(int prime = 2; prime <= limit &amp;&amp; n != 1; prime = NextPrime(prime))
            {
                int c = 0;
                while(n % prime == 0)
                {
                    n /= prime;
                    ++c;
                }
                if (c > 1)
                    return 0;

                if(c > 0)
                    counts += c;
            }
            if (n != 1)
                ++counts;

            return counts % 2 == 0 ? 1 : -1;
        }
    }
}

答案3:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public int Mobius(double n){
          
           int p = 0;
            
            if (n % 2 == 0)
            {
                n = n / 2;
                p++;
               
                if (n % 2 == 0)
                    return 0;
            }

             
            for (int i = 3; i <= Math.Sqrt(n);
                i = i + 2)
            {                
                if (n % i == 0)
                {
                    n = n / i;
                    p++;

                   
                    if (n % i == 0)
                        return 0;
                }
            }

            return (p % 2 == 0) ? -1 : 1;
        }
          
       
       
        
    }
}

答案4:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int Mobius(double n)
    {
      int p = 0;
      if (n % 2 == 0)
      {
        n = n / 2;
        p++;
        if (n % 2 == 0)
        {
          return 0;
        }
      }
      for (int i = 3; i <= Math.Sqrt(n); i = i + 2)
      {
        if (n % i == 0)
        {
          n = n / i;
          p++;
          if (n % i == 0)
          {
            return 0;
          }
        }
      }
      return (p % 2 == 0) ? -1 : 1;
    }

    }
}

答案5:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public bool IsPrime(long m) {
            if (m<=1) return false;
            if (m==2) return true;
            if (m%2==0) return false;
            for(long i = 3; i<Math.Sqrt(m)+1;i+=2){
                if(m%i == 0) return false;
            }
            
            return true;
        }
        
        public int Mobius(double x) {
          //coding and coding..
            int count = 0;
            long n = (long)x;
            if(IsPrime(n)) return -1;
            double sqrt = Math.Sqrt(n);
            for (long i=1; i<=sqrt;i++){                
                if(n%i != 0) continue;
                if(IsPrime(i)) {
                    count++;
                    if(n%(i*i) == 0) return 0;
                }
                if(IsPrime(n/i)) count++;
            }
            return count%2==0?1:-1;
            return 1;
          
        }
    }
}

答案6:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Kata
    {
        public int Mobius(double n1)
        {
            long n = (long)n1;
            long initial = n;
            int res = 0;

            List<long> primes = new List<long>();
            List<long> primeDivisors = new List<long>();
            primes.Add(2);
            int currentPrimeCounter = 0;

            while (n > 1)
            {
                currentPrimeCounter = 0;
                foreach (var p in primes)
                {
                    while (n % p == 0)
                    {
                        primeDivisors.Add(p);
                        n = n / p;
                        currentPrimeCounter++;
                    }
                    if (currentPrimeCounter > 1)
                    {
                        break;
                    }
                }

                if (n == 1 || currentPrimeCounter > 1)
                {
                    break;
                }

                long nextPrime = GetNextPrime(primes);
                primes.Add(nextPrime);
            }

            if(currentPrimeCounter <= 1)
            {
                if (primes.Last() == initial)
                {
                    res = -1;
                }
                else
                {
                    res = primeDivisors.Count % 2 == 0 ? 1 : -1;
                }
            }

            return res;
        }

        private long GetNextPrime(List<long> primes)
        {
            long currentMaxNr = primes.Last() + 1;
            long res = -1;

            while(res < 0)
            {
                foreach(var p in primes)
                {
                    if(currentMaxNr % p == 0)
                    {
                        res = -1;
                        break;
                    }

                    res = currentMaxNr;
                }
                currentMaxNr++;
            }

            return res;
        }
    }
}

答案7:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    
    public class Kata
    {
        public int Mobius(double n) {
          List<long> pfactors = new List<long>();
          long new_num = (long) n;
          for (long f = 2; f * f <= n; f++) {
            if (n % (f * f) == 0 &amp;&amp; IsPrime(f)) return 0;
            if (new_num % f == 0 &amp;&amp; IsPrime(f)) {
              pfactors.Add(f);
              while (new_num % f == 0) new_num = new_num / f;
              if (IsPrime(new_num)) {
                pfactors.Add(new_num);
                break;
              }
            }
          }
          return (pfactors.Count != 0 &amp;&amp; pfactors.Count % 2 == 0) ? 1 : -1;
        }
        
        public bool IsPrime(double num) {
          if (num <= 3) return true;
          if (num % 2 == 0 || num % 3 == 0) return false;
          for (int i = 5; i * i < num; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) return false;
          }
          return true;
        }
    }
}

答案8:

namespace myjinxin
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public class Kata
    {

        private static BitArray sieve = new BitArray(1000010, true);

        private static int NextPrime(int prev)
        {

            int next;
            if (prev == 2)
                next = 3;
            else
            {
                int pos = prev;
                do pos += 2; while (!sieve[pos]);
                next = pos;
            }

            if (next * next >= 0 &amp;&amp; next * next < sieve.Count &amp;&amp; sieve[next * next])
            {
                for (int i = next * next; i < sieve.Count; i += next)
                    sieve[i] = false;
            }
            return next;
        }

        static Kata()
        {
            sieve[0] = false;
            sieve[1] = false;
        }

        public int Mobius(double d)
        {
            long n = (long)d;
            long limit = (long)Math.Sqrt(n);
            long counts = 0;
            for(int prime = 2; prime <= limit &amp;&amp; n != 1; prime = NextPrime(prime))
            {
                int c = 0;
                while(n % prime == 0)
                {
                    n /= prime;
                    ++c;
                }
                if (c > 1)
                    return 0;

                if(c > 0)
                    counts += c;
            }
            if (n != 1)
                ++counts;

            return counts % 2 == 0 ? 1 : -1;
        }
    }
}

答案9:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    public class Kata
    {
        public int Mobius(double n)
        {
            int[] dzielniki = Pierwsza(n);
            if (dzielniki.Length == 0) return -1;
            foreach(int ele in dzielniki) if (n % (ele * ele) == 0) return 0;
            if (dzielniki.Length % 2 == 0) return 1;
            return -1;
        }
        private int[] Pierwsza(double n)
        {
            List<int> zestaw = new List<int>();
            int temp = (int)Math.Sqrt(n);
            while(temp > 1)
            {
                if (n % temp == 0)
                {
                    if (prw(temp)) zestaw.Add(temp);
                    if (prw((int)(n / temp))) zestaw.Add(temp);
                }
                temp--;
            }
            return zestaw.ToArray();
        }
        private bool prw(int n)
        {
            if (n < 2) return false;
            int temp = (int)Math.Sqrt(n);
            while (temp > 1)
            {
                if (n % temp == 0) return false;
                temp--;
            }
            return true;
        }
    }
}

答案10:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Kata
    {
        public int Mobius(double n){
            
            
             IDictionary<int, int> primeFactors = PrimeFactors(n);

            int numberOfPrimeFactors = primeFactors.Distinct().Count();

            // just one factor;
            if (primeFactors.Values.Any(p => p >= 2))
            {
                return 0;
            }
            else if (primeFactors.Keys.Count % 2 == 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        static IDictionary<int, int> PrimeFactors(double n)
        {
            Dictionary<int, int> primeFactors = new Dictionary<int, int>();
            for(int b = 2; b <= n;)
            {
                if(n % b == 0)
                {
                    if (primeFactors.ContainsKey(b))
                    {
                        primeFactors[b]++;
                    }
                    else
                    {
                        primeFactors.Add(b, 1);
                    }

                    n = n / b;
                    b = 2;
                }
                else
                {
                    b++;
                }
            }

            return primeFactors;
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值