科大讯飞笔试第二题 - 模拟开冲
public static void main(String[] args) {
int n,k;
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
k = scanner.nextInt();
String end = scanner.nextLine();
String line = scanner.nextLine();
int[] consume = new int[n];
char[] arr = line.toCharArray();
for(int i=0;i<n;i++){
consume[i] = arr[i] - 'a' + 1;
}
int now = 0;
int energy = k;
while(now+1<n){
int x = consume[now+1]-consume[now];
if(x>0){
energy -= x;
}else{
energy += Math.abs(x);
}
if(energy<0){
System.out.println(-1);
return;
}
now++;
}
System.out.println(energy);
}

int n,k;
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
k = scanner.nextInt();
String end = scanner.nextLine();
String line = scanner.nextLine();
int[] consume = new int[n];
char[] arr = line.toCharArray();
for(int i=0;i<n;i++){
consume[i] = arr[i] - 'a' + 1;
}
int now = 0;
int energy = k;
while(now+1<n){
int x = consume[now+1]-consume[now];
if(x>0){
energy -= x;
}else{
energy += Math.abs(x);
}
if(energy<0){
System.out.println(-1);
return;
}
now++;
}
System.out.println(energy);
}
全部评论
第三题蹲
相关推荐
牛客57693073...:最近找实习只有字节给面试,面完二面没消息了
点赞 评论 收藏
分享
King987:你有实习经历,但是写的也太简单了,这肯定是不行的,你主要要包装实习经历这一块,看我的作品,你自己包装一下吧,或者发我,我给你出一期作品
点赞 评论 收藏
分享
查看5道真题和解析