fork download
  1. using System;
  2.  
  3. class Program
  4. {
  5. static int Key(int x)
  6. {
  7. if (x == 0)
  8. {
  9. return int.MaxValue;
  10. }
  11. return Math.Abs(x);
  12. }
  13.  
  14. static bool IsChain(int[] a)
  15. {
  16. for (int i = 1; i < a.Length; i++)
  17. {
  18. if (a[i - 1] != 0 && a[i] % a[i - 1] != 0)
  19. {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25.  
  26. static int[] ReadArray()
  27. {
  28. Console.WriteLine("Введіть числа через пробіли:");
  29. string line = Console.ReadLine();
  30. string[] parts = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  31. int[] a = new int[parts.Length];
  32. for (int i = 0; i < a.Length; i++)
  33. {
  34. a[i] = int.Parse(parts[i]);
  35. }
  36. return a;
  37. }
  38.  
  39. static void PrintArray(int[] a)
  40. {
  41. for (int i = 0; i < a.Length; i++)
  42. {
  43. Console.Write(a[i] + " ");
  44. }
  45. Console.WriteLine();
  46. }
  47.  
  48. static void Main()
  49. {
  50. int[] a = ReadArray();
  51. Array.Sort(a, (x, y) => Key(x).CompareTo(Key(y)));
  52.  
  53. if (IsChain(a))
  54. {
  55. PrintArray(a);
  56. }
  57. else
  58. {
  59. Console.WriteLine("неможливо");
  60. }
  61. }
  62. }
Success #stdin #stdout 0.04s 11608KB
stdin
-7
1
14
42
7
stdout
Введіть числа через пробіли:
-7