n 代表人數 (最多 2000)n 行輸入每個人的國籍與名字 (最多 75 個字元)n 後,用 while 迴圈重複輸入國名與人名存到二維字元陣列中,因為可能包含空白,所以用 gets(),但因為人名其實不重要 (因為是要求各國人數),每次都覆蓋過去即可
int n;
int i;
char country[2001][76] = {0};
char name[76] = {0};
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf("%s", country[i]);
gets(name);
}
strcmp 能夠比較字典序的特性 (回傳值會不同),用簡單的泡沫排序來實作
for(i = 0; i < n - 1; i++){
for(j = 0; j < n - 1 - i; j++){
if(strcmp(country[j], country[j + 1]) > 0){
strcpy(temp, country[j]);
strcpy(country[j], country[j + 1]);
strcpy(country[j + 1], temp);
}
}
}
flag 的狀態來控制輸出國名或次數
while 迴圈及 i 變數來遍歷字元陣列flag == false 就輸出國名,並改變狀態開始計算次數flag == true 且當前國名和下個國名相同 (回傳值是 0) 就繼續累加;反之則輸出次數並初始 flag 及次數flag = false;
count = 0;
i = 0;
while(i < n){
if(!flag){
printf("%s ", country[i]);
count++;
flag = true;
}
else if(flag){
if(strcmp(country[i], country[i + 1]) == 0){
count++;
}
else{
printf("%d\n", count);
flag = false;
count = 0;
}
i++;
}
}
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
int main(){
int n;
int i, j;
int count, flag;
char country[2001][76] = {0};
char name[76] = {0};
char temp[76] = {0};
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf("%s", country[i]);
gets(name);
}
for(i = 0; i < n - 1; i++){
for(j = 0; j < n - 1 - i; j++){
if(strcmp(country[j], country[j + 1]) > 0){
strcpy(temp, country[j]);
strcpy(country[j], country[j + 1]);
strcpy(country[j + 1], temp);
}
}
}
flag = false;
count = 0;
i = 0;
while(i < n){
if(!flag){
printf("%s ", country[i]);
count++;
flag = true;
}
else if(flag){
if(strcmp(country[i], country[i + 1]) == 0){
count++;
}
else{
printf("%d\n", count);
flag = false;
count = 0;
}
i++;
}
}
return 0;
}
map 來實作,因為每個元素都唯一且排序,直接 key 放國名,value 放次數
.insert() 或 .emplace() 都可以;遍歷也可改用 iterator 的標準寫法#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
string str;
map<string, int> mp;
cin >> n;
while(n--){
cin >> str;
mp[str]++;
getline(cin, str);
}
for(auto i: mp){
cout << i.first << " " << i.second << endl;
}
return 0;
}