Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <sstream>
- #include <fstream>
- #include <numeric>
- #include <time.h>
- using namespace std;
- ofstream fout("out.out");
- void readInput(vector<vector<int>>& cityDatabase, int noBlocks)
- {
- ifstream fin("input.in");
- if (fin.good())
- {
- string str;
- for (int i = 0; i < noBlocks; ++i)
- {
- while (getline(fin, str))
- {
- istringstream ss(str);
- int num;
- vector<int> temp;
- while (ss >> num)
- {
- temp.push_back(num);
- }
- cityDatabase.push_back(temp);
- temp.clear();
- }
- }
- }
- fin.close();
- }
- void printData(vector<vector<int>> cityDatabase)
- {
- for (int i = 0; i < cityDatabase.size(); ++i) {
- for (int j = 0; j < cityDatabase[i].size(); ++j)
- {
- fout << cityDatabase[i][j] << " ";
- }
- fout << endl;
- }
- fout << endl;
- }
- void subpA(vector<vector<int>> cityDatabase, int noBlocks)
- {
- for (int i = 0; i < cityDatabase.size(); ++i)
- {
- for (int j = 0; j < cityDatabase[i].size(); ++j)
- {
- if (cityDatabase[i][j] > 500)
- {
- fout << i << " - " << j << " - " << cityDatabase[i][j] << "\n";
- }
- }
- }
- fout << endl;
- }
- void subpB(vector<vector<int>>& cityDatabase)
- {
- for (int i = 0; i < cityDatabase.size(); ++i)
- {
- sort(cityDatabase[i].begin(), cityDatabase[i].end(), greater<int>());
- }
- printData(cityDatabase);
- }
- void subpC(vector<vector<int>> cityDatabase)
- {
- int maximum = INT_MIN, mostPopular = -1;
- for (int i = 0; i < cityDatabase.size(); ++i)
- {
- if (accumulate(cityDatabase[i].begin(), cityDatabase[i].end(), 0) > maximum)
- {
- maximum = max(maximum, accumulate(cityDatabase[i].begin(), cityDatabase[i].end(), 0));
- mostPopular = i;
- }
- }
- fout << mostPopular << " " << maximum;
- fout << endl;
- }
- void subpExtra(vector<vector<int>> cityDatabase)
- {
- for (int i = 0; i < cityDatabase.size(); ++i)
- {
- auto lower = lower_bound(cityDatabase[i].begin(), cityDatabase[i].end(), 10);
- if (accumulate(lower - cityDatabase[i].begin(), cityDatabase[i].end(), 0) < 100)
- {
- int sum = accumulate(lower - cityDatabase[i].begin(), cityDatabase[i].end(), 0);
- cityDatabase[i].erase((lower - cityDatabase[i].begin()), cityDatabase[i].end());
- cityDatabase[i].insert(cityDatabase[i].end(), sum);
- }
- }
- printData(cityDatabase);
- fout.close();
- }
- int main()
- {
- srand(time(NULL));
- int noBlocks = rand() % 15;
- if (noBlocks < 2) { noBlocks += 2; }
- vector<vector<int>> cityDatabase;
- //int noBlocks = 7;
- readInput(cityDatabase, noBlocks);
- subpA(cityDatabase, noBlocks);
- subpB(cityDatabase);
- subpC(cityDatabase);
- subpExtra(cityDatabase);
- return 0;
- }
Add Comment
Please, Sign In to add comment