问题描述
编写递归函数,实现将一个十进制的正整数以二进制的形式输出。
注:不考虑符号位,仅从二进制数不为0的最高位开始输出。
输入格式
一个十进制正整数
输出格式
该整数对应的二进制数
样例输入
一个满足题目要求的输入范例。
例:
96
样例输出
与上面的样例输入对应的输出。
例:
1100000
数据规模和约定
输入为正整数
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class llq1{
static List<Integer>list=new ArrayList<Integer>();
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
while(n!=0) {
list.add(n%2);
n/=2;
}
Collections.reverse(list);
for(Integer s2:list)
System.out.print(s2);
}
}
注意 list的遍历