Найти минимум из положительных элементов вектора, расположенных правее первого элемента, равного нулю - C#
Формулировка задачи:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2
{
class Program
{
static void Main(string[] args)
{
string s;
double min = 1000000;
Console.WriteLine("Qty of elements");
s = Console.ReadLine();
int n = int.Parse(s);
int d = n;
double[] a = new double[n];
Console.WriteLine("Enter the number: ");
for (int i = 0; i < a.Length; i++)
{
if (double.TryParse(Console.ReadLine(), out a[i]) != true)
{
Console.WriteLine("Enter correct number");
}
else
{
if (a[i] == 0 && d == n)
{ d = i; }
{
for (i = 0; i < d; i++)
if (a[i] < min) min = a[i];
}
}
}
Console.WriteLine ("минимальный элемент = ",min);
}
}
}
Переделал вот так
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2
{
class Program
{
static void Main(string[] args)
{
string s;
int min = 1000000;
Console.WriteLine("Qty of elements");
s = Console.ReadLine();
int n = int.Parse(s);
int d = n;
int[] a = new int[n];
Console.WriteLine("Enter the number: ");
for (int i = 0; i < a.Length; i++)
{
if (int.TryParse(Console.ReadLine(), out a[i]) != true)
{
Console.WriteLine("Enter correct number");
}
else
{
if (a[i] == 0)
continue;
if (a[i] > 0 && a[i] < min)
min = a[i];
}
}
Console.WriteLine ("минимальный элемент = \t\n", min);
}
}
}Решение задачи: «Найти минимум из положительных элементов вектора, расположенных правее первого элемента, равного нулю»
textual
Листинг программы
using System;
namespace _2
{
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 4, 7, 6, 0, 3, 8, -5, 3, 0, 3, 5, 4 };
int firstZeroIndex = Array.IndexOf(a, 0);
if(firstZeroIndex < 0)
{
Console.WriteLine("Нет нулей!");
return;
}
int minIndex = firstZeroIndex + 1;
for (int i = minIndex+1; i < a.Length; i++)
{
if (a[i] < a[minIndex] & a[i] > 0)
minIndex = i;
}
Console.WriteLine("Минимальное после первого нуля: " + a[minIndex]);
}
}
}