『题解』洛谷P5015 标题统计
2024-09-01 16:34:27
题意描述
给你一个字符串,求所有字符的总数。
字符只包含数字,大小写字母。
分析
字符串的长度还是\(\le5\)的。
直接枚举就可以了。
AC代码:
NOIP官方标准程序是这样的
#include <iostream>
#include <cstdlib>
#include <string>
int main() {
freopen("title.in", "r", stdin);
freopen("title.out", "w", stdout);
std::string s;
std::getline(std::cin, s);
int cnt = 0;
if (s.length() > 0 && s[0] != ' ') ++cnt;
if (s.length() > 1 && s[1] != ' ') ++cnt;
if (s.length() > 2 && s[2] != ' ') ++cnt;
if (s.length() > 3 && s[3] != ' ') ++cnt;
if (s.length() > 4 && s[4] != ' ') ++cnt;
std::cout << cnt << std::endl;
return 0;
}
直接枚举\(5\)个位。
还是我的比较可观:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
char s[10];
int main() {
gets(s);//读入字符串(不推荐用,再Linux下的换行符和Windows下的不一样)
int ans=0;//清空ans
for (int i=0; i<strlen(s); i++) {//别忘了C++字符串下标是从0开始的
if (s[i]>='A' &&s[i]<='Z') ans++;
if (s[i]>='a' &&s[i]<='z') ans++;
if (s[i]>='0' &&s[i]<='9') ans++;//数字
}
printf("%d\n",ans);//输出结果
return 0;
}
最新文章
- 022 UFT虚拟对象
- python basic programs
- Eclipse安装ADT失败解决办法
- Linux IPC(Inter-Process Communication,进程间通信)之管道学习
- 用java理解程序逻辑小结
- PageRank之基于C C#的基本实现
- Mac下显示隐藏的文件
- Django+Bootstrap+Mysql 搭建个人博客(三)
- idea 快键键
- ERROR: Field * doesn&#39;t have a default value
- Jenkins小试
- 查看Selinux和关闭Selinux
- HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP错误解决方法
- 新建maven web 项目后,出现的小问题
- C/C++的Name Mangling
- SpringMvc注解开发
- [javaSE] 数组(获取最值)
- 造成MySQL全表扫描的原因
- Android调试大法 自定义IDE默认签名文件==>;微信支付、微信登录、微信分享,debug时调试通过,release时调不起微信
- C语言——无向带权图邻接矩阵的建立