끄적끄적 코딩
article thumbnail
Published 2021. 8. 4. 20:25
[C++] 백준 4358번 생태학 알고리즘

 

1. map을 이용해서 해당 문자가 몇번 나왔는지 개수를 저장
2. 이름 오름차순으로 차례대로 전체 개수에서 해당 개수를 나눈 수를 출력

#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

int main (int argc, char *argv[])
{
  int n;
  int personnel;
  char s[31];
  vector<string> v;
  map<string, int> m;
	
  while(1) {
    cin.getline(s, 31, '\n');
		
	if(cin.eof() == true) {
      break;
    }
    if(m.count(s) == 0 ){
	  v.push_back(s);
	  m.insert(make_pair(s, 1));
    } else {
	  ++m[s];	
    }
    ++personnel;
  }
	
  sort(v.begin(), v.end());
		
  cout << fixed;
  cout.precision(4);
	
  for(int i=0; i<v.size(); ++i){
   	cout << v[i] << " ";
    cout << (double)m[v[i]] / (double)personnel * 100 << endl;
  }
	
  return 0;
}

 

 

'알고리즘' 카테고리의 다른 글

[C++] 백준 1339번 단어 수학  (0) 2021.08.09
[C++] 백준 1744번 수 묶기  (0) 2021.08.04
[C++] 프로그래머스 - 압축  (0) 2021.05.26
[C++] 프로그래머스 - N진수 게임  (0) 2021.05.18
[C++] 프로그래머스 - 캐시  (0) 2021.05.14

검색 태그