#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main(){
//freopen("input.txt","r",stdin);
char a[76];
string countries[3000]; // record countries
string buffer; //country to compare
char tmp[100];
int country_count[3000]; // count the girls each country
memset(country_count,0,sizeof(country_count));
int n;
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++){
fgets(a,sizeof(a),stdin); //讀取一整行
for(int j=0;j<76;j++){
if(a[j]!=' ')tmp[j]=a[j]; //在第一個空白出現之前,將字元存入tmp
else break; //讀取到空白就跳出,如此僅拿取前面的國家,後面名字不理會
}
countries[i] = tmp; // tmp 指定給 countries[i]
}
sort(countries,countries+n); //按字典排序
int count=0;
for(int i=0;i<n;i++){
if(i==0){
buffer = countries[i]; //一開始先將第一個國家存入暫存
}
if(buffer == countries[i]){ //如果接下來讀取的國家和暫存器相同
country_count[count]++; //計數器+1
}
if(buffer != countries[i] || i == n-1){ //如果讀取的國家和暫存不同,或是讀取到最後一個項目
cout << buffer << " " << country_count[count] << '\n'; //輸出暫存器國家,和該國家的計數
count++; //切換到下一個國家的計數
buffer = countries[i]; //切換到下一個國家,存入暫存
country_count[count]++; //新國家的計數器+1
}
}
return 0;
}
2014年10月20日星期一
[UVa] 10420 - List of Conquests
訂閱:
發佈留言 (Atom)
-
因為先前寫UVa時,檔案名稱有時會花心思改,有時就直接把題目名稱加上.cpp就貼上了 導致現在有不同的格式出現 現在要處理的事情很簡單 1. 去除空白 2. 將底線 ( _ ) 換成dash ( - ) 經過一番查詢,終於發現最簡單的方法 - re...
-
文章出處: http://infbugs.blogspot.tw/2011/12/c_20.html 謝謝沙耶,解答了我長久以來對於 input/output 的疑惑。 C 語言入門 - 在線上批改系統練功 如何練習使用基本語法 自己出個練習題試著寫...
-
一開始用數學方法推斷得出,設輸入為n k為在n的前一斜線列數,故只要找到 (1+k)*k/2 < n 的最大k值,即可判定 k%2 == 1 => ((2*n)-(k*k)-k)/2 / ((k*k)+(3*k)-(2*n)+4)/2 k%2 ==...
沒有留言:
發佈留言