題目連結
題意:
一個將領在戰場上,要求自己跟敵方的士兵數量差距。
測資是彼此的士兵數。
解法:
求兩數之差即可,但數字可能到2的32次方,所以資料結構要用long。
可以用絕對值求解,或是先判斷誰大,再扣掉對方。
程式(Java):
題意:
一個將領在戰場上,要求自己跟敵方的士兵數量差距。
測資是彼此的士兵數。
解法:
求兩數之差即可,但數字可能到2的32次方,所以資料結構要用long。
可以用絕對值求解,或是先判斷誰大,再扣掉對方。
程式(Java):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Main{ | |
public static void main(String args[]) { | |
Scanner input = new Scanner(System.in); | |
while (input.hasNext()) { | |
BigInteger A = input.nextBigInteger(); | |
BigInteger B = input.nextBigInteger(); | |
System.out.println(A.subtract(B).abs()); | |
//這題範圍還在long內,也可以這樣寫 | |
//long A = input.nextLong(); | |
//long B = input.nextLong(); | |
//System.out.println(Math.abs(A-B)); | |
//或是如下 | |
//long A = sc.nextLong(); | |
//long B = sc.nextLong(); | |
//long ans = A>B ? A-B : B-A; | |
//System.out.println(ans + ""); | |
} | |
} | |
} |
留言
張貼留言