输入的时候每一位以空格隔开,以字母结束输入
import java.util.Collections;
import java.util.Scanner;
import java.util.Stack;
public class Translate
{
public static void main(String[] args)
{
System.out.println("例子:");
outPut2(nTo2(inPutN()));
}
//输入一个多进制数,并存储起来
public static Stack
{
Stack
System.out.println("多少进制?");
Scanner screen=new Scanner(System.in);
s.add(screen.nextInt());
System.out.println("输入这个多进制数,各位之间用空格分开,以字母结束输入,例如123 34 fd:");
while(screen.hasNextInt())
s.push(screen.nextInt());
return s;
}
//将一个多进制数转化为2进制
public static Stack
{
Stack
int high=1;
while(high
int c=0;
for(int i=1;i
s.set(i, s.get(i)+c*s.get(0));
c=s.get(i)%2;
s.set(i, s.get(i)/2);
}
d.push((short)c);
if(s.get(high)==0)
high++;
}
Collections.reverse(d);
return d;
}
//输出一个二进制数
public static void outPut2(Stack
{
for(short it:out)
System.out.print(it);
System.out.println();
}
}
#include
#include
void decimal2Binary(int n) {
std::stack
int mod = 0;
while (n != 0) {
mod = n % 2;
n /= 2;
stack.push(mod);
}
while (!stack.empty()) {
std::cout << stack.top();
stack.pop();
}
}
int main() {
decimal2Binary(12);
return 0;
}
用c++写的你自己改一下