下面我将用两种题解解答这个题目
简单模拟题
方法一:
1.因为开始就是大写锁定,所以如果第一个字母是小写那么就已经按了一次转换键
2.接着模拟一遍,从头开始,如果当前是小写字母下一个是大写字母那么就++,如果当前是大写字母下一个是小写字母那么也需要++
3.最后得出来的就是答案
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
char s[maxn];
int main()
{
while (~scanf("%s", s))
{
int ans = 0;
int ls = strlen(s);
if (islower(s[0]))
++ans;
for (int i = 0; i < ls; ++i)
{
if (isupper(s[i]) && islower(s[i + 1]))
++ans;
else if (islower(s[i]) && isupper(s[i + 1]))
++ans;
}
printf("%d\n", ans);
}
}方法二:
装换为01串,以为大写小写字母都有26个,如果转换为01串就很好比较了,此时大小写的区别就只有一个
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
char s[maxn];
int main()
{
while (~scanf("%s", s))
{
int ans = 0;
int ls = strlen(s);
for (int i = 0; i < ls; ++i)
{
if (islower(s[i]))
s[i] = '0';
else
s[i] = '1';
}
if (s[0] == '0')
++ans;
for (int i = 0; i < ls - 1; ++i)
if (s[i] != s[i + 1])
++ans;
printf("%d\n", ans);
}
}
京公网安备 11010502036488号