Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] NegClosest = new int[2], PosClosest = new int[2];
- NegClosest[0] = int.MinValue;
- PosClosest[0] = int.MaxValue;
- Console.Write("Въведете големината на едномерен масив:");
- int n = int.Parse(Console.ReadLine());
- Console.Write("Въведете число, с което ще сравнявате:");
- int num = int.Parse(Console.ReadLine());
- int[] a = new int[n];
- Console.WriteLine("Въведете числата:");
- for (int i = 0; i < n; i++)
- {
- a[i] = int.Parse(Console.ReadLine());
- if (num - a[i] >= 0 && PosClosest[0] > num - a[i])
- {
- PosClosest[0] = num - a[i];
- PosClosest[1] = a[i];
- }
- else if (num - a[i] <= 0 && NegClosest[0] < num - a[i])
- {
- NegClosest[0] = num - a[i];
- NegClosest[1] = a[i];
- }
- }
- int closest;
- string pos = "";
- try
- {
- if (Math.Abs(NegClosest[0]) < PosClosest[0])
- {
- closest = NegClosest[1];
- }
- else closest = PosClosest[1];
- }
- catch (Exception)
- {
- closest = PosClosest[1];
- }
- for (int i = 0; i < n; i++)
- {
- if (closest == a[i])
- {
- pos += (i+1) + " | ";
- }
- }
- Console.WriteLine("Най-близкото число до {0} е {1}.", num, closest);
- Console.WriteLine("Намира се на позиции " + pos);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment