Advertisement
miknik97

Antypolski Program

Oct 29th, 2018
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Algorithms05
  8. {
  9.     class Program
  10.     {
  11.         static void zad1()
  12.         {
  13.             int[] mandaty;
  14.             int[] glosy;
  15.             float[] calcTab;
  16.             float max;
  17.             int groupCount, mandatesCount, maxInd = 0;
  18.  
  19.             Console.Write("Podaj liczbe partii: ");
  20.             groupCount = int.Parse(Console.ReadLine());
  21.  
  22.             mandaty = new int[groupCount];
  23.             glosy = new int[groupCount];
  24.             calcTab = new float[groupCount];
  25.  
  26.             Console.Write("Podaj liczbe mandatow: ");
  27.             mandatesCount = int.Parse(Console.ReadLine());
  28.  
  29.             for (int i = 0; i < groupCount; i++)
  30.             {
  31.                 Console.Write("Podaj liczbe glosow na partie " + (i + 1) + ": ");
  32.                 glosy[i] = int.Parse(Console.ReadLine());
  33.             }
  34.        
  35.             for (int i = 0; i < groupCount; i++) mandaty[i] = 0;
  36.             for (int i = 0; i < groupCount; i++) calcTab[i] = glosy[i];
  37.          
  38.             for (int i = 0; i < mandatesCount; i++)
  39.             {
  40.                 max = -1;
  41.                 for (int j = 0; j < groupCount; j++)
  42.                 {
  43.                     if (max < calcTab[j])
  44.                     {
  45.                         max = calcTab[j];
  46.                         maxInd = j;
  47.                     }
  48.                 }
  49.                 mandaty[maxInd]++;
  50.                 calcTab[maxInd] = (float)((glosy[maxInd] * 1.0) / (mandaty[maxInd] + 1.0));
  51.             }
  52.  
  53.             for (int i = 0; i < groupCount; i++)
  54.             {
  55.                 Console.WriteLine("Partia " + (i + 1) + ": " + mandaty[i]);
  56.             }
  57.             Console.ReadLine();
  58.         }
  59.         static void zad3()
  60.         {
  61.             int[] wartosc = { 10, 50, 70 };
  62.             int[] waga = { 10, 20, 30 };
  63.             int pojemnosc = 40;
  64.             int ilosc = 3;
  65.  
  66.             int[,] plecak = new int[ilosc + 1, pojemnosc + 1];
  67.             for (int i = 0; i <= ilosc; i++)
  68.             {
  69.                 for (int j = 0; j <= pojemnosc; j++)
  70.                 {
  71.                     if (i == 0 || j == 0)
  72.                     {
  73.                         plecak[i, j] = 0;
  74.                     }
  75.                     else if (waga[i - 1] <= j)
  76.                     {
  77.                         plecak[i, j] = Math.Max(wartosc[i - 1] + plecak[i - 1, j - waga[i - 1]], plecak[i - 1, j]);
  78.                     }
  79.                     else
  80.                     {
  81.                         plecak[i, j] = plecak[i - 1, j];
  82.                     }
  83.                 }
  84.             }
  85.             Console.Write("Wynik: {0}", plecak[ilosc, pojemnosc]);
  86.             Console.ReadLine();
  87.         }
  88.         static void Main(string[] args)
  89.         {
  90.             zad1();
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement