
2个回答
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int min, max, i, j, n;
bool flag;
Console.WriteLine(" 素数 \n");
Console.WriteLine(" 输入范围的下限(不小于2)");
min = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(" 输入范围的上限");
max = Convert.ToInt16(Console.ReadLine());
n = 0; //初始化素数的个数为0
for (i = min; i <= max;i++ ) //i为min到max之间的所有自然数
{
flag = true; //设置为真
for (j = 2; j <= Math.Sqrt(i);j++ ) //j为2到i的平方根之间的自然数
{
if (i % j == 0) //如果i能被j整除
{
flag = false; //设置标记为假
break; //退出内循环
}
}
if (!flag) //如果标记为假
{ continue; } // 退出本次循环
Console.Write("{0,6}",i); //输出素数
n++; //素数递增
if(n%10==0) //没输出十个素数换一次行
{
Console.WriteLine();
}
}
Console.WriteLine("\n {0}到{1}之间的素数共有{2}",min,max,n);
Console.ReadLine();
}
}
}
汗,素数和质数是一个意思
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int min, max, i, j, n;
bool flag;
Console.WriteLine(" 素数 \n");
Console.WriteLine(" 输入范围的下限(不小于2)");
min = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(" 输入范围的上限");
max = Convert.ToInt16(Console.ReadLine());
n = 0; //初始化素数的个数为0
for (i = min; i <= max;i++ ) //i为min到max之间的所有自然数
{
flag = true; //设置为真
for (j = 2; j <= Math.Sqrt(i);j++ ) //j为2到i的平方根之间的自然数
{
if (i % j == 0) //如果i能被j整除
{
flag = false; //设置标记为假
break; //退出内循环
}
}
if (!flag) //如果标记为假
{ continue; } // 退出本次循环
Console.Write("{0,6}",i); //输出素数
n++; //素数递增
if(n%10==0) //没输出十个素数换一次行
{
Console.WriteLine();
}
}
Console.WriteLine("\n {0}到{1}之间的素数共有{2}",min,max,n);
Console.ReadLine();
}
}
}
汗,素数和质数是一个意思
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |