c# 除第一个外全部运行时错误 执行时间0ms,占用资源0b
看不到测试内容输入,如果是我的代码有问题,回复任意即可
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class Program
{
[STAThread]
private static void Main(string[] args)
{
Console.ReadLine();
string question = Console.ReadLine();
string[] input = question.Split(' ');
int[] value = new int[input.Length];
StringBuilder builder = new StringBuilder();
for (int index = 0; index < input.Length; index++)
{
value[index] = int.Parse(input[index]);
if (IsA(value[index]))
{
builder.Append($"{(builder.Length > 0 ? " " : "")}{value[index]}");
}
}
Console.WriteLine(builder.ToString());
Console.ReadLine();
}
private static bool IsA(float value)
{
if (value == 2f)
return true;
else if(value < 2f || value % 2 == 0)
return false;
int sqrt = (int)Math.Sqrt(value);
for (int index = 3; index <= sqrt; index += 2)
if (value % sqrt == 0f)
return false;
return true;
}
}
}