0
点赞
收藏
分享

微信扫一扫

Java设计模式之策略模式

朱悟能_9ad4 03-23 07:30 阅读 3

在这里插入图片描述
要么是以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<s.length()-1;i++){
                long res0=0;
                long res1=0;
                for(int j=i;j<s.length();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);
        }
    }
}
举报

相关推荐

0 条评论