美团2024届秋招笔试第二场编程真题

慈云数据 8个月前 (03-25) 技术支持 51 0

在这里插入图片描述

要么是以0开头 要么以1开头 选择最小的答案累加

IMPort Java.util.SCanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(string[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) {
             // 注意 while 处理多个 case
            //  in.nextLine();
            String s = in.nextLine();
            //一种是以1开头的子串 一种是以0开头的子串 求最小值求和
            // System.out.println(s);
            long res=0;
            for(int i=0;i
                long res0=0;
                long res1=0;
                for(int j=i;j
                    //偶数位 0开头为1 1开头为0
                    if((j+1-i)%2==0){
                        if(s.charAt(j)=='1'){
                           res1++;
                        }else if(s.charAt(j)=='0'){
                            res0++;
                        }
                    }else if((j+1-i)%2==1){
                        if(s.charAt(j)=='1'){
                            res0++;
                        }else if(s.charAt(j)=='0'){
                            res1++;
                        }
                    }
                    // System.out.println(res0);
                    //  System.out.println(res1);
                    res+=Math.min(res0,res1);
                }
            }
            System.out.println(res);
        }
    }
}
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon