#UVa:10226-Hardwood Species

灆洢 2016-07-30 08:21:25

將所有物種的數量記下來算比例即可。

C++(0.680)

/*******************************************************/
/* UVa 10226 Hardwood Species                          */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2016/07/30                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

int main(){
  int numTestcase;
  while( scanf("%d", &numTestcase) != EOF ){
    getchar(); // for '\n'

    string input;
    getline(cin, input); // for blank line

    for( int testcase = 0 ; testcase < numTestcase ; ++testcase ){
      if( testcase > 0 ){
        printf("\n");
      }

      map<string, int> numSpecies;
      int total = 0;
      while( getline(cin, input) && input != "" ){
        ++numSpecies[input];
        ++total;
      }

      for( map<string, int>::iterator it = numSpecies.begin();
           it != numSpecies.end() ; ++it ){
        printf("%s %.4f\n", it->first.c_str(), (double)it->second / total * 100);
      }

    }

  }
  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料