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;
}
<<: 细看seldon core所部署出来的POD在做什麽
>>: DAY 17 Big Data 5Vs – Variety(速度) Glue Data Brew
前言: 有了变数,就要知道如何用变数来判断下一步要作什麽事呀。来看看kotlin 条件判断 有什麽不...
若是键盘没有之前教的设定 next 与 go 的机制 打完字过後键盘就会收不起来 这边来跟大家分享...
随着我们专案功能的增加,虽然目前只有两个函数,但是我们的测试函数已经增加了不少。 为了减少我们未来阅...
用於双向的单次讯息传递,包括发送讯息、接收讯息两个功能 经过以上的实作,其实两端的通讯步骤都差不多,...
Promise 解决了回调地狱,在处理一些需要花费比较长时间的任务时使用 Promise 就可以进行...