http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=34243
這題就使用貪婪比較從前面轉或從後面轉比較少次 大小比對這部分我是使用問號運算式當為真執行冒號前面,反之
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
while(n--!=0){
char str1[]=scn.next().toCharArray(),str2[]=scn.next().toCharArray();
int tot=0;
for(int i=0;i<str1.length;i++){
double a=str1[i]-'0',b=str2[i]-'0';
if(a<b){
double temp=b;
b=a;
a=temp;
}
tot+=Math.abs(a-b)>Math.abs(10-a)+b?(int)Math.abs(10-a)+b:(int)Math.abs(a-b);
}
System.out.println(tot);
}
}
/*題目:ITSA 第53次月賽 [Problem 3] 號碼鎖最少轉動幾次
作者:1010
時間:西元 2017 年 2 月 */
}