程序正常运行,现在不再运行。
我首先在Visual Studio中编写了代码,问题出在Hackerrank.com,所以Hackerrank的编译器不想运行此代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
/*
* Complete the gradingStudents function below.
*/
static int[] gradingStudents(int[] grades) {
int fail = 40;
for (int i = 0; i < grades.Length; i++)
{
if (grades[i] < fail)
{
double failcheck = Math.Round((double)grades[i] / 5, MidpointRounding.AwayFromZero) * 5;
if (failcheck < fail)
{
Console.WriteLine(grades[i]);
}
else
{
Console.WriteLine(failcheck);
}
}
}
return grades;
}
static void Main(string[] args) {
TextWriter tw = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int n = Convert.ToInt32(Console.ReadLine());
grades = new int [n];
for (int gradesItr = 0; gradesItr < n; gradesItr++) {
int gradesItem = Convert.ToInt32(Console.ReadLine());
grades[gradesItr] = gradesItem;
}
int[] result = gradingStudents(grades);
tw.WriteLine(string.Join("\n", result));
tw.Flush();
tw.Close();
}
}
样本输入0
4
73
67
38
33
样本输出0
75
67
40
33
答案 0 :(得分:0)
如果您的问题是this, 然后根据hackerrank问题描述,应将方法gradingStudents更改为:
static int[] gradingStudents(int[] grades)
{
for (int i = 0; i < grades.Length; i++)
{
var item = grades[i];
if (item >= 38)
{
var diff = 5 - (item % 5);
if (diff < 3)
grades[i] = item + diff;
}
}
return grades;
}
答案 1 :(得分:0)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
/*
* Complete the gradingStudents function below.
*/
static int[] gradingStudents(int[] grades) {
for (int i = 0; i < grades.Length; i++)
{
if (grades[i] >= 38)
{
int nextMultiple = grades[i];
while (nextMultiple % 5 != 0)
{
nextMultiple++;
}
if(nextMultiple - grades[i] < 3)
{
grades[i] = nextMultiple;
}
}
}
return grades;
}
static void Main(string[] args) {
TextWriter tw = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int n = Convert.ToInt32(Console.ReadLine());
int[] grades = new int [n];
for (int gradesItr = 0; gradesItr < n; gradesItr++) {
int gradesItem = Convert.ToInt32(Console.ReadLine());
grades[gradesItr] = gradesItem;
}
int[] result = gradingStudents(grades);
tw.WriteLine(string.Join("\n", result));
tw.Flush();
tw.Close();
}
}