#UVa:10420-List of Conquests

灆洢 2015-01-02 23:08:56

計算國家的數量並照國家的名稱排序輸出即可。

P.S. 女生的名字並沒規定會有幾個字,若假定只有兩個字的話會WA。

C++(0.022)

/*******************************************************/
/* UVa 10420 List of Conquests                         */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2015/01/02                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;

int main(){
  int n;
  string country[2005];
  while( scanf("%d", &n) != EOF ){

    string name;
    for( int i = 0 ; i < n ; ++i ){
      cin >> country[i];
      getline(cin, name);
    }
  }

  sort( country, country+n );

  for( int i = 0 ; i < n ; ++i ){
    printf("%s", country[i].c_str() );

    int count = 0;
    int j;
    for( j = i ; j < n ; ++j ){
      if( country[i] != country[j] ){
        break;
      }

      ++count;
    }

    printf(" %d\n", count);
    i = j-1;
  }

  return 0;
}

發表迴響

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