【編程題】騰訊筆試第一題(Java實現)
題目描述
有一天,小Q發現了n個被上鎖的寶箱和m串鑰匙。第i個寶箱上寫着一個整數a,第串鑰匙上寫着一個整數b。 小Q已經通過一些古籍得知了這些寶箱內有很多的珍貴的寶物。所以他想盡可能多地打開這些寶箱。
當且僅當第i個寶箱上的數字ai;與第j串鑰匙上的數字bj;之和爲奇數的時候,這個寶箱才能被這串鑰匙打開。每個寶箱只能被打開一次,且每一串鑰匙也只能被使用一次。
現在小Q想知道他最多能開多少個寶箱,請你幫他計算出這個結果。
輸入描述:
第一行兩個整數,n和m,表示寶箱的數量和鑰匙的數量第二行n個整數,a1,ag.-an, 表示每個寶箱上的數字第三行m個整數,b2,2…b. 表示每個鑰匙上的數字每兩個數字之間用一個空格分隔
滿足1<=n,m<=10^ 5,1<=ai<=10^ 9,1<=bi<=10^ 9
輸出描述:
一個整數,表示最多能打開的寶箱的數量
題目代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int[] a=new int[n];
int[] b=new int[m];
int aj=0;
int ao=0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
if((a[i]&1)==0)
ao++;
else
aj++;
}
int bj=0,bo=0;
for(int i=0;i<m;i++){
b[i]=sc.nextInt();
if((b[i]&1)==0)
bo++;
else
bj++;
}
int max=Math.min(bj,ao)+Math.min(aj,bo);
System.out.println(max);
sc.close();
}
}