♐ CSP_J 组真题
2018年
1. 标题统计

线上OJ

一本通:http://ybt.ssoier.cn:8088/problem_show.php?pid=1978 (opens in a new tab)
AcWing:https://www.acwing.com/problem/content/475/ (opens in a new tab)
洛谷:https://www.luogu.com.cn/problem/P5015 (opens in a new tab)

核心思想:

这道题考察的是 cin.getline() 的用法。cin在遇到空格时就会自动结束,所以只用 cin 肯定不行。

💡

cin.getline() 从输入流中读取最多 255 个字符(本题范围为仅5),如果在读取 255 个字符之前遇到换行符('\n')时将停止读取。
注意,cin.getline() 会读取并丢弃换行符,所以在使用 cin.getline() 时其实可以不判断换行符('\n')。

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
  char s[10];
  cin.getline(s, 10);
  int len = strlen(s), cnt = 0;
  
  for(int i = 0; i < len; ++i)
      if(s[i] != ' ' && s[i] != '\n')
          cnt++;
 
  cout << cnt;
  return 0;
}