这是窗体应用程序: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace L1241 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { long max = Convert.ToInt64(textBox1.Text); long[] SXH = Find(max); foreach (long i in SXH) { label2.Text = i + "\n"; } } public static long[] Find(long Max) { long[] array1 = new long[Max]; long index=0; for (long i = 100; i < Max; i++) { long[] array2 = divide(i); long sum = 0; for (long j = 0; j < array2.Length; j++) { sum += Convert.ToInt64(Math.Pow(array2[j], WeiShu(i))); } if (sum == i) { array1[index] = sum; index++; } } long[] array3 = new long[index]; for (long i = 0; i < index; i++) { array3[i] = array1[i]; } return array3; } private static long[] divide(long sum) { long[] array1 = new long[7]; long index = 0; while (sum != 0) { array1[index] = sum % 10; sum /= 10; index++; } long[] array2 = new long[index]; for (long i = 0; i < index; i++) { array2[i] = array1[i]; } return array2; } private static long WeiShu(long sum) { long i = 0; while (sum != 0) { i++; sum /= 10; } return i; } } } 求大神
其实就只是改了button按钮的程序代码,但是为什么就会结果不对呢?
label2.Text += i + "\n";
label2大小设置成自适应,否则换行后的内容你看不到了 或者改用richtextbox输出结果
这个不会啦,我已经把lable2设置成autoSIZE了不过还是感谢提出另一个方法
不过话说你的水仙花数代码写的也太麻烦了吧 public static void M() { Console.WriteLine("请输入一个上限值:(小于" + long.MaxValue + ")"); long MAX = Convert.ToInt64(Console.ReadLine()); foreach (long i in Find(MAX)) { Console.Write(i + ","); } } public static IEnumerable<long> Find(long Max) { if (Max < 100) throw new Exception("水仙花数位数必须>=3"); for (long i = 100; i < Max; i++) { string maxStr = i.ToString(); int n = maxStr.Length; long count = (long)maxStr.Select(m => Math.Pow(Convert.ToInt32(m.ToString()), n)).Sum(); if (count != i) continue; yield return i; } }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace L1241
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
long max = Convert.ToInt64(textBox1.Text);
long[] SXH = Find(max);
foreach (long i in SXH)
{
label2.Text = i + "\n";
} }
public static long[] Find(long Max)
{
long[] array1 = new long[Max];
long index=0;
for (long i = 100; i < Max; i++)
{
long[] array2 = divide(i);
long sum = 0;
for (long j = 0; j < array2.Length; j++)
{
sum += Convert.ToInt64(Math.Pow(array2[j], WeiShu(i)));
}
if (sum == i)
{
array1[index] = sum;
index++;
}
}
long[] array3 = new long[index];
for (long i = 0; i < index; i++)
{
array3[i] = array1[i];
}
return array3;
}
private static long[] divide(long sum)
{
long[] array1 = new long[7];
long index = 0;
while (sum != 0)
{
array1[index] = sum % 10;
sum /= 10;
index++;
}
long[] array2 = new long[index];
for (long i = 0; i < index; i++)
{
array2[i] = array1[i];
}
return array2;
}
private static long WeiShu(long sum)
{
long i = 0;
while (sum != 0)
{
i++;
sum /= 10;
}
return i;
}
}
}
求大神
或者改用richtextbox输出结果
public static void M()
{
Console.WriteLine("请输入一个上限值:(小于" + long.MaxValue + ")");
long MAX = Convert.ToInt64(Console.ReadLine());
foreach (long i in Find(MAX))
{
Console.Write(i + ",");
}
}
public static IEnumerable<long> Find(long Max)
{
if (Max < 100) throw new Exception("水仙花数位数必须>=3");
for (long i = 100; i < Max; i++)
{
string maxStr = i.ToString();
int n = maxStr.Length;
long count = (long)maxStr.Select(m => Math.Pow(Convert.ToInt32(m.ToString()), n)).Sum();
if (count != i) continue;
yield return i;
}
}