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

皮皮数字!【难度:2级】:

答案1:

using System;
using System.Collections.Generic;
using System.Text;
using System.Numerics;

public class JomoPipi
{
  private static List<BigInteger> _numbers = new List<BigInteger> { 0, 1, 9, 3025 };
  
  static JomoPipi()
  {
    for (int i = 4; i <= 18; i++)
    {
      BigInteger sqri = i * i;
      
      for (int j = 1; j < i; j++)
      {
        sqri -= _numbers[j];
        sqri *= sqri;
      }
      
      _numbers.Add(sqri);
    }
  }
  
  public static BigInteger pipi(int n)
  {
    return _numbers[n];
  }
}

答案2:

using System;
using System.Collections.Generic;
using System.Text;
using System.Numerics;

public class JomoPipi 
{
    private static Dictionary<int,BigInteger> cache = new Dictionary<int,BigInteger>();

    public static BigInteger pipi(int n) 
    {  
       if(cache.ContainsKey(n))
       {
         return cache[n];
       }
       var b = new BigInteger(n);
       
       for(var i=1;i<n;i++)
       {       
         b *= b;
         b -= pipi(i);  
       }       
       
       var result = b * b;
       
       cache.Add(n, result);
       
       return result;
    }
}

答案3:

using System.Collections.Concurrent;
using System.Numerics;

public class JomoPipi {

    private static ConcurrentDictionary<int, BigInteger> _lookup = new ConcurrentDictionary<int, BigInteger>();        

    public static BigInteger pipi(int n) 
    {      
      BigInteger result;
      if (_lookup.TryGetValue(n, out result))
      {
        return result;
      }
      result = n;
      for (int i = 1; i < n; i++)
      {
        result = (result * result) - pipi(i);
      }
      result = result * result;
      _lookup.TryAdd(n, result);
      return result;
    }
}

答案4:

using System;
using System.Text;
using System.Numerics;
using System.Collections.Generic;
public class JomoPipi
{
        public static Dictionary<int, BigInteger> pipiN = new Dictionary<int, BigInteger>() { { 0, 0 } };

        public static BigInteger pipi(int n)
        {
            BigInteger p;
            if (!pipiN.TryGetValue(n, out p))
            {
                if (n == 0) return new BigInteger(0);
                p = new BigInteger(n);
                for (int i = 0; i < n; i++)
                {
                    p -= pipi(i);
                    p *= p;
                }         
                pipiN[n] = p;
            }
            return p;
        }
}

答案5:

using System.Numerics;
using System.Collections.Generic;

public class JomoPipi
{
    private static Dictionary<int, BigInteger> pipiNumberDict = new Dictionary<int, BigInteger>{
        {0, 0}
    };

    public static BigInteger pipi(int n)
    {
        BigInteger p;
        if (!pipiNumberDict.TryGetValue(n, out p))
        {
            p = new BigInteger(n);
            for (int i = 0; i < n; i++)
            {
                p -= pipi(i);
                p *= p;
            }
            pipiNumberDict[n] = p;
        }

        return p;
    }
}

答案6:

using System.Numerics;
using System.Collections.Generic;

public class JomoPipi
{
    private static Dictionary<int, BigInteger> pipiNumberDict = new Dictionary<int, BigInteger>{
        {0, 0}
    };

    public static BigInteger pipi(int n)
    {
        BigInteger p;
        if (!pipiNumberDict.TryGetValue(n, out p))
        {
            if (n == 0) return new BigInteger(0);
            p = new BigInteger(n);
            for (int i = 0; i < n; i++)
            {
                p -= pipi(i);
                p *= p;
            }
            pipiNumberDict[n] = p;
        }

        return p;
    }
}

答案7:

using System.Collections.Generic;
using System.Numerics;

public class JomoPipi {
    public static BigInteger pipi( int n ) {
        return GetNumber( n );
    }

    private static readonly Dictionary<int, BigInteger> Pipi = new Dictionary<int, BigInteger> {{0, 0}, {1, 1}, {2, 9}};

    private static BigInteger GetNumber( int n ) {
        if ( !Pipi.ContainsKey( n ) ) {
            BigInteger t = Square( Square( n ) - GetNumber( 1 ) );
            for ( int i = 2; i < n; i++ ) {
                t = Square( t - GetNumber( i ) );
            }
            Pipi [ n ] = t;
        }
        return Pipi [ n ];
    }

    private static BigInteger Square( BigInteger n ) {
        return BigInteger.Pow( n, 2 );
    }

}

答案8:

using System.Collections.Generic;
using System.Numerics;
public class JomoPipi
{
    private static List<BigInteger> lookup = new List<BigInteger>() { 0 };
    public static BigInteger pipi(int n)
    {
        if (lookup.Count > n) return lookup[n];
        BigInteger p = n;
        for(var j = 1; j <= n; ++j)
        {
          p -= pipi(j - 1);
          p *= p;
        }
        lookup.Add(p); // safe because algorithm always requests these in increasing order
        return p;
    }
}

答案9:

using System;
using System.Numerics;

public class JomoPipi {

    public static BigInteger pipi(int n) {
        return ps[n];
    }
    
    static readonly BigInteger[] ps = new BigInteger[19];
    static JomoPipi() {
        for (int i = 0; i < ps.Length; i++) {
            BigInteger p = i;
            for (int j = 0; j < i; j++)
                p = BigInteger.Pow(p - ps[j], 2);
            ps[i] = p;
        }
    }
}

答案10:

using System;
using System.Text;
using System.Numerics;
public class JomoPipi {
    public static BigInteger[] pipis=new BigInteger[]{0,1,9,3025,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    public static BigInteger pipi(int n) {
       if(n==0) return 0;
       if(JomoPipi.pipis[n]>0) return JomoPipi.pipis[n];
       var x=new BigInteger(n);
       var m=1;
       while(m<n){
         x=x*x-pipi(m);
         m++;
       }
       JomoPipi.pipis[n]=x*x;
       return x*x;
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值