Created
March 13, 2017 15:01
-
-
Save splitline/f5c0d38c838d0cfed061fec5204b0bb1 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
| // Name: 黃志仁 | |
| // Date: 2017/03/11 | |
| // Last Update: 2017/03/11 | |
| // Problem statement: Sort and output the input number with occurences | |
| #include <iostream> | |
| #include <fstream> | |
| #include <map> | |
| using namespace std; | |
| int main() { | |
| int N; | |
| map < int, int, greater < int > > intArr; //define a map which is sort from max to min | |
| ifstream inFile("intArray.txt"); | |
| ofstream outFile("output.txt"); //read file | |
| while (inFile >> N) { | |
| if (intArr.find(N) == intArr.end()) intArr[N] = 1; | |
| else intArr[N]++; | |
| } | |
| //if N exist , + counter | |
| //else insert an element | |
| map < int, int > ::iterator it; | |
| outFile << "N" << "\t" << "Count" << endl; | |
| for (it = intArr.begin(); it != intArr.end(); it++) | |
| outFile << it -> first << "\t" << it -> second << endl; //just output | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment