在前幾天參加了一次某個大公司的筆試,前面的題全是邏輯題圖形不過還好,到了最后是兩道編程題.其中一道是這樣的:輸入兩串字符,開始對比,ab與bb輸出1,a與b不同就加一,b與b相同就不加,如果相同就不加.ababab與bb就輸出5.當時因為個人原因沒寫成,今天寫寫試試:
import java.util.Scanner;
public class wordContrast {
public static void Contrast (String firstWord, String sendWord) {
char f[] = firstWord.toCharArray();
char s[] = sendWord.toCharArray();
int num = 0;
for (int i = 0; i < f.length - s.length + 1; i++) {
int k = i;
for (int j = 0; j < s.length; j++) {
if (f[k] != s[j]) {
num = num +1;
}
//System.out.println("f[i]"+f[k]+k+"=="+"s[j]"+s[j]+j+"==");
k++;
}
}
System.out.println(num);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String firstWord = sc.next();
String sendWord = sc.next();
Contrast(firstWord, sendWord);
}
}