科大讯飞笔试第二题 - 模拟开冲

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);
    }

全部评论
第三题蹲
点赞 回复 分享
发布于 2023-08-13 16:41 广东

相关推荐

仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-23 23:49
中南大学 Java
成绩一坨屎,英语6级没过,没读研,没考教资,没考计算机二级,没考公,没谈过恋爱,你们说我的这个大学生涯是不是混的有点失败啊?哎老中一生的容错还是太低了下辈子一定注意混好大学生涯不留遗憾
K1einMoretti:1.不保研 成绩没太大用 2.6级没过看用人企业要求了,基本上只要4级以上 3. 读不读研看自己选择,现在这环境螚先就业就先就业 4. 你不当老师考啥教资 5. 计算机二级没用(这证纯给国家上供) 6. 订婚***案了解一下?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务