using System; using System.Collections.Generic; using System.Linq; using System.Text;…
- User
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Olimp2 {
class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] visota = Console.ReadLine().Split(' '); int Q = int.Parse(Console.ReadLine()); List<int> h = new List<int>(); for (int i = 0; i < N; i++) { h.Add(int.Parse(visota[i])); } List<double> result = new List<double>(); for (int i = 0; i <= Q; i++) { string[] photo = Console.ReadLine().Split(' '); int lt = int.Parse(photo[0]); int rt = int.Parse(photo[1]); double max = 0; double G = 0; for (int j = lt; j == rt; j++) { G += h[j]; } double M = 1 / rt - lt + 1 * G; double K = 0; for (int j = lt; j == rt; j++) { K += ((h[j] - 1) / (rt - lt + 1) * G) * ((h[j] - 1) / (rt - lt + 1) * G); } double D = 1 / rt - lt * K; double o = Math.Sqrt(D); double L = 0f; for (int j = lt; j == rt; j++) { L += ((h[j] - M) / o) * ((h[j] - M) / o) * ((h[j] - M) / o); } double a = 1 / (rt - lt) * L; if (a > max) { result.Add(max); } } for (int i = 0; i < Q; i++) { Console.WriteLine(result[i]); } Console.ReadLine(); } }} в чем ошибка?
- Assistant
В данном коде есть несколько ошибок:
-
В цикле
for (int j = lt; j == rt; j++)условие цикла должно бытьj <= rt, а неj == rt. Правильное условие позволит выполнить цикл для всех значенийjотltдоrt, включаяrt. -
В строке
double M = 1 / rt - lt + 1 * G;необходимо использовать скобки, чтобы правильно расставить приоритеты операций:double M = 1 / (rt - lt + 1) * G; -
В строке
double K = 0;необходимо инициализировать переменнуюK, чтобы избежать ошибки компиляции при дальнейшем использованииK += .... -
В строке
double D = 1 / rt - lt * K;необходимо использовать скобки, чтобы правильно расставить приоритеты операций:double D = 1 / (rt - lt) * K; -
В строке
result.Add(max);необходимо добавитьaв списокresult, а неmax:result.Add(a);
С учетом внесенных изменений, исправленный код будет выглядеть следующим образом:
using System; using System.Collections.Generic; namespace Olimp2 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] visota = Console.ReadLine().Split(' '); int Q = int.Parse(Console.ReadLine()); List<int> h = new List<int>(); for (int i = 0; i < N; i++) { h.Add(int.Parse(visota[i])); } List<double> result = new List<double>(); for (int i = 0; i < Q; i++) { string[] photo = Console.ReadLine().Split(' '); int lt = int.Parse(photo[0]); int rt = int.Parse(photo[1]); double max = 0; double G = 0; for (int j = lt; j <= rt; j++) { G += h[j]; } double M = 1.0 / (rt - lt + 1) * G; double K = 0; for (int j = lt; j <= rt; j++) { K += ((h[j] - 1) / (rt - lt + 1) * G) * ((h[j] - 1) / (rt - lt + 1) * G); } double D = 1.0 / (rt - lt) * K; double o = Math.Sqrt(D); double L = 0f; for (int j = lt; j <= rt; j++) { L += ((h[j] - M) / o) * ((h[j] - M) / o) * ((h[j] - M) / o); } double a = 1.0 / (rt - lt) * L; if (a > max) { max = a; } result.Add(max); } for (int i = 0; i < Q; i++) { Console.WriteLine(result[i]); } Console.ReadLine(); } } } -
- User
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Olimp2 {
class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] visota = Console.ReadLine().Split(' '); int Q = int.Parse(Console.ReadLine()); List<int> h = new List<int>(); for (int i = 0; i < N; i++) { h.Add(int.Parse(visota[i])); } List<double> result = new List<double>(); for (int i = 0; i <= Q; i++) { string[] photo = Console.ReadLine().Split(' '); int lt = int.Parse(photo[0]); int rt = int.Parse(photo[1]); double max = 0; double G = 0f; for (int j = lt; j < rt; j++) { G += h[j]; } double M = 1 / (rt - lt + 1) * G; double K = 0f; for (int j = lt; j < rt; j++) { K += ((h[j] - 1) / (rt - lt + 1) * G) * ((h[j] - 1) / (rt - lt + 1) * G); } double D = 1 / (rt - lt) * K; double o = Math.Sqrt(D); double L = 0f; for (int j = lt; j < rt; j++) { L += ((h[j] - M) / o) * ((h[j] - M) / o) * ((h[j] - M) / o); } double a = 1 / (rt - lt) * L; if (a > max) { max = a; result.Add(max); } } for (int i = 0; i < Q; i++) { Console.WriteLine(result[i]); } Console.ReadLine(); } }}
- Assistant
В данном коде есть следующие ошибки:
-
В цикле
for (int i = 0; i <= Q; i++), условие цикла должно бытьi < Q, а неi <= Q. Правильное условие позволит выполнить цикл для значенийiот 0 до Q-1. -
Переменная
maxдолжна быть объявлена и инициализирована за пределами циклаfor (int i = 0; i <= Q; i++). В текущей реализации, каждый раз когда переменнаяmaxприсваивается новое значение, оно сбрасывается на 0. -
Строка
result.Add(max);должна находиться за пределами циклаif (a > max)для того, чтобы добавитьmaxв результат только один раз после окончания проверок внутри цикла.
Исправленный код будет выглядеть следующим образом:
using System; using System.Collections.Generic; namespace Olimp2 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] visota = Console.ReadLine().Split(' '); int Q = int.Parse(Console.ReadLine()); List<int> h = new List<int>(); for (int i = 0; i < N; i++) { h.Add(int.Parse(visota[i])); } List<double> result = new List<double>(); for (int i = 0; i < Q; i++) { string[] photo = Console.ReadLine().Split(' '); int lt = int.Parse(photo[0]); int rt = int.Parse(photo[1]); double max = 0; double G = 0f; for (int j = lt; j < rt; j++) { G += h[j]; } double M = 1 / (rt - lt + 1) * G; double K = 0f; for (int j = lt; j < rt; j++) { K += ((h[j] - 1) / (rt - lt + 1) * G) * ((h[j] - 1) / (rt - lt + 1) * G); } double D = 1 / (rt - lt) * K; double o = Math.Sqrt(D); double L = 0f; for (int j = lt; j < rt; j++) { L += ((h[j] - M) / o) * ((h[j] - M) / o) * ((h[j] - M) / o); } double a = 1 / (rt - lt) * L; if (a > max) { max = a; } result.Add(max); } for (int i = 0; i < Q; i++) { Console.WriteLine(result[i]); } Console.ReadLine(); } } } -
- User
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Olimp2 {
class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] visota = Console.ReadLine().Split(' '); int Q = int.Parse(Console.ReadLine()); List<int> h = new List<int>(); for (int i = 0; i < N; i++) { h.Add(int.Parse(visota[i])); } List<double> result = new List<double>(); for (int i = 0; i <= Q; i++) { string[] photo = Console.ReadLine().Split(' '); int lt = int.Parse(photo[0]); int rt = int.Parse(photo[1]); double max = 0; double G = 0f; for (int j = lt; j < rt; j++) { G += h[j]; } double M = 1 / (rt - lt + 1) * G; double K = 0f; for (int j = lt; j < rt; j++) { K += ((h[j] - 1) / (rt - lt + 1) * G) * ((h[j] - 1) / (rt - lt + 1) * G); } double D = 1 / (rt - lt) * K; double o = Math.Sqrt(D); double L = 0f; for (int j = lt; j < rt; j++) { L += ((h[j] - M) / o) * ((h[j] - M) / o) * ((h[j] - M) / o); } double a = 1 / (rt - lt) * L; if (a > 0) { result.Add(a); } } for (int i = 0; i < Q; i++) { Console.WriteLine(result[i]); } Console.ReadLine(); } }}
- Assistant
В представленном коде внесены некоторые изменения. Корректировки произведены в следующих местах:
-
В цикле for(int i = 0; i <= Q; i++) - изменили условие цикла на i < Q, чтобы выполнить цикл для значений i от 0 до Q-1.
-
В строке "double G = 0f;" - убрали аффикс f, так как переменная G имеет тип double, и нет необходимости явно указывать тип.
-
В циклах for(int j = lt; j < rt; j++) - изменены условия циклов на j <= rt, чтобы выполнить цикл для значений j от lt до rt-1.
-
В условии if (a > 0) - добавлено условие, чтобы добавить в список result только положительные значения a.
Исправленный код будет выглядеть следующим образом:
using System; using System.Collections.Generic; namespace Olimp2 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] visota = Console.ReadLine().Split(' '); int Q = int.Parse(Console.ReadLine()); List<int> h = new List<int>(); for (int i = 0; i < N; i++) { h.Add(int.Parse(visota[i])); } List<double> result = new List<double>(); for (int i = 0; i < Q; i++) { string[] photo = Console.ReadLine().Split(' '); int lt = int.Parse(photo[0]); int rt = int.Parse(photo[1]); double max = 0; double G = 0; for (int j = lt; j <= rt; j++) { G += h[j]; } double M = 1.0 / (rt - lt + 1) * G; double K = 0; for (int j = lt; j <= rt; j++) { K += ((h[j] - 1) / (rt - lt + 1) * G) * ((h[j] - 1) / (rt - lt + 1) * G); } double D = 1.0 / (rt - lt) * K; double o = Math.Sqrt(D); double L = 0; for (int j = lt; j <= rt; j++) { L += ((h[j] - M) / o) * ((h[j] - M) / o) * ((h[j] - M) / o); } double a = 1.0 / (rt - lt) * L; if (a > 0) { result.Add(a); } } for (int i = 0; i < Q; i++) { Console.WriteLine(result[i]); } Console.ReadLine(); } } } -
- User
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Olimp2 {
class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); string[] visota = Console.ReadLine().Split(' '); int Q = int.Parse(Console.ReadLine()); List<int> h = new List<int>(); for (int i = 0; i < N; i++) { h.Add(int.Parse(visota[i])); } List<double> result = new List<double>(); for (int i = 0; i <= Q; i++) { string[] photo = Console.ReadLine().Split(' '); int lt = int.Parse(photo[0]); int rt = int.Parse(photo[1]); double G = 0f; for (int j = lt; j < rt; j++) { G += h[j]; } double M = 1f / (rt - lt + 1) * G; double K = 0f; for (int j = lt; j < rt; j++) { K += ((h[j] - 1) / (rt - lt + 1) * G) * ((h[j] - 1) / (rt - lt + 1) * G); } double D = 1f / (rt - lt) * K; double o = Math.Sqrt(D); double L = 0f; for (int j = lt; j < rt; j++) { L += ((h[j] - M) / o) * ((h[j] - M) / o) * ((h[j] - M) / o); } double a = 1 / (rt - lt) * L; if (a > 0) { result.Add(a); } } for (int i = 0; i < Q; i++) { Console.WriteLine(result[i]); } Console.ReadLine(); } }}
- Assistant
В представленном коде внесены некоторые корректировки. Ошибки, которые были исправлены:
- В цикле
for (int i = 0; i <= Q; i++)изменено условие цикла наi < Q, чтобы выполнить цикл для значенийiот 0 до Q-
- В цикле