Last active
March 27, 2016 06:23
-
-
Save mark-jay/e7e4023a32a82e2bc630 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Copyright 2016 <Mark Jay> | |
| */ | |
| #include <iostream> | |
| #include <vector> | |
| #include <cstdlib> // abs | |
| #include <algorithm> // std::swap | |
| #include <numeric> // std::accumulate | |
| using std::cout; | |
| using std::cin; | |
| using std::endl; | |
| void inputWithPrompt(const char * prompt, int * input) { | |
| cout << prompt; | |
| cin >> (*input); | |
| } | |
| std::vector<int> readInput() { | |
| int n; | |
| int input; | |
| std::vector<int> vector; | |
| inputWithPrompt("vvedite nach znachenie golovki: ", &input); | |
| vector.push_back(input); | |
| inputWithPrompt("vvedite kolichestvo processov:", &n); | |
| cout << "vvedite processy v poryadke zaprosov: "; | |
| for (int i=1; i <= n; i++) { | |
| cin >> input; | |
| vector.push_back(input); | |
| } | |
| // For Oleg: it's ok to return object: | |
| // http://stackoverflow.com/questions/10476665/c-avoiding-copy-with-the-return-statement | |
| // https://en.wikipedia.org/wiki/Copy_elision | |
| return vector; | |
| } | |
| std::vector<int> process(std::vector<int> userData) { | |
| int minIndex; // previously : x | |
| int min; // previously : s | |
| std::vector<int> res; | |
| for (int i=0; i < userData.size()-1; i++) { | |
| for (int j=i+1; j <= userData.size()-1; j++) { | |
| //вычисление расст | |
| int d = abs(userData[i] - userData[j]); | |
| if (d < min) { | |
| min = d; | |
| minIndex = j; | |
| } | |
| } | |
| //сортировка | |
| std::swap(userData[i+1], userData[minIndex]); | |
| res.push_back(userData[i+1]); | |
| } | |
| return res; | |
| } | |
| void printResult(std::vector<int> const & result) { | |
| cout << "obrabotka zaprosov:"; | |
| int prevValue = result[0]; | |
| int sum = 0; | |
| for (int i=0; i < result.size(); i++) { | |
| cout << result[i] << " "; | |
| sum += abs(result[i] - prevValue); | |
| prevValue = result[i]; | |
| } | |
| cout << "itogo dvizhenie golovy: " | |
| << std::accumulate(result.begin(), result.end(), 0); | |
| } | |
| int main() { | |
| std::vector<int> result = process(readInput()); | |
| printResult(result); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment