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

03-27 1370阅读

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

要么是以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);
        }
    }
}
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]