-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (63 loc) · 1.44 KB
/
main.cpp
File metadata and controls
66 lines (63 loc) · 1.44 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
#include <iostream>
#include <vector>
#include <string>
#include <typeinfo>
std::vector <int> getInput(int times)
{
std::string buffer;
std::vector <int> input(times);
std::getline(std::cin, buffer);
for (int i = 0; i < times; i++)
{
if (i < times - 1)
{
int spaceLocation = buffer.find(" ");
input[i] = stoi(buffer.substr(0, spaceLocation));
buffer.erase(0, spaceLocation + 1);
}
else if (i == times - 1)
{
input[i] = stoi(buffer.substr(0, buffer.length()));
}
}
return input;
}
int trashFinder(std::vector <int> n, std::vector <int> a, std::vector <int> b)
{
int inversionCount{ 0 };
for (int i1 = 0; i1 < n[0]; i1++)
{
for (int i2 = i1 + 1; i2 < n[0]; i2++)
{
if (i1 < i2 && find(b.begin(), b.end(), a[i1]) > find(b.begin(), b.end(), a[i2]))
{
inversionCount++;
}
else if (i1 > i2 && find(b.begin(), b.end(), a[i1]) < find(b.begin(), b.end(), a[i2]))
{
inversionCount++;
}
}
}
return inversionCount;
}
int main()
{
std::cout << "Please enter inputs:\n";
std::vector <std::vector <int>> input(3);
input[0] = getInput(1);
input[1] = getInput(input[0][0]);
input[2] = getInput(1);
input.resize(input[2][0] + 3);
for (int i = 3; i < input[2][0] + 3; i++)
{
input[i] = getInput(input[0][0]);
}
std::cout << "Number of inversions:\n";
for (int i = 3; i < input[2][0] + 3; i++)
{
std::cout << trashFinder(input[0], input[1], input[i]) << "\n";
}
std::getchar();
return EXIT_SUCCESS;
}