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

Figurate数字#1 - 五角形数字【难度:2级】:

答案1:

using System;
public class Penta {
  public static Boolean pNum(long n) {
    return ((Math.Sqrt(24 * n + 1) + 1) / 6) % 1 == 0;
  }
  public static Boolean gpNum(long n) {
    return Math.Sqrt(24 * n + 1) % 1 == 0;
  }
  public static Boolean spNum(long n) {
    return Math.Sqrt(n) % 1 == 0 && pNum(n);
  }
}

答案2:

using System;
  public class Penta
    {
        public static Boolean pNum(long n)
        {
            int licznik = 2;
            long a = 1;
            while(a < n) a += licznik++ * 3 - 2;
            return a == n;
        }
            public static Boolean gpNum(long n)
        {
        if(n < 3) return true;
            int licznik = 2;
            long a = 1;
            while (a < n)
            {
                a += licznik++ * 3 - 2;
                if (a + licznik - 1 == n) return true;
            }
            return a == n;
        }
           public static Boolean spNum(long n)
        {
            if(pNum(n))
            {
                return (long)Math.Sqrt(n) * (long)Math.Sqrt(n) == n;
            }
            return false;
        }
    }

答案3:

using System;

public class Penta {

    private const double Tolerance = 0.000000000001;

    public static Boolean pNum( long n ) {
        if ( n == 0 ) {
            return false;
        }
        if ( !gpNum( n ) ) {
            return false;
        }
        var r = ( Math.Sqrt( 24*n + 1 ) + 1 )/6.0;
        return IsNatural( r );
    }

    public static Boolean gpNum( long n ) {
        if ( n == 0 ) {
            return true;
        }
        var t = 24*n + 1;
        return IsPerfectSquare( t );
    }

    public static Boolean spNum( long n ) {
        return pNum( n ) &amp;&amp; IsPerfectSquare( n );
    }

    private static bool IsPerfectSquare( long n ) {
        var sqrt = Math.Sqrt( n );
        return IsNatural( sqrt );
    }

    private static bool IsNatural( double n ) {
        return Math.Abs( ( n - ( int ) n ) ) < Tolerance;
    }

}

答案4:

using System;

public class Penta
{
        public static bool TestIt(Func<long, long> func,long n)
        {
            for (long i = 0; ; i++)
            {
                var r = func(i);
                if (r == n) return true;
                if (r > n) return false;
            }
        }
        public static bool pNum(long n) => n != 0 &amp;&amp; TestIt(i => i * (3 * i - 1) / 2, n);
        public static bool gpNum(long n) => TestIt(i => (long) (Math.Ceiling(i / 2.0) * Math.Ceiling((3 * i + 1) / 2.0) / 2), n);
        public static bool spNum(long n) => n != 0 &amp;&amp; TestIt(i => (long) Math.Floor(Math.Pow(Math.Sqrt(2) + Math.Sqrt(3), 8 * i - 4) / 96), n);
}

答案5:

using System;

public class Penta {
  
  public static Boolean pNum(long n) {
    double N = (Math.Sqrt(24 * n + 1) + 1) / 6;
    return N == (long)N;
  }
  public static Boolean gpNum(long n) {
    double N = Math.Sqrt(24 * n + 1);
    return N == (long)N;
  }
  public static Boolean spNum(long n) {
    if (pNum(n))
    {
      double N = Math.Sqrt(n);
      return N == (long)N;
    }
    else
      return false;
  }
}

答案6:

using System;

public class Penta {
  
  public static Boolean pNum(long n) {
    if(n<=0)
    {
     return false;
    }
    long dots = 1;
    long counter = 1;
    do
    {
        if(n==dots)
        {
          return true;
        }
        counter++;
        dots+=CalculateAddedDotsOnNLevel(counter);
   }
   while(n>=dots);
   
   return false;
    
  }
  
  private static long CalculateAddedDotsOnNLevel(long n)
  {
     return 3*n-2;
  }
  
  public static Boolean gpNum(long n) {
    if(pNum(n))
    {
      return true;
    }
    long dots=0;
    long counter = 1;
    do
    {
        if(n==dots)
        {
          return true;
        }
        counter++;
        long diff = CalculateAddedDotsOnNLevel(counter)-2;
        if(diff<0) diff=0;
        dots+=diff;
   }
   while(n>=dots);
    
    return false;
  }
  
  private static long CalculateDotsOfPentagonOnNLevel(long n)
  {
    if(n==0)
    {
      return 0;
    }
    return 5*(n-1);
  }
  
  public static Boolean spNum(long n) {
    if(!pNum(n))
    {
      return false;
    }
    
    return IsPerfectSquere(n);
  }
  
  public static Boolean IsPerfectSquere(long n)
  {
    var squere = Math.Sqrt(n);
    if(squere == Math.Truncate(squere))
    {
      return true;
    }
    return false;
  }
}

答案7:

using System;

public class Penta {
  public static Boolean pNum(long n) {
    return (1 + Math.Sqrt(1 + 24 * n)) / 6 == (int)((1 + Math.Sqrt(1 + 24 * n)) / 6);
  }
  
  public static Boolean gpNum(long n) {
    for(int i = 0; ; i++) {
      if((3 *i* i - i) / 2 == n || (3 *i* i + i) / 2 == n) return true;
      if((3 *i* i - i) / 2 > n) break;
    }
    return false;   
  }
  
  public static Boolean spNum(long n) {
    return pNum(n) &amp;&amp; Math.Sqrt(n) == (int)Math.Sqrt(n);
  }
}

答案8:

using System;

public class Penta {
  
  public static Boolean pNum(long n)
  {
    if(n < 1) return false;
    int i = 0;
    while(n > 0)
    {
      n = n - (i * 3 + 1);
      ++i;
    }
    return n == 0;
  }
  public static Boolean gpNum(long n)
  {
  // from wikipedia
    long square = 24*n + 1;
    long root = Convert.ToInt64(Math.Sqrt(square));
    return root * root == square;
  }
  public static Boolean spNum(long n)
  {
    long root = Convert.ToInt64(Math.Sqrt(n));
    if(pNum(n) &amp;&amp; root * root == n)
      return true;
    return false;
  }
}

答案9:

using System;

public class Penta {
  
  public static long[] dp = new long[10000000];
    
  public static Boolean pNum(long n) {
      dp[1] = 1;
      long factor = 1;
      for (int i = 2; i < dp.Length; i++)
      {
          dp[i] = dp[i-1] + factor + 3;
          factor += 3;
      }
      return n > 0 &amp;&amp; Array.IndexOf(dp, n) != -1;
  }
  public static Boolean gpNum(long n) {
      long factor = 1;
      for (int i = 1; i < dp.Length; i++)
      {
          if (n + factor == dp[i]) return true;
          factor += 2;
      }
      return n == 0 || n == 1 || pNum(n);
  }
  public static Boolean spNum(long n) {
      return pNum(n) &amp;&amp; Math.Sqrt(n) % 1 == 0;
  }
}

答案10:

using System;

public class Penta
{
    public static bool pNum(long n)
    {
        var x = (Math.Sqrt(24*n + 1) + 1)/6;
        return x % 1 == 0;
    }

    public static bool gpNum(long n)
    {
        var newN = n % 2 == 0 ? n / 2 : -n / 2;
        return n == 0 || pNum(n) || pNum(newN);
    }

    public static bool spNum(long n)
    {
        return pNum(n) &amp;&amp; Math.Sqrt(n) % 1 == 0;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值