Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4.  
  5. namespace ZadankaAK
  6. {
  7. class Krysztal
  8. {
  9. public int[] x = new int[3];
  10. public List<int> uzyteId = new List<int>();
  11.  
  12. public Krysztal()
  13. {
  14. x[0] = 0;
  15. x[1] = 0;
  16. x[2] = 0;
  17. }
  18.  
  19. public Krysztal(int aaa)
  20. {
  21. x[0] = aaa;
  22. }
  23.  
  24. public void dodajId(int id)
  25. {
  26. uzyteId.Add(id);
  27. }
  28. }
  29.  
  30. class Paliwo
  31. {
  32. public int iloscKrysztalow;
  33.  
  34. public Paliwo()
  35. {
  36. StreamReader plik = new StreamReader("in.txt");
  37. iloscKrysztalow = Convert.ToInt32(Console.ReadLine());
  38. //iloscKrysztalow = Convert.ToInt32(plik.ReadLine());
  39. plik.ReadLine();
  40. Krysztal[] krysztaly = new Krysztal[iloscKrysztalow];
  41. List<Krysztal> krysztal2 = new List<Krysztal>();
  42.  
  43. int nr = 0;
  44. string bufor;
  45. while ((bufor = plik.ReadLine()) != null)
  46. {
  47. bufor = bufor + ' ';
  48. Krysztal buforKrysztal = new Krysztal();
  49. int id = 0;
  50.  
  51. for (int i = 0; i < bufor.Length-1; i+=2)
  52. {
  53. if (Char.IsDigit(bufor[i + 1])==true)
  54. {
  55. buforKrysztal.x[id] = (Convert.ToInt32(bufor[i]) - 48) * 10 + Convert.ToInt32(bufor[i + 1]) - 48;
  56. i++;
  57. id++;
  58. }
  59. else
  60. {
  61. buforKrysztal.x[id] = Convert.ToInt32(bufor[i]) - 48;
  62. id++;
  63. }
  64. }
  65.  
  66. krysztaly[nr] = buforKrysztal;
  67. krysztal2.Add(buforKrysztal);
  68. krysztaly[nr].dodajId(nr);
  69. nr++;
  70. }
  71.  
  72. plik.Close();
  73.  
  74. int maks = maxTon(krysztaly);
  75. for(int i=maks; i>0; i--)
  76. {
  77. Console.WriteLine("Witam");
  78. sprawdzanie(i, krysztaly, krysztal2);
  79. }
  80. }
  81.  
  82. public void sortowanie(Krysztal[] krysztaly)
  83. {
  84. for(int i=0; i<iloscKrysztalow; i++)
  85. {
  86. for(int j=1; j<iloscKrysztalow; j++)
  87. {
  88. int sumaG = 0, sumaD = 0;
  89. sumaG = krysztaly[j - 1].x[0] + krysztaly[j - 1].x[1] + krysztaly[j - 1].x[2];
  90. sumaD = krysztaly[j].x[0] + krysztaly[j].x[1] + krysztaly[j].x[2];
  91.  
  92. if (sumaG < sumaD)
  93. {
  94. Krysztal bufor = new Krysztal();
  95. bufor = krysztaly[j];
  96. krysztaly[j] = krysztaly[j - 1];
  97. krysztaly[j - 1] = bufor;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. public int maxTon(Krysztal[] krysztaly)
  104. {
  105. int wynik = 0;
  106. Krysztal czek = new Krysztal();
  107. for (int i = 0; i < krysztaly.Length; i++)
  108. {
  109. czek.x[0] += krysztaly[i].x[0];
  110. czek.x[1] += krysztaly[i].x[1];
  111. czek.x[2] += krysztaly[i].x[2];
  112. }
  113.  
  114. if (czek.x[0] < czek.x[1]) wynik = czek.x[0];
  115. else wynik = czek.x[1];
  116. if (wynik > czek.x[2]) wynik = czek.x[2];
  117. if (wynik > 100) wynik = 100;
  118.  
  119. return wynik;
  120. }
  121.  
  122. public bool check(Krysztal krysztal, Krysztal[] buff)
  123. {
  124. bool wynik = false;
  125. int x = 0, y = 0, z = 0;
  126.  
  127. for(int i=0; i<iloscKrysztalow; i++)
  128. {
  129. if(krysztal.uzyteId.Contains(i) == true)
  130. {
  131. x += buff[i].x[0];
  132. y += buff[i].x[1];
  133. z += buff[i].x[2];
  134. }
  135. }
  136.  
  137. //Console.WriteLine(x + " " + y + " " + z);
  138. if (x == y && y == z) wynik = true;
  139. if (wynik == true)
  140. {
  141. Console.WriteLine("Zdobyta ilosc paliwa: " + x * 3);
  142. Console.WriteLine("Kupione kryształy: " + krysztal.uzyteId.Count);
  143. }
  144. return wynik;
  145. }
  146.  
  147. public int sprawdzanie(int maxTon, Krysztal[] buff, List<Krysztal> buff2)
  148. {
  149. List<Krysztal> krzystal2 = new List<Krysztal>();
  150. int czyDalej = 0;
  151.  
  152. for(int i=0; i<buff2.Count; i++)
  153. {
  154. for(int j=0; j<iloscKrysztalow; j++)
  155. {
  156. if (buff2[i].uzyteId.Contains(j) == true) continue;
  157. int xd = buff2[i].x[0] + buff[j].x[0];
  158. if (xd > maxTon) continue;
  159.  
  160. Krysztal dodatkowy = new Krysztal();
  161.  
  162. for(int k=0; k<iloscKrysztalow; k++)
  163. {
  164. if (buff2[i].uzyteId.Contains(k)) dodatkowy.dodajId(k);
  165. }
  166. dodatkowy.x[0] = xd;
  167.  
  168. krzystal2.Add(dodatkowy);
  169. krzystal2[krzystal2.Count - 1].dodajId(j);
  170. czyDalej++;
  171.  
  172. if (xd == maxTon)
  173. {
  174. if (check(dodatkowy, buff) == true)
  175. {
  176. return 0;
  177. }
  178. continue;
  179. }
  180. }
  181. }
  182.  
  183. if (czyDalej != 0) sprawdzanie(maxTon, buff, krzystal2);
  184. return 0;
  185. }
  186.  
  187. public void wypisz(Krysztal []krysztaly)
  188. {
  189. for(int i=0; i<krysztaly.Length; i++)
  190. {
  191. Console.WriteLine(krysztaly[i].x[0] + " " + krysztaly[i].x[1] + " " + krysztaly[i].x[2]);
  192. }
  193. }
  194. }
  195.  
  196. class Program
  197. {
  198. static void Main(string[] args)
  199. {
  200. Paliwo paliwo = new Paliwo();
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement