本文主要是介绍codeforces 486A(Calculating Function) Java,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
日常水题,秒一波!!!
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;/*** 题意:f(n) = -1+2-3+4-5+6-7... 。f(4) = - 1 + 2 - 3 + 4 = 2 ; f(5) = - 1 + 2 - 3 + 4 - 5 = - 3** @author TinyDolphin* 2017/7/3 15:55.*/
public class Main {public static void main(String[] args) throws IOException {Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));BigInteger inputN;BigInteger ans;while (in.hasNext()) {inputN = in.nextBigInteger();//判断一个数是否是偶数:inputN & 1 == 0 时,为偶数。如:4==> 0100 & 0001 = 0000 ;5==> 0101 & 0001 = 0001//ans = inputN & 1 == 0 ? inputN / 2 : -(inputN / 2 + 1);//下面为转化为大整数进行运算ans = inputN.and(BigInteger.valueOf(1)).compareTo(BigInteger.valueOf(0)) == 0 ? inputN.divide(BigInteger.valueOf(2)) : inputN.divide(BigInteger.valueOf(2)).add(BigInteger.valueOf(1)).negate();out.println(ans);}out.flush();}
}
这篇关于codeforces 486A(Calculating Function) Java的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!