華為2019.9.18筆試第一題:
判斷數據是否合理,給了三種合理的情況,一個是兩位數與一位數交替出現,一個是兩頭是兩位數,中間全是一位數,最后一種情況是兩頭是一位數,中間全是兩位數。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
List<String> list = new ArrayList<String>();
String s;
while(true){
if((s = scanner.nextLine()).equals("")){
break;
}
list.add(s);
}
for(int i = 0; i < list.size(); i++){
String string = list.get(i);
String temp[] = string.split(" ");
int res[] = new int[temp.length];
for(int j = 0; j < temp.length; j++){
res[j] = Integer.parseInt(temp[j]);
}
System.out.print(isequ(res) + " ");
}
}
public static boolean isequ(int a[]) {
if(a[0] >= 10 && a[a.length - 1] >= 10){
for(int i = 1; i < a.length - 1; i++){
if(a[i] >= 10){
return false;
}
}
}else if (a[0] < 10 && a[a.length - 1] < 10) {
for(int i = 1; i < a.length - 1; i++){
if(a[i] < 10){
return false;
}
}
}else {
for(int i = 0; i < a.length; i++){
if(i % 2 == 0 && a[i] >= 10){
return false;
}else if(i % 2 == 1 && a[i] < 10){
return false;
}
}
}
return true;
}
}
