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

将跟踪【难度:3级】:

答案1:

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

public class KPrimes {
    
    public static long[] CountKprimes(int k, long start, long end) 
    {
        List<int> primes = new List<int>(new[] { 2 });
        List<long> kPrimes = new List<long>();
        
        Enumerable.Range(3, 10000).ToList().ForEach(num =>
        {
            if (!primes.Any(x => num % x == 0))
            {
                primes.Add(num);
            }
        });
      
        Enumerable.Range((int)start, (int)end - (int)start + 1).ToList().ForEach(num =>
        {
            int fac = num;
            int count = 1;
            for (int i = 0; count <= k &amp;&amp; i < primes.Count &amp;&amp; primes[i] < fac; i++)
            {
                if (fac % primes[i] == 0)
                {
                    fac /= primes[i--];
                    count++;   
                }
            }
          
            if (count == k)
            {
                kPrimes.Add(num);
            }
        });
      
      return kPrimes.ToArray();
    }
      
    public static int Puzzle(int s) 
    {
        var primes_7 = CountKprimes(7, 128, s).ToList();
        var primes_3 = CountKprimes(3, 8, s - primes_7.Min()).ToList();
        var primes_1 = CountKprimes(1, 2, s - primes_7.Min() - primes_3.Min()).ToList();
        int i = 0;
        foreach (int c in primes_7)
            foreach (int b in primes_3)
                foreach (int a in primes_1)
                    i += a + b + c == s ? 1 : 0;
    
        return i;
    }
}

答案2:

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

public class KPrimes {
    
        public static int Puzzle(int s)
        {
            var _1prime = CountKprimes(1, 2, s);
            var _3prime = CountKprimes(3, 8, s);
            var _7prime = CountKprimes(7, 128, s);
            int n = 0;

            if (_1prime.Min() + _3prime.Min() + _7prime.Min() > s) return n;
                
            for(int i=0; i< _7prime.Length  ;i++)
            {
                if (_7prime[i] + _3prime[0] + _1prime[0] > s) break;
                if (_7prime[i] > s) break;

                for(int j=0;  ; j++)
                {
                    if (_7prime[i] + _3prime[j] + _1prime[0] > s) break;
                    long diff = s - _7prime[i] - _3prime[j];
                    if (_1prime.Contains(diff)) n++;
                    
                }
            }
            return n;
        }

        static List<long> primes = new List<long> { 2, 3 };

        public static long[] CountKprimes(int k, long start, long end)
        {
            
            if (start < 1) start = 1;

            List<long> kPrime = new List<long>();
            for (long i = start; i <= end; i++)
            {
                long j = i;
                for (int l = 1; l <= k; l++)
                {
                    long factor = GetPrimeFactors(j);
                    if (factor == 0) break;
                    j = j / factor;
                    if (j == 1)
                    {
                        if (l == k) kPrime.Add(i);
                        break;
                    }
                }
            }
            return kPrime.ToArray();
        }


        static long GetPrimeFactors(long n)
        {
            //if (n == 1) return;

            foreach (var p in primes.Where(x=> x <= n/2)) if (n % p == 0) return p;
            

            long l = (long)Math.Sqrt(n)+1;
            for (long i = primes[primes.Count() - 1] + 2; i <= l; i+=2) 
            {
                if (IsPrime(i))
                {
                    primes.Add(i);
                    if (n % i == 0) return i;
                }
            }
            if (IsPrime(n))
                return n;
            return 0;
        }

        static bool IsPrime(long i)
        {
            if (i == 0) return false;
            if (i == 1) return false;
            if (i == 2) return true;
            if (i % 2 == 0) return false;

            for (long j = 3; j <= Math.Sqrt(i); j += 2)
            {
                if (i % j == 0) return false;
            }
            
            return true;
        }
}

答案3:

using System;
using System.Collections.Generic;

public class KPrimes {

    private static int PrimeFactors(long n) 
    {
        List<long> factors = new List<long>();
        for (long i = 2; i * i <= n; i++) 
        {
            while (n % i == 0) 
            {
                factors.Add(i);
                n /= i;
            }
        }
        if (n > 1) factors.Add(n);
        return factors.Count;
    }
    public static long[] CountKprimes(int k, long start, long end) 
    {
        List<long> kprimes = new List<long>();
        for (long i = start; i <= end; i++)
            if (PrimeFactors(i) == k)
                kprimes.Add(i);
        long[] result = kprimes.ToArray();
        return result;
    }
    public static int Puzzle(int s) 
    {
        long[] a = CountKprimes(1, 0, s);
        long[] b = CountKprimes(3, 0, s);
        long[] c = CountKprimes(7, 0, s);
        int cnt = 0;
        int ia = 0;
        while (ia < a.Length) 
        {
            int ib = 0;
            while (ib < b.Length) 
            {
                int ic = 0;
                while (ic < c.Length) 
                {
                    if (a[ia] + b[ib] + c[ic] == s)
                        cnt++;
                    ic++;
                }
                ib++;
            }
            ia++;
        }
        return cnt;
    }
}

答案4:

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

public class KPrimes {
    public static long[] CountKprimes( int k, long start, long end ) {
        var result = new List<long>( );
        for ( var i = start; i <= end; i++ ) {
            if ( GetFactorsCount( i ) == k ) {
                result.Add( i );
            }
        }
        return result.ToArray( );
    }

    public static int Puzzle( int s ) {
        var primes1 = CountKprimes( 1, 2, s );
        var primes3 = CountKprimes( 3, 2, s );
        var primes7 = CountKprimes( 7, 2, s );
        return
            ( from p1 in primes1
                from p2 in primes3
                where p1 + p2 < s
                from p3 in primes7
                where p1 + p2 + p3 == s
                select p1 ).Count( );
    }

    private static int GetFactorsCount( long n ) {
        var result = 0;
        long factor = 2;
        while ( factor*factor <= n ) {
            while ( n%factor == 0 ) {
                result += 1;
                n /= factor;
            }
            factor += 1;
        }
        if ( n > 1 ) {
            result += 1;
        }

        return result;
    }
}

答案5:

using System;
using System.Collections.Generic;

public class KPrimes {
    public static int KPrime(long n) {
        int k = 1;
        long p = 2;

        while (n >= (p * p)) {
            if ((n % p) == 0) {
                k++;
                n /= p;
            } else {
                p++;
            }
        }

        return k;
    }
    
    public static long[] CountKprimes(int k, long start, long end) {
        var list = new List<long>();

        for (long n = start; n <= end; n++) {
            if (KPrime(n) == k)
                list.Add(n);
        }

        return list.ToArray();
    }
    
    public static int Puzzle(int s) {
        var one = CountKprimes(1, 2, s);
        var three = CountKprimes(3, 2, s);
        var seven = CountKprimes(7, 2, s);

        int solutions = 0;
        foreach (var a in one) {
            foreach (var b in three) {
                foreach (var c in seven) {
                    if (a + b + c == s)
                        solutions++;
                }
            }
        }

        return solutions;
    }
}

答案6:


using System.Collections.Generic;
using System.Linq;
public class KPrimes {

    public static long[] CountKprimes(int k, long start, long end)
        {
            List<long> result = new List<long>();
            while (start <= end)
            {
                result.Add(start);
                start++;
            }
            return result.Where(x => Find_k(x) == k).ToArray();
        }
        public static int Puzzle(int s)
        {
            var a = CountKprimes(1, 0, s);
            var b = CountKprimes(3, 0, s);
            var c = CountKprimes(7, 0, s);
            var result = 0;
            foreach(var aa in a)
            {
                foreach(var bb in b)
                {
                    foreach(var cc in c)
                    {
                        if (aa + bb + cc == s)
                            result++;
                    }
                }
            }
            return result;
        }
        public static int Find_k(long n)
        {
            var res = 0;
            var i = 2;
            while (i * i <= n)
            {
                while (n % i == 0)
                {
                    n /= i;
                    res += 1;
                }
                i += 1;
            }
            if (n > 1) res += 1;
            return res;
        }
}

答案7:

using System.Collections.Generic;
using System;

public class KPrimes {

    public static long[] CountKprimes(int k, long start, long end) 
    {
        List<long> kPrimes = new List<long>();
        for (long i = start; i <= end; i++) {
          bool isKPrime = CalcPrimeFactors(i).Count == k;
          if (isKPrime) kPrimes.Add(i);
        }
        return kPrimes.ToArray();
    }
    
    private static List<long> CalcPrimeFactors(long number) {
      long remainder = number;
      List<long> primeFactors = new List<long>();
      for (long i = 2; i <= Math.Ceiling(Math.Sqrt(number)); i++) {
        while (remainder % i == 0) {
          primeFactors.Add(i);
          remainder /= i;
        }
        if (remainder < i) break;
      }
      if (remainder > 1) primeFactors.Add(remainder);
      return primeFactors;
    }
    
    public static int Puzzle(int s) 
    {
        const long minOnePrime = 2;
        const long minThreePrime = 8;
        const long minSevenPrime = 128;
        long[] possibleAValues = CountKprimes(1, minOnePrime, s-minSevenPrime-minThreePrime);
        long[] possibleBValues = CountKprimes(3, minThreePrime, s-minSevenPrime-minOnePrime);
        long[] possibleCValues = CountKprimes(7, minSevenPrime, s-minThreePrime-minOnePrime);
        int solutionCount = 0;
        foreach (long a in possibleAValues) {
          foreach (long b in possibleBValues) {
            foreach (long c in possibleCValues) {  
              if (a + b + c == s) solutionCount++;
            }
          }
        }
        return solutionCount;
    }
}

答案8:

using System;
using System.Collections.Generic;
public class KPrimes {
   private static int PrimeFactors(long n) 
    {
        List<long> factors = new List<long>();
        for (long i = 2; i * i <= n; i++) 
        {
            while (n % i == 0) 
            {
                factors.Add(i);
                n /= i;
            }
        }
        if (n > 1) factors.Add(n);
        return factors.Count;
    }
  public static long[] CountKprimes(int k, long start, long end) 
    {
      List<long> list=new List<long>();
      for(long i=start;i<=end;i++)
      {
        if(PrimeFactors(i)==k)
          list.Add(i);
      }
      return list.ToArray();
    }
    public static int Puzzle(int s) 
    {
      long[] a=CountKprimes(1,1,s);
      long[] b=CountKprimes(3,1,s);
      long[] c=CountKprimes(7,1,s);
      long num=0;
      int count1=0;
      for(int i=0;i<a.Length;i++)
      {
        for(int j=0;j<b.Length;j++)
        {
          for(int k=0;k<c.Length;k++)
            {
              num=a[i]+b[j]+c[k];
              if(num==s)
              count1++;
            }
        }
      }
      return count1;
    }
}

答案9:

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

public class KPrimes {
    private static bool IsKPrime(int k, long number)
    {
        int primes = 0;
        for (int p = 2; p * p <= number &amp;&amp; primes < k; ++p)
        {
            while (number % p == 0 &amp;&amp; primes < k)
            {
                number /= p;
                ++primes;
            }
        }
        if (number > 1)
        {
            ++primes;
        }
        return primes == k;
    }

    public static long[] CountKprimes(int k, long start, long end) {

        // Store for kprimes
        var kPrimes = new List<long>();
        for(int i = 0; i < end - start + 1; i++)
        {
            if (IsKPrime(k, start + i))
                kPrimes.Add(start + i); 
        }
            
        
        return kPrimes.ToArray();
    }

    public static int Puzzle(int s) 
    {
        const byte MIN_KPRIME_7 = 128;
        const byte MIN_KPRIME_3 = 8;
        const byte MIN_KPRIME_1 = 2;

        // Return zero if less than minimum realizable number
        if (s < MIN_KPRIME_7 + MIN_KPRIME_3 + MIN_KPRIME_1)  
            return 0;

        // Get the three sets of KPrimes for the given value
        var kPrimes1 = CountKprimes(1, 0, s);   
        var kPrimes3 = CountKprimes(3, 0, s);   
        var kPrimes7 = CountKprimes(7, 0, s);

        var count = 0;
        foreach (var kPrime7 in kPrimes7)
        {              
            for (int i = 0; i < kPrimes3.Length &amp;&amp; s >= (kPrime7 + kPrimes3[i] + MIN_KPRIME_1); i++) 
            {
                if (kPrimes1.Contains(s - kPrime7 - kPrimes3[i]))
                {
                    count++;
                }
            }
        }

        return count;
    }
}

答案10:

using System.Linq;

public class KPrimes {
        public static long[] CountKprimes(int k, long start, long end)
        {
            long[] numbers = new long[end - start + 1];
            for (long n = start; n <= end; n++)
                numbers[n-start] = n;

            return (from num in numbers
                    where getKprime(num) == k
                    select num).ToArray();
        }

        private static int getKprime(long n)
        {
            if (n == 0)
                return 0;
            if (n == 1)
                return 1;

            for (int i = 2; i <= System.Math.Sqrt(n); i++)
                if (n % i == 0)
                    return getKprime(i) + getKprime(n / i);
            return 1;
        }

        public static int Puzzle(int s)
        {
            int puzzle = 0;
            for (int k = 2; k < s; k++)
                if (getKprime(k) == 1)
                    for (int j = 2; j < s; j++)
                        if ((s - j - k) > 1 &amp;&amp; getKprime(j) == 3 &amp;&amp; getKprime(s-j-k) == 7)
                            puzzle++;
            return puzzle;
        }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值