标签:练习题 数字 C# List System long new using public
取一个数字并将它的数字总和提升到连续的力量和… Eureka !!【难度:2级】:
答案1:
using System;
using System.Collections.Generic;
using System.Linq;
public class SumDigPower {
public static long[] SumDigPow(long a, long b)
{
List<long> values = new List<long>();
for (long x = a; x <= b; x++)
{
if (x.ToString().Select((c, i) => Math.Pow(Char.GetNumericValue(c), i + 1)).Sum() == x)
values.Add(x);
}
return values.ToArray();
}
}
答案2:
using System;
using System.Linq;
using System.Collections.Generic;
public class SumDigPower {
public static long[] SumDigPow(long a, long b)
{
List<long> r = new List<long>();
for(long l = a; l<=b; l++){
int n = 1;
long sum = 0;
foreach(char c in (""+l)){
sum+=(long)Math.Pow(Int64.Parse(""+c), n);
n++;
}
if(sum==l){
r.Add(l);
}
}
return r.ToArray();
}
}
答案3:
using System;
using System.Collections.Generic;
public class SumDigPower {
public static long[] SumDigPow(long a, long b)
{
if (b < a)
return new long[0];
List<long> result = new List<long>();
for (long i = a; i <= b; i++)
{
if (IsEureka(i))
result.Add(i);
}
return result.ToArray();
}
public static bool IsEureka(long num)
{
int digitCount = 0;
long i = num;
while (i > 0)
{
digitCount++;
i /= 10;
}
if (digitCount == 1)
return true;
long sum = 0;
i = num;
while (i > 0)
{
sum += (long)Math.Pow(i % 10, digitCount--);
if (sum > num)
return false;
i /= 10;
}
return sum == num;
}
}
答案4:
using System;
using System.Collections.Generic;
using System.Linq;
public class SumDigPower
{
public static long[] SumDigPow(long a, long b)
{
List<long> values = new List<long>();
for(; a <= b; a++)
{
var digits = a.ToString().Select(x => int.Parse(x.ToString()));
long rslt = 0;
int itrs = 1;
foreach(var digit in digits)
{
rslt += (long)Math.Pow(digit, itrs++);
}
if (rslt == a)
values.Add(rslt);
}
return values.ToArray();
}
}
答案5:
using System;
using System.Linq;
using System.Collections.Generic;
public class SumDigPower {
public static long[] SumDigPow(long a, long b)
{
List<long> ls = new List<long>();
for (long n = a; n <= b; n++) //remember <= since it's inclusive
{
var digitsPow = n.ToString() //seperate digits
.Select(x => x-'0') //convert chars into ints
.Select((x, i) => Math.Pow(x, i+1)) //raise ints to power of digit position
.Sum();
if (digitsPow == n) ls.Add(n);
}
return ls.ToArray();
}
}
答案6:
using System;
public class SumDigPower {
public static long[] SumDigPow(long a, long b)
{
long[] result={};
int rL=0;
for(long i=a;i<=b;i++)
{
var S = i.ToString ().ToCharArray ();
long I = 0;
for(int n=0;n<S.Length;n++){
int Sn = S [n]-48;
I = I + (long)(Math.Pow(Sn,n+1));
if (I > i) {
break;
}
}
if (I == i) {
Array.Resize (ref result, ++rL);
result [rL-1] = i;
}
}
return result;
}
}
答案7:
using System.Collections.Generic;
using System.Linq;
using System;
public class SumDigPower {
public static long[] SumDigPow(long a, long b)
{
List<long> found = new List<long>();
List<long> numbers = new List<long>();
for (long l = a; l <= b; l++)
numbers.Add(l);
numbers.AsParallel().ForAll(num =>
//numbers.ForEach(num =>
{
long temp = num;
double sum = 0;
List<int> digits = new List<int>();
while (temp / 10 > 0)
{
digits.Add((int)(temp % 10));
temp /= 10;
}
digits.Add((int)(temp % 10));
for (int l = 0; l < digits.Count; l++)
sum += Math.Pow(digits[l], digits.Count - l);
if (sum == num)
lock (found)
found.Add(num);
});
return found.ToArray();
}
}
答案8:
using System.Collections.Generic;
using System;
public class SumDigPower {
public static long[] SumDigPow(long a, long b)
{
var result = new List<long>();
for (long i = a; i < b; i++)
{
double temp = 0;
var arrayStringNum = i.ToString().ToCharArray();
for (int j = 0; j < arrayStringNum.Length; j++)
{
var tempNum = int.Parse(arrayStringNum[j].ToString());
temp += Math.Pow(tempNum, j + 1);
}
if (temp == i)
result.Add(i);
}
return result.ToArray();
}
}
答案9:
using System;
using System.Linq;
using System.Collections.Generic;
public class SumDigPower {
public static long[] SumDigPow(long a, long b)
{
if(a > b)
return new long[1];;
var list = new List<long>();
foreach(var number in Enumerable.Range((int)a, (int)b-(int)a))
{
var numbers = $"{number}".ToCharArray().Select(p => int.Parse($"{p}"));
int powerIndex = 0;
var sum = numbers.Sum(p => Math.Pow(p, ++powerIndex));
if(sum == number)
list.Add(Convert.ToInt64(number));
}
return list.ToArray<long>();
}
}
答案10:
using System;
using System.Collections.Generic;
using System.Linq;
public class SumDigPower {
public static long[] SumDigPow(long a, long b)
{
long start = a > b ? b : a;
long end = a > b ? a : b;
List<long> eurekas = new List<long>();
for (long i = start; i <= end; i++) {
if (isEureka(i))
eurekas.Add(i);
}
return eurekas.ToArray();
}
private static bool isEureka(long n) {
char[] digits = n.ToString().ToCharArray();
long eureka = 0;
for (int i = 0; i < digits.Length; i++) {
eureka += (long)Math.Pow(Convert.ToDouble(digits[i].ToString()), i+1);
}
return n == eureka;
}
}
标签:练习题,数字,C#,List,System,long,new,using,public 来源: https://blog.csdn.net/weixin_45444821/article/details/100846183
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。