Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. int[] values = {2,5,6,7,8,8,9};
  6.  
  7. int num = Convert.ToInt32(Console.ReadLine());
  8. SearchClosestValue(values,num);
  9.  
  10. Console.ReadKey();
  11. }
  12.  
  13. static int diff = 99999, index = -1;
  14.  
  15. static void SearchClosestValue(int[] values,int num)
  16. {
  17. foreach(int value in values)
  18. {
  19. if (value == num)
  20. {
  21. index = Array.IndexOf(values, value);
  22. }
  23. else
  24. {
  25. if (Math.Abs(num - value) < diff)
  26. {
  27. diff = Math.Abs(num - value);
  28. index = Array.IndexOf(values, value);
  29. }
  30. }
  31. }
  32.  
  33. Console.WriteLine(values[index]);
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement