Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace algorytmy06._12._2019
  7. {
  8. class Program
  9. {
  10. Zadanie 1
  11.  
  12. static void Zadanie1()
  13.  
  14. {
  15.  
  16. Console.WriteLine("Podaj liczbe n");
  17.  
  18. int n = int.Parse(Console.ReadLine());
  19.  
  20. Console.WriteLine("n!/(3^n)");
  21.  
  22. Console.WriteLine("Rekurencyjnie: " + (SilniaR(n) / PotegaI(3, n)));
  23.  
  24. Console.WriteLine("Iteracyjnie: " + (SilniaI(n) / PotegaI(3, n)));
  25.  
  26. }
  27.  
  28.  
  29.  
  30. static int SilniaI(int liczba)
  31.  
  32. {
  33.  
  34. if (liczba < 2) return 1;
  35.  
  36.  
  37.  
  38. for(int i = liczba-1; i > 1; i--)
  39.  
  40. {
  41.  
  42. liczba = liczba * i;
  43.  
  44. }
  45.  
  46. return liczba;
  47.  
  48. }
  49.  
  50.  
  51.  
  52. static int PotegaI(int liczba, int potega)
  53.  
  54. {
  55.  
  56. if (potega == 0) return 1;
  57.  
  58.  
  59.  
  60. int pomoc = liczba;
  61.  
  62. for(int i = 0; i < potega-1; i++)
  63.  
  64. {
  65.  
  66. liczba = pomoc * liczba;
  67.  
  68. }
  69.  
  70. return liczba;
  71.  
  72. }
  73.  
  74.  
  75.  
  76. static int SilniaR(int liczba)
  77.  
  78. {
  79.  
  80. if (liczba < 2)
  81.  
  82. return 1;
  83.  
  84. else
  85.  
  86. return SilniaR(liczba-1) * liczba;
  87.  
  88. }
  89.  
  90.  
  91.  
  92. static int PotegaR(int liczba, int potega)
  93.  
  94. {
  95.  
  96. if(potega == 0)
  97.  
  98. {
  99.  
  100. return 1;
  101.  
  102. }
  103.  
  104. else
  105.  
  106. {
  107.  
  108. return liczba*PotegaR(liczba, potega -1);
  109.  
  110. }
  111.  
  112. }
  113.  
  114.  
  115.  
  116. //Zadanie 2
  117.  
  118. static void Zadanie2()
  119.  
  120. {
  121.  
  122. //Console.WriteLine("An(8)");
  123.  
  124. Console.WriteLine("Rekurencyjnie: "+ AnR(8));
  125.  
  126. Console.WriteLine("Iteracyjnie: " + AnI(8));
  127.  
  128. }
  129.  
  130.  
  131.  
  132. static int AnI(int n)
  133.  
  134. {
  135.  
  136. int i = 0;
  137.  
  138. n++;//Bo potrzebujemy Bn+1 do obliczenia An
  139.  
  140. int[] wartosciBn = new int[n + 1];
  141.  
  142. while (i <= n)
  143.  
  144. {
  145.  
  146. if (i <= 3)
  147.  
  148. wartosciBn[i] = i;
  149.  
  150. else if (i > 3)
  151.  
  152. wartosciBn[i] = 2 * wartosciBn[i - 1] + wartosciBn[i - 2];
  153.  
  154. i++;
  155.  
  156. }
  157.  
  158. n--;//Wracamy
  159.  
  160.  
  161.  
  162. i = 0;
  163.  
  164. int[] wartosci = new int[n+1];
  165.  
  166. while(i <= n)
  167.  
  168. {
  169.  
  170. if (i <= 4)
  171.  
  172. wartosci[i] = PotegaI(-1, 4);
  173.  
  174. else if(i > 4)
  175.  
  176. {
  177.  
  178. wartosci[i] = wartosciBn[i + 1] - wartosci[i - 1];
  179.  
  180. }
  181.  
  182. i++;
  183.  
  184. }
  185.  
  186. return wartosci[n];
  187.  
  188. }
  189.  
  190.  
  191.  
  192. static int AnR(int n)
  193.  
  194. {
  195.  
  196. if( n<= 4)
  197.  
  198. {
  199.  
  200. return PotegaR(-1, 4);
  201.  
  202. }
  203.  
  204. else if(n > 4)
  205.  
  206. {
  207.  
  208. return BnR(n + 1) - AnR(n - 1);
  209.  
  210. }
  211.  
  212. return 0;
  213.  
  214. }
  215.  
  216.  
  217.  
  218. static int BnR(int n)
  219.  
  220. {
  221.  
  222. if (n <= 3)
  223.  
  224. return n;
  225.  
  226. else if(n > 3)
  227.  
  228. {
  229.  
  230. return (2 * BnR(n - 1) + BnR(n - 2));
  231.  
  232. }
  233.  
  234. return 0;
  235.  
  236. }
  237.  
  238.  
  239.  
  240. //Zadanie 3
  241.  
  242. static void Zadanie3()
  243.  
  244. {
  245.  
  246. int[] tab = { 1, 2, 5, 7, 9 };
  247.  
  248. int szukana = 7;
  249.  
  250. int pos = WyszukajBinRek(tab, szukana, 0, tab.Length-1);
  251.  
  252. Console.WriteLine("Znaleziono "+szukana+" na pozycji " +pos);
  253.  
  254. pos = WyszukajBinIt(tab, szukana);
  255.  
  256. Console.WriteLine("Znaleziono " + szukana + " na pozycji " + pos);
  257.  
  258.  
  259.  
  260. }
  261.  
  262.  
  263.  
  264. static int WyszukajBinIt(int[] tab, int szuk)
  265.  
  266. {
  267.  
  268. int l = 0;
  269.  
  270. int p = tab.Length - 1;
  271.  
  272. int pivot = 0;
  273.  
  274. while(l <= p)
  275.  
  276. {
  277.  
  278. pivot = (l + p) / 2;
  279.  
  280. if (tab[pivot] == szuk)
  281.  
  282. return pivot+1;
  283.  
  284.  
  285.  
  286. if(tab[pivot] > szuk)
  287.  
  288. {
  289.  
  290. p = pivot - 1;
  291.  
  292. }
  293.  
  294. else if(tab[pivot] < szuk)
  295.  
  296. {
  297.  
  298. l = pivot + 1;
  299.  
  300. }
  301.  
  302. }
  303.  
  304. return -1;
  305.  
  306. }
  307.  
  308.  
  309.  
  310. static int WyszukajBinRek(int[] tab, int szuk, int l, int p)
  311.  
  312. {
  313.  
  314. int pivot = (l + p) / 2;
  315.  
  316. if (tab[pivot] == szuk)
  317.  
  318. return pivot+1;
  319.  
  320. if (l > p)
  321.  
  322. return -1;
  323.  
  324. if (tab[pivot] > szuk)
  325.  
  326. {
  327.  
  328. return WyszukajBinRek(tab, szuk, l, pivot-1);
  329.  
  330. }
  331.  
  332. else if(tab[pivot] < szuk)
  333.  
  334. {
  335.  
  336. return WyszukajBinRek(tab, szuk, pivot+1, p);
  337.  
  338. }
  339.  
  340. return -1;
  341.  
  342. }
  343.  
  344.  
  345.  
  346. //Zadanie 4
  347.  
  348. static void Zadanie4()
  349.  
  350. {
  351.  
  352. string path = @"C:\Users\student\Downloads";
  353.  
  354. Console.WriteLine(NajwiekszyPlik(path));
  355.  
  356. Console.WriteLine(Ilew2019(path));
  357.  
  358. }
  359.  
  360.  
  361.  
  362. static string NajwiekszyPlik(string path)
  363.  
  364. {
  365.  
  366. long size = 0;
  367.  
  368. long maxsize = 0;
  369.  
  370. string maxpath = "";
  371.  
  372. foreach (string f in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
  373.  
  374. {
  375.  
  376. size = new FileInfo(f).Length;
  377.  
  378. if (maxsize < size)
  379.  
  380. {
  381.  
  382. maxsize = size;
  383.  
  384. maxpath = f;
  385.  
  386. }
  387.  
  388. }
  389.  
  390. return maxpath;
  391.  
  392. }
  393.  
  394.  
  395.  
  396. static int Ilew2019(string path)
  397.  
  398. {
  399.  
  400. int count = 0;
  401.  
  402. foreach (string f in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
  403.  
  404. {
  405.  
  406. if (new FileInfo(f).CreationTime.Year == 2019)
  407.  
  408. {
  409.  
  410. count++;
  411.  
  412. }
  413.  
  414. }
  415.  
  416. return count;
  417.  
  418. }
  419.  
  420.  
  421.  
  422. static void Main(string[] args)
  423.  
  424. {
  425.  
  426. Zadanie1();
  427.  
  428. Zadanie2();
  429.  
  430. Zadanie3();
  431.  
  432. Zadanie4();
  433.  
  434. Console.ReadKey();
  435.  
  436. }
  437.  
  438. }
  439.  
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement