C#练习题答案: 数字哪个其位数的权力的总和相同数量【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

数字哪个其位数的权力的总和相同数量【难度:1级】:

答案1:

using System.Collections.Generic;

public class Sumpowdig {
    
    public static long[] EqSumPowDig(long hmax, int exp) 
    {
        List<long> numbers = new List<long>();
        for (long i = 2; i <= hmax; i++) if ( digPowSum (i, exp) == i ) numbers.Add(i);
        return numbers.ToArray();
    }
    
    private static long digPowSum(long num, int exp) 
    {
        long sum = 0;
        while(num > 0)
        {
            sum += (long) System.Math.Pow( (num % 10), exp );
            num /= 10;
        }
        return sum;
    }
}

答案2:

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

public class Sumpowdig
{
    public static IEnumerable<long> EqSumPowDig(long hmax, int exp)
    {
        for (long i = 2; i <= hmax; i++)
        {
            if (i.ToString().Sum(c => Math.Pow(int.Parse(c.ToString()), exp)) == i)
            {
                yield return i;
            }
        }
    }
}

答案3:

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

public class Sumpowdig {
    
    public static long[] EqSumPowDig(long hmax, int exp) 
    {
        var r = new List<long>();
        for (long i = 2; i <= hmax; i++)
            if (i.ToString().ToList().Sum(c => Math.Pow(long.Parse(c.ToString()), exp)) == i) r.Add(i);
        return r.ToArray();
    }
}

答案4:

using System;
using System.Linq;
using System.Collections.Generic;
public class Sumpowdig {
    
    public static long[] EqSumPowDig(long hmax, int exp) 
    {
        List<long> a = new List<long>();
        for(int i=2; i<=hmax; i++){
          if((""+i).ToCharArray().Sum(c=>Math.Pow((int)Char.GetNumericValue(c),exp))==i){
            a.Add(i);
          }
        }
        return a.ToArray();
    }
}

答案5:

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

public class Sumpowdig {
    
    public static long[] EqSumPowDig(long hmax, int exp) 
    {
        List<long> l = new List<long>();
        for (int i = 2; i <= hmax;i++)
        {
          if (i.ToString().ToCharArray().Sum(x => Math.Pow(x-'0',exp)) == i)
            l.Add(i);
        }
        return l.ToArray();
    }
}

答案6:

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

public class Sumpowdig {
  public static Int64[] EqSumPowDig(Int64 HMax, Int32 Exp) 
  {
    var result = new List<Int64>();
    
    for (var index = 2; index <= HMax; index++){
      var digits = index.ToString().ToCharArray().Select(Digit => Int32.Parse(Digit.ToString()));
      var sum = digits.Sum(Digit => Math.Pow(Digit, Exp));
      if (sum == index){
        result.Add(index);
      }
    }
    
    return result.ToArray();
  }
}

答案7:

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

public class Sumpowdig
{
    public static long[] EqSumPowDig(long hmax, int exp)
    {
      List<long> result = new List<long>();
      for(long i = 153; i <= hmax; ++i)
        if(i == i.ToString().ToCharArray().Select(c => Char.GetNumericValue(c)).Sum(n => Math.Pow(n, exp)))
          result.Add(i);
          
      return result.ToArray();
    }
}

答案8:

using System;
using System.Linq;
using System.Collections.Generic;
public class Sumpowdig {
    
    public static long[] EqSumPowDig(long hmax, int exp) 
    {
        return BruteForce(hmax, exp).ToArray();
    }
    
    private static IEnumerable<long> BruteForce(long hmax, int exp)
    {
      var nums = Enumerable.Range(0, 10).Select(d => (long)Math.Pow(d, exp)).ToArray();
      for (long i = 2; i <= hmax; i++)
      {
        if (i.ToString().Sum(d => nums[d - '0']) == i)
        {
          yield return i;
        }
      }
    }
}

答案9:

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

public class Sumpowdig {
    public static long[] EqSumPowDig( long hmax, int exp ) {
        var result = new Stack<long>( );
        for ( long n = hmax; n > 1; n-- ) {
            var digits = Convert.ToString( n ).Select( d => d - '0' ).ToArray( );
            if ( digits.Sum( d => Math.Pow( d, exp ) ) == n ) {
                result.Push( n );
            }
        } 
        return result.ToArray( );
    }
}

答案10:

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

public class Sumpowdig
{
    public static long[] EqSumPowDig(long hmax, int exp) 
    {
        var list = new List<long>();
        for (var l = 2L; l <= hmax; l++)
        {
            var sumPower = l.ToString().Select(c => (long)Math.Pow(char.GetNumericValue(c), exp)).Sum();
            if (l == sumPower) list.Add(l);
        }
        return list.ToArray();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值