-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSComputer_Discs.cs
More file actions
94 lines (90 loc) · 2.96 KB
/
SComputer_Discs.cs
File metadata and controls
94 lines (90 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
// объявите струкуру, которая бы описывала персональный компьютер SComputer.
// ...
// Далее разработайте функцию, которая бы получала список элементов типа SComputer,
// отсортированный по общему размеру дисков и определяла бы бинарным поиском номер компьютера с заданным объемом дисков
namespace ConsoleApp1
{
class Program
{
public struct SComputer
{
public List<double> Drives;
private double SumVolume;
public void AddDrives(double drive)
{
Drives.Add(drive);
}
public double GetSum()
{
foreach(double drive in Drives)
{
SumVolume += drive;
}
return SumVolume;
}
}
public static void Main(string[] args)
{
int resInd;
List<SComputer> test=new List<SComputer>();
for(int i = 0; i < 5; i++)
{
SComputer c = new SComputer();
c.Drives = new List<double>();
test.Add(c);
}
test[0].AddDrives(1.2);
test[1].AddDrives(1.2);
test[1].AddDrives(1.2);
test[2].AddDrives(1.2);
test[2].AddDrives(1.2);
test[2].AddDrives(1.2);
test[3].AddDrives(1.2);
test[3].AddDrives(1.2);
test[3].AddDrives(1.2);
test[3].AddDrives(1.2);
test[4].AddDrives(1.2);
test[4].AddDrives(1.2);
test[4].AddDrives(1.2);
test[4].AddDrives(1.2);
test[4].AddDrives(1.2);
Search(test, 6, out resInd);
if (resInd == -1)
{
Console.WriteLine("компьютер не найден");
}
else
{
Console.WriteLine("Номер компьютера {0}", resInd);
}
Console.ReadKey();
}
public static bool Search(List<SComputer> computers, double volume, out int resInd)
{
int mid;
int index1 = 0;
int index2 = computers.Count-1;
while (index1<=index2)
{
mid = (index1 + index2) / 2;
if(computers[mid].GetSum()==volume)
{
resInd = mid;
return true;
}
else if (computers[mid].GetSum() < volume)
{
index1 = mid+1;
}
else
{
index2 = mid - 1;
}
}
resInd = -1;
return false;
}
}
}