Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace algorytmy06._12._2019
- {
- class Program
- {
- Zadanie 1
- static void Zadanie1()
- {
- Console.WriteLine("Podaj liczbe n");
- int n = int.Parse(Console.ReadLine());
- Console.WriteLine("n!/(3^n)");
- Console.WriteLine("Rekurencyjnie: " + (SilniaR(n) / PotegaI(3, n)));
- Console.WriteLine("Iteracyjnie: " + (SilniaI(n) / PotegaI(3, n)));
- }
- static int SilniaI(int liczba)
- {
- if (liczba < 2) return 1;
- for(int i = liczba-1; i > 1; i--)
- {
- liczba = liczba * i;
- }
- return liczba;
- }
- static int PotegaI(int liczba, int potega)
- {
- if (potega == 0) return 1;
- int pomoc = liczba;
- for(int i = 0; i < potega-1; i++)
- {
- liczba = pomoc * liczba;
- }
- return liczba;
- }
- static int SilniaR(int liczba)
- {
- if (liczba < 2)
- return 1;
- else
- return SilniaR(liczba-1) * liczba;
- }
- static int PotegaR(int liczba, int potega)
- {
- if(potega == 0)
- {
- return 1;
- }
- else
- {
- return liczba*PotegaR(liczba, potega -1);
- }
- }
- //Zadanie 2
- static void Zadanie2()
- {
- //Console.WriteLine("An(8)");
- Console.WriteLine("Rekurencyjnie: "+ AnR(8));
- Console.WriteLine("Iteracyjnie: " + AnI(8));
- }
- static int AnI(int n)
- {
- int i = 0;
- n++;//Bo potrzebujemy Bn+1 do obliczenia An
- int[] wartosciBn = new int[n + 1];
- while (i <= n)
- {
- if (i <= 3)
- wartosciBn[i] = i;
- else if (i > 3)
- wartosciBn[i] = 2 * wartosciBn[i - 1] + wartosciBn[i - 2];
- i++;
- }
- n--;//Wracamy
- i = 0;
- int[] wartosci = new int[n+1];
- while(i <= n)
- {
- if (i <= 4)
- wartosci[i] = PotegaI(-1, 4);
- else if(i > 4)
- {
- wartosci[i] = wartosciBn[i + 1] - wartosci[i - 1];
- }
- i++;
- }
- return wartosci[n];
- }
- static int AnR(int n)
- {
- if( n<= 4)
- {
- return PotegaR(-1, 4);
- }
- else if(n > 4)
- {
- return BnR(n + 1) - AnR(n - 1);
- }
- return 0;
- }
- static int BnR(int n)
- {
- if (n <= 3)
- return n;
- else if(n > 3)
- {
- return (2 * BnR(n - 1) + BnR(n - 2));
- }
- return 0;
- }
- //Zadanie 3
- static void Zadanie3()
- {
- int[] tab = { 1, 2, 5, 7, 9 };
- int szukana = 7;
- int pos = WyszukajBinRek(tab, szukana, 0, tab.Length-1);
- Console.WriteLine("Znaleziono "+szukana+" na pozycji " +pos);
- pos = WyszukajBinIt(tab, szukana);
- Console.WriteLine("Znaleziono " + szukana + " na pozycji " + pos);
- }
- static int WyszukajBinIt(int[] tab, int szuk)
- {
- int l = 0;
- int p = tab.Length - 1;
- int pivot = 0;
- while(l <= p)
- {
- pivot = (l + p) / 2;
- if (tab[pivot] == szuk)
- return pivot+1;
- if(tab[pivot] > szuk)
- {
- p = pivot - 1;
- }
- else if(tab[pivot] < szuk)
- {
- l = pivot + 1;
- }
- }
- return -1;
- }
- static int WyszukajBinRek(int[] tab, int szuk, int l, int p)
- {
- int pivot = (l + p) / 2;
- if (tab[pivot] == szuk)
- return pivot+1;
- if (l > p)
- return -1;
- if (tab[pivot] > szuk)
- {
- return WyszukajBinRek(tab, szuk, l, pivot-1);
- }
- else if(tab[pivot] < szuk)
- {
- return WyszukajBinRek(tab, szuk, pivot+1, p);
- }
- return -1;
- }
- //Zadanie 4
- static void Zadanie4()
- {
- string path = @"C:\Users\student\Downloads";
- Console.WriteLine(NajwiekszyPlik(path));
- Console.WriteLine(Ilew2019(path));
- }
- static string NajwiekszyPlik(string path)
- {
- long size = 0;
- long maxsize = 0;
- string maxpath = "";
- foreach (string f in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
- {
- size = new FileInfo(f).Length;
- if (maxsize < size)
- {
- maxsize = size;
- maxpath = f;
- }
- }
- return maxpath;
- }
- static int Ilew2019(string path)
- {
- int count = 0;
- foreach (string f in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
- {
- if (new FileInfo(f).CreationTime.Year == 2019)
- {
- count++;
- }
- }
- return count;
- }
- static void Main(string[] args)
- {
- Zadanie1();
- Zadanie2();
- Zadanie3();
- Zadanie4();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement