codeforces 解題報告 556A. Case of the Zeros and Ones

http://codeforces.com/problemset/problem/556/A

解題思路:

1.計算0和1的個數,答案就是大減小

import java.util.Scanner;

public class Main {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int a0 = 0,a1 = 0;
        String str = sc.nextLine();
        str = sc.nextLine();
        for(int i = 0;i < str.length();i++) {
            if(str.charAt(i) == '1') {
                a1++;
            } else if(str.charAt(i) == '0'){
                a0++;
            }
        }

        System.out.println(Math.abs(a0 - a1));

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章