public class first {
public static void print(String s) {
System.out.print(s);
}
public static int getMaxofTwo(int n) {
int i=0;
while(true) {
double t = Math.pow(2, i);
if(t>n) {
return i-1;
}else if(t==n) {
return i;
}
i++;
}
}
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
int n = input.nextInt();//得到输入
int t,a;
ArrayList<Integer> num = new ArrayList<>();
while(true) {
t = getMaxofTwo(n);
num.add(t);
a = (int) Math.pow(2,t);
if(a==n) {//找到最后一个指数
break;
}
n = n - a;
}//求出所有的指数
//格式化输出好看点
for(int i=0;i<num.size()-1;i++) {
print(2+"^"+num.get(i)+"+");
}
System.out.println(2+"^"+num.get(num.size()-1));
}
}
Java:2的幂次方表示
最新推荐文章于 2022-01-04 19:41:48 发布