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 void SortByKey(int[] a)
  15. {
  16. for (int i = 0; i < a.Length - 1; i++)
  17. {
  18. for (int j = 0; j < a.Length - 1 - i; j++)
  19. {
  20. if (Key(a[j]) > Key(a[j + 1]))
  21. {
  22. int temp = a[j];
  23. a[j] = a[j + 1];
  24. a[j + 1] = temp;
  25. }
  26. }
  27. }
  28. }
  29.  
  30. static bool IsChain(int[] a)
  31. {
  32. for (int i = 1; i < a.Length; i++)
  33. {
  34. if (a[i - 1] != 0 && a[i] % a[i - 1] != 0)
  35. {
  36. return false;
  37. }
  38. }
  39. return true;
  40. }
  41.  
  42. static int[] ReadArray()
  43. {
  44. Console.WriteLine("Введіть числа через пробіли:");
  45. string line = Console.ReadLine();
  46. string[] parts = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  47. int[] a = new int[parts.Length];
  48. for (int i = 0; i < a.Length; i++)
  49. {
  50. a[i] = int.Parse(parts[i]);
  51. }
  52. return a;
  53. }
  54.  
  55. static void PrintArray(int[] a)
  56. {
  57. for (int i = 0; i < a.Length; i++)
  58. {
  59. Console.Write(a[i] + " ");
  60. }
  61. Console.WriteLine();
  62. }
  63.  
  64. static void Main()
  65. {
  66. int[] a = ReadArray();
  67. SortByKey(a);
  68.  
  69. if (IsChain(a))
  70. {
  71. PrintArray(a);
  72. }
  73. else
  74. {
  75. Console.WriteLine("неможливо");
  76. }
  77. }
  78. }
Success #stdin #stdout 0.02s 11592KB
stdin
-7
1
14
42
7
stdout
Введіть числа через пробіли:
-7