finish day 1
This commit is contained in:
parent
2ae5de4a0e
commit
11e1ceaa9f
4 changed files with 1084 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -32,3 +32,4 @@
|
|||
*.out
|
||||
*.app
|
||||
|
||||
.vscode/
|
||||
|
|
6
01/example
Normal file
6
01/example
Normal file
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
77
01/main.cc
Normal file
77
01/main.cc
Normal file
|
@ -0,0 +1,77 @@
|
|||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
void parseFile(const char *path, vector<int> &list1, vector<int> &list2) {
|
||||
ifstream input_stream(path);
|
||||
int a;
|
||||
int b;
|
||||
|
||||
while(input_stream >> a >> b) {
|
||||
list1.push_back(a);
|
||||
list2.push_back(b);
|
||||
}
|
||||
}
|
||||
|
||||
void bubbleSort(vector<int> &list) {
|
||||
bool changed = false;
|
||||
|
||||
for (int i = 0; i < list.size() - 1; i++) {
|
||||
int temp = list[i];
|
||||
if (list[i] > list[i + 1]) {
|
||||
int temp = list[i];
|
||||
list[i] = list[i + 1];
|
||||
list[i + 1] = temp;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) bubbleSort(list);
|
||||
}
|
||||
|
||||
int count(int n, vector<int> list) {
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (list[i] == n) cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
vector<int> list1;
|
||||
vector<int> list2;
|
||||
|
||||
// parse input
|
||||
parseFile("input", list1, list2);
|
||||
|
||||
// sort lists
|
||||
bubbleSort(list1);
|
||||
bubbleSort(list2);
|
||||
|
||||
// PART 1
|
||||
/// sum up differences
|
||||
int sum = 0;
|
||||
for (int i = 0; i < list1.size(); i++) {
|
||||
cout << list1[i] << " " << list2[i] << endl;
|
||||
int difference = list1[i] - list2[i];
|
||||
if (difference > 0) {
|
||||
sum += difference;
|
||||
} else {
|
||||
sum -= difference;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "distance between lists is: " << sum << endl;
|
||||
|
||||
// PART 2
|
||||
/// sum up similarities
|
||||
int sim_score = 0;
|
||||
for (int i = 0; i < list1.size(); i++) {
|
||||
sim_score += list1[i] * count(list1[i], list2);
|
||||
}
|
||||
|
||||
cout << "similarity score is: " << sim_score << endl;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue