bogdanNiculeasa

Server Password Problem - More efficient

Feb 4th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5. int printMaxNum(int num);
  6.  
  7.  
  8. int main() {
  9.     ifstream inFile;
  10.     inFile.open("acces.in");
  11.     int value;
  12.     int numberOfTotalDigits, numberOfDigitsToBeRemoved;
  13.     inFile >> numberOfTotalDigits >> numberOfDigitsToBeRemoved;
  14.  
  15.  
  16.     //Generate frequency array
  17.     int count[10] = {0}; // last part simply puts 0 for eeach position
  18.     //Read remaining values from the file
  19.     while (inFile >> value) {
  20.         count[value]++;
  21.     }
  22.     int result = 0, multiplier = 1; // here multiplier means unitati, zeci, sute..etc
  23.     for (int i = 0; i <= 9; i++)
  24.     {
  25.         while (count[i] > 0)
  26.         {
  27.             result = result + (i * multiplier);
  28.             count[i]--;
  29.             multiplier = multiplier * 10;
  30.         }
  31.     }
  32.  
  33.     //After we have the greatest number that can be obtained, simply discard its last m numbers;
  34.     int truncatedResult = result / pow(10, numberOfDigitsToBeRemoved);
  35.     cout << truncatedResult;
  36.     return 0;
  37. }
Add Comment
Please, Sign In to add comment