input
2 (按enter)
1 2 3 4 (按enter)
會先output: 1 2 3 4 5
1 2 4 (enter)
會先output: 1 2 4 8
----------
因為用java的scanner,是要按enter的....
不了解題目說的
input
2
1 2 3 4
1 2 4
要怎麼讀比較好…
----------------------------------
public static void a005(){
boolean tf = false;
int count = -1;
int check_count = 0;
System.out.println("please a number(0~20): ");
Scanner cin = new Scanner(System.in);
try{
// ArrayList arrayResult = new ArrayList();
while (cin.hasNext()) {
if(!tf){
count = Integer.parseInt(cin.nextLine());
tf=true;
}else{
StringBuffer result = new StringBuffer();
String data = cin.nextLine();
if(count>=0 && count<=20){
check_count++;
String[] num = data.split(" ");
boolean tf_deviate = true; //true: 等差數列;false: 則否.
int check_deviate = Integer.parseInt(num[1])-Integer.parseInt(num[0]); //等差
boolean tf_isocratic = true; //true: 等比數列;false: 則否.
int check_isocratic = Integer.parseInt(num[1])/Integer.parseInt(num[0]); //等比
result.append(num[0]+" ");
for(int j=1; j<num.length; j++){
result.append(num[j]+" ");
int tmp_deviate = Integer.parseInt(num[j])-Integer.parseInt(num[j-1]);
if(tmp_deviate!=check_deviate){
tf_deviate=false;
}
int tmp_isocratic = Integer.parseInt(num[j])/Integer.parseInt(num[j-1]);
if(tmp_isocratic!=check_isocratic){
tf_isocratic=false;
}
}
if(tf_deviate && !tf_isocratic){//等差
result.append((Integer.parseInt(num[num.length-1])+check_deviate));
}else if(!tf_deviate && tf_isocratic){//等比
result.append((Integer.parseInt(num[num.length-1])*check_isocratic));
}
System.out.println(result.toString());
// arrayResult.add(result.toString());
}
}
if(check_count==count){
break;
}
}
// for(int k=0; k<arrayResult.size();k++){
// System.out.println(arrayResult.get(k).toString());
// }
}catch(Exception ex){
System.out.println("please enter the correct data.");
ex.printStackTrace();
}
}