-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
138 lines (112 loc) · 4.45 KB
/
Program.cs
File metadata and controls
138 lines (112 loc) · 4.45 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace MorePizza
{
class Program
{
public static string PathToFile { get; set; }
static void Main(string[] args)
{
var exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
Regex appPathMatcher = new Regex(@"(?<!fil)[A-Za-z]:\\+[\S\s]*?(?=\\+bin)");
var thisAppPath = appPathMatcher.Match(exePath).Value;
string outputPath = Path.Combine(thisAppPath, "OutputFiles");
//string PathToFile = Path.Combine(thisAppPath, "InputFiles\\a_example.in");
//string PathToFile = Path.Combine(thisAppPath, "InputFiles\\b_small.in");
//string PathToFile = Path.Combine(thisAppPath, "InputFiles\\c_medium.in");
//string PathToFile = Path.Combine(thisAppPath, "InputFiles\\d_quite_big.in");
string PathToFile = Path.Combine(thisAppPath, "InputFiles\\e_also_big.in");
int MaxSlices;
int Types;
int[] SlisesInPizza;
List<int> result = new List<int>();
bool findGreateResult = false;
List<Pizzas> res = new List<Pizzas>();
using (StreamReader sr = new StreamReader(PathToFile))
{
var line = sr.ReadLine();
var Parts = line.Split(' ');
MaxSlices = int.Parse(Parts[0]);
Types = int.Parse(Parts[1]);
line = sr.ReadLine();
Parts = line.Split(' ');
SlisesInPizza = new int[Types];
for (int i = 0; i < Types; i++)
{
SlisesInPizza[i] = int.Parse(Parts[i]);
}
}
var count = 0;
int start = 1;
while (start < Types / 2)
{
for (int i = Types - start; i >= 0; i--)
{
if (count + SlisesInPizza[i] <= MaxSlices)
{
count += SlisesInPizza[i];
result.Add(i);
}
}
if (count == MaxSlices)
{
// we find greate result
// break all calculations
findGreateResult = true;
var sortedList = result.OrderBy(c => c).ToList();
using (StreamWriter sw = new StreamWriter(outputPath + "\\" + Path.GetFileNameWithoutExtension(PathToFile) + ".out"))
{
sw.WriteLine(result.Count);
foreach (var item in sortedList)
{
sw.Write(item);
sw.Write(' ');
}
sw.WriteLine();
}
Console.WriteLine($"Score: {MaxSlices}");
break;
}
else
{
start++;
count = 0;
res.Add(new Pizzas
{
TypesOfPizza = result.Count,
ArrayOfIndexes = result.OrderBy(c => c).ToList()
});
result = new List<int>();
}
}
if (!findGreateResult)
{
// if no greate result => out max result
var maxSutableRes = res.OrderByDescending(x => x.TypesOfPizza).First();
var score = 0;
using (StreamWriter sw = new StreamWriter(outputPath + "\\" + Path.GetFileNameWithoutExtension(PathToFile) + ".out"))
{
sw.WriteLine(maxSutableRes.TypesOfPizza);
foreach (var item in maxSutableRes.ArrayOfIndexes)
{
sw.Write(item);
sw.Write(' ');
score += SlisesInPizza[item];
}
sw.WriteLine();
}
Console.WriteLine($"Score: {score}");
}
Console.WriteLine("Done!");
Console.ReadKey();
}
}
public class Pizzas
{
public int TypesOfPizza { get; set; }
public List<int> ArrayOfIndexes { get; set; }
}
}