Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13. public partial class Form1 : Form
  14. {
  15. int n;
  16. int G;
  17. public struct obiect
  18. {
  19. public int nr, gr, val;
  20. }
  21. obiect []o;
  22.  
  23.  
  24. public void afisare(obiect []o, int n)
  25. {
  26. int i;
  27. for (i=0;i<n;i++)
  28. {
  29. dataGridView1.Rows[i].Cells[0].Value = (o[i].nr).ToString();
  30. dataGridView1.Rows[i].Cells[1].Value = (o[i].gr).ToString();
  31. dataGridView1.Rows[i].Cells[2].Value = (o[i].val).ToString();
  32. }
  33. }
  34.  
  35. public void sortare(obiect []o, int n)
  36. {
  37. int i, j;
  38. obiect aux;
  39. for (i = 0; i < n - 1; i++)
  40. for (j = i + 1; j < n; j++)
  41. if ((double)o[i].gr / o[i].val > (double)o[j].gr / o[j].val) { aux = o[i]; o[i] = o[j]; o[j] = aux; }
  42. }
  43.  
  44. public Form1()
  45. {
  46. InitializeComponent();
  47. }
  48.  
  49. private void Form1_Load(object sender, EventArgs e)
  50. {
  51.  
  52. }
  53.  
  54. private void button1_Click(object sender, EventArgs e)
  55. {
  56. int i=0;
  57.  
  58. string sir = File.ReadAllText("intrare.txt");
  59. string[] linii = sir.Split('\n');
  60. n = linii.Length;
  61. o = new obiect[n];
  62. label1.Text = "n =" + n.ToString();
  63.  
  64. dataGridView1.RowCount = n;
  65. dataGridView1.ColumnCount = 3;
  66. dataGridView1.Columns[0].HeaderCell.Value = "Nr.";
  67. dataGridView1.Columns[1].HeaderCell.Value = "Gr.";
  68. dataGridView1.Columns[2].HeaderCell.Value = "Val.";
  69.  
  70. for (i=0;i<n;i++)
  71. {
  72. string[] valori = linii[i].Split(' ');
  73. o[i].gr = Convert.ToInt32(valori[0]);
  74. o[i].val = Convert.ToInt32(valori[1]);
  75. o[i].nr = i + 1;
  76. }
  77. afisare(o, n);
  78. }
  79.  
  80. private void button2_Click(object sender, EventArgs e)
  81. {
  82. int i,m,greutate,V;
  83. sortare(o, n);
  84. afisare(o, n);
  85.  
  86. obiect[] oa = new obiect[n];
  87. m = 0; V = 0; greutate = 0;
  88. for (i = 0; i < n && greutate < G; i++)
  89. if (greutate + o[i].gr <= G)
  90. {
  91. greutate += o[i].gr;
  92. oa[m++] = o[i];
  93. V += o[i].val;
  94. }
  95.  
  96.  
  97.  
  98. label2.Text = "Total obiecte alese = " + m.ToString();
  99. label4.Text = "Valoare obiecte alese = " + V.ToString();
  100.  
  101. dataGridView2.RowCount = m;
  102. dataGridView2.ColumnCount = 3;
  103. dataGridView2.Columns[0].HeaderCell.Value = "Nr.";
  104. dataGridView2.Columns[1].HeaderCell.Value = "Gr.";
  105. dataGridView2.Columns[2].HeaderCell.Value = "Val.";
  106.  
  107. for (i = 0; i < m; i++)
  108. {
  109. dataGridView2.Rows[i].Cells[0].Value = (oa[i].nr).ToString();
  110. dataGridView2.Rows[i].Cells[1].Value = (oa[i].gr).ToString();
  111. dataGridView2.Rows[i].Cells[2].Value = (oa[i].val).ToString();
  112. }
  113. }
  114.  
  115.  
  116. private void button3_Click(object sender, EventArgs e)
  117. {
  118. int[,] V = new int[n + 1, G + 1];
  119. int i, j;
  120. for (i = 1; i <= n; i++)
  121. for (j = 1; j <= G; j++)
  122. if (o[i - 1].gr > j)
  123. V[i, j] = V[i - 1, j];
  124. else V[i, j] = Math.Max(V[i - 1, j], V[i - 1, j - o[i - 1].gr] + o[i - 1].val);
  125. label5.Text = "val=" + V[n, G].ToString();
  126. }
  127.  
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement