ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

东方博宜OJ 1164:字符统计 ← 字符串

2026/8/10 1:01:57 拓冰建站 浏览量
东方博宜OJ 1164:字符统计 ← 字符串

【题目来源】
https://oj.czos.cn/p/1164

【题目描述】
输入一串小写字母(以 . 为结束标志),统计出每个字母在该字符串中出现的次数(若某字母不出现,则不要输出)。
要求:每行输出 5 项,每项以空格隔开。

【输入格式】
输入一行以 . 结束的字符串(字符串长度≤105)。

【输出格式】
输出相应小写字母的个数。

【输入样例】
aaaabbbccc.

【输出样例】
a:4 b:3 c:3

【数据范围】
字符串长度≤105。

【算法分析】
● 执行 t[s[i]-'a']++; 时,要注意特判一下字符串末尾的点 ·。
●​​​​​​​ 每输出 5 项后换行,执行 if(cnt>0 && cnt%5==0) cout<<endl;
●​​​​​​​ 将 0~25 转换为 'a'~'z',执行 char x=i+'a';

【算法代码】

#include <bits/stdc++.h>
using namespace std;int t[26];int main() {string s;getline(cin,s);for(int i=0; i<s.size(); i++) {if(s[i]>='a' && s[i]<='z') {t[s[i]-'a']++;}}int cnt=0;for(int i=0; i<26; i++) {if(t[i]!=0) {if(cnt>0 && cnt%5==0) cout<<endl;char x=i+'a';cout<<x<<":"<<t[i]<<" ";cnt++;}}return 0;
}/*
in:
aaaabbbccc.out:
a:4 b:3 c:3
*/




【参考文献】
https://blog.csdn.net/carbohydratesE/article/details/128392841
​​​​​​​https://blog.csdn.net/like_astar/article/details/128984236
https://blog.csdn.net/LYLYC_3/article/details/139996436