.将大于整数m且小于n的k个素数存入数组x。请编写函数num()实现程序的要求
解决方案 »
- 两个gridview控件联动的问题
- log4Net如何记录详细的变量信息?
- GridView+ObjectDataSource分页问题
- 【100分】如何显示WMI创建的远程进程的窗口
- txt文本读出后二进制的问题
- codeporject的速度很慢怎么处理?
- 如何在WebBrowser控件中屏蔽鼠标“右键”
- 用WMI如何使远程主机重新启动?
- WinForm中想实现当单击最小化按钮后,整个窗体以图标的形式缩小到右下角托盘中双击托盘的图标后恢复原窗体,怎么做?
- 一启动程序抓到错误,提示后,如何不再让程序继续运行,马上关闭
- dotnetbar 里的 chart X轴能否用字符串 另外Y轴如果float格式的话,再侧边的刻度那里就全变成浮点刻度了,如何手动调整刻度
- C#如何利用itextSharp 设置一个PDF文字 背景颜色
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace T392508289
{
class Program
{
static IEnumerable<int> Primes()
{
yield return 2;
int i = 3;
while (true)
{
if (Enumerable.Range(2, i - 2).All(x => i % x != 0))
yield return i;
i++;
}
} static void num(ref int[] x, int m, int n)
{
if (m >= n) return;
x = Primes().SkipWhile(_ => _ <= m).TakeWhile(_ => _ < n).ToArray();
} static void Main(string[] args)
{
Console.WriteLine("m:");
int m = int.Parse(Console.ReadLine());
Console.WriteLine("n:");
int n = int.Parse(Console.ReadLine());
int[] x = default(int[]);
num(ref x, m, n);
for (int i = 0; i < x.Length; i++)
Console.WriteLine(x[i]);
}
}
}
{
static void Main(string[] args)
{
int[] x = num(2, 40, 10); x.ToList().ForEach(p => Console.WriteLine(p));
Console.Read();
} static int[] num(int m,int n,int k)
{
List<int> result = new List<int>(); for (int i = m; i <= n; i++)
{
if (i == 2 || Math.Pow(2, i) % i == 2)
{
result.Add(i);
}
} return result.ToArray();
}
}
{
List<int> result = new List<int>(); for (int i = m; i <= n && k > 0; i++)
{
if (i == 2 || Math.Pow(2, i) % i == 2)
{
k--;
result.Add(i);
}
} return result.ToArray();
}
{
List<int> value= new List<int>();
for (int i = m,int j= 0; i <= n && j < k; i++)
{
if (i == 2)
{
j++;
value.Add(i);
}
else
{
for(int m = 2 ;m<i;++m)
{
if(i%m!=0&&i==(m+1))
{
j++;
value.Add(i);
}
}
}
}
}//算法很垃圾,但是很“底层”