全部评论
import java.util.Scanner;
public class Main {
private static final int LOWER_CASE = 1;
private static final int UPPER_CASE = -1;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
String text = scanner.nextLine();
scanner.close();
int curr = LOWER_CASE;
int count = 0;
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (curr == LOWER_CASE && isLower(ch)) {
count += 1;
} else if (curr == UPPER_CASE && !isLower(ch)) {
count += 1;
} else {
count += 2;
if (i != text.length() - 1) {
char next = text.charAt(i + 1);
if (isLower(next) && isLower(ch)) {
curr *= -1;
} else if(!isLower(next) && !isLower(ch)){
curr *= -1;
}
}
}
}
System.out.println(count);
}
private static boolean isLower(char ch) {
return ch >= 'a' && ch <= 'z';
}
}
一个true跟false写反了,一直0 ac,愣是没检查出来,我可能是个瞎子
18%不知道为啥。。。
0,我太难了
放弃
shift咋考虑的呀
36 一交卷就知道错哪儿了😤
27。zz
贪心法可以,100%ac
package main
import (
"fmt"
)
func main() {
var n int
fmt.Scan(&n)
var str string
fmt.Scan(&str)
is_capslocks := false
count := 0
Rune := []rune(str)
for i := 0; i < n; i++ {
if Rune[i] >= 'A' && Rune[i] <= 'Z' {
if is_capslocks == true {
count++
} else {
if Rune[i+1] >= 'A' && Rune[i+1] <= 'Z' {
is_capslocks = !is_capslocks
}
count += 2
}
} else {
if is_capslocks == false {
count++
} else {
if Rune[i+1] >= 'a' && Rune[i+1] <= 'z' {
is_capslocks = !is_capslocks
}
count += 2
}
}
}
fmt.Println(count)
}
每串连续一样的算一组,按贪心的策略进行下去即可,因为当shift和lock方式按键数量一样时,一定是选择shift
import sys
n = int(input())
S = str(input())
S_list = list(S)
lower_s = [chr(i) for i in range(97, 123)]
upper_s = [chr(i) for i in range(65, 91)]
num = 0
u_bool = False # True:当前输入法为大写
for i,s in enumerate(list(S_list)):
if s in lower_s:
if not u_bool:
num += 1 # 如果当前输入法为小写,输入一次字母
elif i < len(S_list) - 1:
if S_list[i + 1] in lower_s :
num += 2 # 一次capslock,一次当前字母
u_bool = False # 输入法切换为小写
else:
num += 2 # 一次shift,一次当前字母
else:
num += 2 # 最后一个字母,不再检测下一个s
if s in upper_s:
if u_bool:
num += 1
elif i < len(S_list) - 1:
if S_list[i + 1] in upper_s:
num += 2
u_bool = True
else:
num += 2
else:
num += 2
sys.stdout.write(str(num))
AC200% https://www.nowcoder.com/discuss/232706
相关推荐
11-06 20:05
门头沟学院 Java 点赞 评论 收藏
分享