题目解析
输出2的指定数的次方
解题思路
可以利用 位运算 求出2的次方
或者 自带函数 即可求出任意数的次方
代码
位运算
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n = sc.nextInt();
out.printf("2^%d = %d", n, 1 << n);
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
自带函数
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n = sc.nextInt();
out.printf("2^%d = %d", n, (int) Math.pow(2, n));
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
切记 java 自带类库中 Math.pow() 的返回值为 double 类型