Guest User

Untitled

a guest
Jun 25th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class ListaLeniwa
  9.     {
  10.         ListaLeniwa nast;
  11.         protected int ile;
  12.         protected int wartosc;
  13.         protected static Random losowa = new Random();
  14.         public ListaLeniwa()
  15.         {
  16.             wartosc = 0;
  17.             ile = 0;
  18.             this.nast = null;
  19.         }
  20.         virtual public int elementy(int i)
  21.         {
  22.             if (i <= 0)
  23.             {
  24.                 System.Console.WriteLine("Bledna liczba\n");
  25.                 return 0;
  26.             }
  27.             if (ile == 0)
  28.             {
  29.                 ListaLeniwa pom;
  30.                 this.wartosc = losowa.Next();
  31.                 this.nast = new ListaLeniwa();
  32.                 pom = this.nast;
  33.                 for (int x = 0; x < i; x++)
  34.                 {
  35.  
  36.                     pom.wartosc = losowa.Next();
  37.                     pom.nast = new ListaLeniwa();
  38.                     pom = pom.nast;
  39.                     this.ile++;
  40.                 }
  41.                 return 0;
  42.             }
  43.             if (i < ile)
  44.             {
  45.                 ListaLeniwa pom = this;
  46.                 for (int x = 0; x < i; x++)
  47.                 {
  48.                     pom = pom.nast;
  49.                 }
  50.                 return pom.wartosc;
  51.             }
  52.             if (i > ile)
  53.             {
  54.                 ListaLeniwa pom = this;
  55.                 for (int x = 0; x < ile; x++, i--)
  56.                 {
  57.                     pom = pom.nast;
  58.                 }
  59.                 for (int x = 0; x < i; x++)
  60.                 {
  61.                     pom.nast = new ListaLeniwa();
  62.                     pom.wartosc = losowa.Next();
  63.                     ile++;
  64.                     pom = pom.nast;
  65.                 }
  66.                 return 0;
  67.             }
  68.             return 0;
  69.         }
  70.     }
  71.  
  72.  
  73.     class Pierwsze : ListaLeniwa
  74.     {
  75.         int z;
  76.         int[] liczby;
  77.         Pierwsze nast;
  78.         public Pierwsze()
  79.         {
  80.             z = 0;
  81.             int n = 1000;
  82.             bool[] tab = new bool[n+1];
  83.             for (int i = 2; i * i <= n; i++)
  84.             {
  85.                 if (tab[i] == true)
  86.                 {
  87.                     continue;
  88.                 }
  89.                 for (int j = 2 * i; j <= n; j += i)
  90.                 {
  91.                     tab[j] = true;
  92.                 }
  93.             }
  94.             for (int i = 2; i <= n; i++)
  95.             {
  96.                 if (tab[i] == false)
  97.                 {
  98.                     z++;
  99.                 }
  100.             }
  101.             liczby = new int[z];
  102.             int w = 0;
  103.             for (int i = 2; i <= n; i++)
  104.             {
  105.                 if (tab[i] == false)
  106.                 {
  107.                     liczby[w] = i;
  108.                     w++;
  109.                 }
  110.             }
  111.         }
  112.  
  113.         override public int elementy(int i)
  114.         {
  115.             if (i <= 0)
  116.             {
  117.                 System.Console.WriteLine("Bledna liczba\n");
  118.                 return 0;
  119.             }
  120.             if (ile == 0)
  121.             {
  122.                 Pierwsze pom;
  123.                 this.wartosc = this.liczby[losowa.Next(this.z)];
  124.                 this.nast = new Pierwsze();
  125.                 pom = this.nast;
  126.                 for (int x = 0; x < i; x++)
  127.                 {
  128.  
  129.                     pom.wartosc = this.liczby[losowa.Next(this.z)];
  130.                     pom.nast = new Pierwsze();
  131.                     pom = pom.nast;
  132.                     this.ile++;
  133.                 }
  134.                 return 0;
  135.             }
  136.             if (i < ile)
  137.             {
  138.                 Pierwsze pom = this;
  139.                 for (int x = 0; x < i; x++)
  140.                 {
  141.                     pom = pom.nast;
  142.                 }
  143.                 return this.liczby[losowa.Next(this.z)];
  144.             }
  145.             if (i > ile)
  146.             {
  147.                 Pierwsze pom = this;
  148.                 for (int x = 0; x < ile; x++, i--)
  149.                 {
  150.                     pom = pom.nast;
  151.                 }
  152.                 for (int x = 0; x < i; x++)
  153.                 {
  154.                     pom.nast = new Pierwsze();
  155.                     pom.wartosc = this.liczby[losowa.Next(this.z)];
  156.                     ile++;
  157.                     pom = pom.nast;
  158.                 }
  159.                 return 0;
  160.             }
  161.             return 0;
  162.         }
  163.     }
  164.  
  165.     class Program
  166.     {
  167.         static void Main()
  168.         {
  169.             Pierwsze moje;
  170.             moje = new Pierwsze();
  171.             moje.elementy(100);
  172.             for (int x = 1; x < 100; x++)
  173.                 System.Console.WriteLine("{0} Liczba {1}\n", x, moje.elementy(x));
  174.             moje.elementy(102);
  175.             for (int x = 1; x < 103; x++)
  176.                 System.Console.WriteLine("{0} Liczba {1}\n", x, moje.elementy(x));
  177.  
  178.         }
  179.     }
  180. }
Add Comment
Please, Sign In to add comment