P5736 运行时错误
  • 板块工单反馈版
  • 楼主Mesmer
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/5/8 23:25
  • 上次更新2023/11/4 23:31:25
查看原帖
P5736 运行时错误
327499
Mesmer楼主2021/5/8 23:25

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;
        }
    }
}

2021/5/8 23:25
加载中...