import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int m = scan.nextInt();
int n = scan.nextInt();
StringBuilder s = new StringBuilder();
String table = "0123456789ABCDEF";
boolean flg = false;
if (m < 0) {
m = -m;
flg = true;
} else if (m == 0) {
s.append('0');
}
while (m != 0) {
s.append(table.charAt(m % n));
m /= n;
}
if (flg == true) {
s.append("-");
}
s.reverse();
System.out.println(s);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();//要转换的数字
int base = sc.nextInt();//要转换的进制
int tmp = num;
StringBuilder str = new StringBuilder();//把转换好的进制放到这里
String table = "0123456789ABCDEF";
boolean flg = false;//定义一个布尔变量标记num是正还是负
if(num < 0) {
//负数:要先变成正数才好操作
num = -num;
flg = true;//是负数
}
while(num != 0) {
str.append(table.charAt(num % base));
num /= base;
}
if(flg == true) {
str.append("-");//是负数的话,把之前删下去的负号给他加回来
}
if(tmp == 0) {
System.out.println("0");
} else {
System.out.println(str.reverse());
}
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
StringBuilder sb = new StringBuilder();
String cur = "0123456789ABCDEF";
boolean flg = false;
if(m == 0){
System.out.println(m);
}
if(m < 0){
m = -m;
flg = true;
}
while(m != 0){
sb.append(cur.charAt(m%n));
m /= n;
}
if(flg){
sb.append("-");
}
sb.reverse();
System.out.println(sb);
}
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int M = scan.nextInt();
int N = scan.nextInt();
boolean flags = true;
if(M == 0){
System.out.print(M);
}
if(M < 0){
M = -M;
flags = false;
}
String str = "0123456789ABCDEF";
Stack<Integer> stack = new Stack<>();
while(M%N != 0||M/N !=0){
stack.push(M%N);
M = M/N;
}
int len = stack.size()-1;
if(!flags){
System.out.print("-");
}
for(int i = len ; i>=0 ; i--){
System.out.print(str.charAt(stack.pop()));
}
}
} import java.util.*;
public class Main{
static StringBuffer sb = new StringBuffer();
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(); //7 8
int n = sc.nextInt(); //2 111 3 22
//111
//7,2,3
if(m<0){
a(Math.abs(m),n);
sb.append('-');
}else{
a(m,n);
}
String num = sb.reverse().toString();
System.out.println(num);
}
public static void a(int m,int n){
if(m%n >= 0){
if(m%n == 10){
sb.append('A');
}else if(m%n == 11){
sb.append('B');
}else if(m%n == 12){
sb.append('C');
}else if(m%n == 13){
sb.append('D');
}else if(m%n == 14){
sb.append('E');
}else if(m%n == 15){
sb.append('F');
}else{
sb.append(m%n) ;
}
if(m/n != 0){
a((m/n),n);
}
}else{
return ;
}
}
}
import java.util.Scanner;
public class Main37 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int basic=sc.nextInt();
StringBuffer sb=new StringBuffer();
String diff;
if(num<0) {
num=Math.abs(num);
while(num!=0) {
diff=Integer.toString(num%basic);
if(Integer.parseInt(String.valueOf(diff))>=10) {
diff=String.valueOf((char)(65+(Integer.parseInt(String.valueOf(diff))-10)));
}
num=num/basic;
sb.append(diff);
}
sb.append("-");
System.out.println(sb.reverse().toString());
}else {
while(num!=0) {
diff=Integer.toString(num%basic);
if(Integer.parseInt(String.valueOf(diff))>=10) {
diff=String.valueOf((char)(65+(Integer.parseInt(String.valueOf(diff))-10)));
}
num=num/basic;
sb.append(diff);
}
System.out.println(sb.reverse().toString());
}
}
}
辗转相除法+ASCII码转换
import java.util.Arrays;
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int m = in.nextInt(); int n = in.nextInt(); System.out.println(zhuanHuan(m, n)); }
public static String zhuanHuan(int m,int n) { String tables = "0123456789ABCDEFG"; String result = ""; if(n==0 || n==1) { return Integer.valueOf(m).toString(); } if(m == 0) { return "0"; } int x = m >0 ? m : -m; while(x!=0) { int index = x % n; x = x / n; result = tables.charAt(index) + result; } if(m > 0) { return result; }else { return "-"+result; } }
}
import java.util.*;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// //N进制转换器
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt();
int N = scanner.nextInt();
//查询表,
String table = "0123456789ABCDEF";
int flag = 1;
if (M < 0) {
M = -M;
flag = 0;
}
Stack S = new Stack();
decimalBaseToNBaseConvertor(M, N, S, table);
//output
if (flag == 0) {
S.push('-');
}
while (!S.empty()) {
System.out.print(S.pop());
}
}
//十进制转二进制,占用空间较多;递归算法;
/*改进:
1. 使用堆栈来存储余数,并且利用堆栈的特点,倒序输出余数的值,N进制.堆栈是wrapper Character Object
2. 输入用Scanner类型到存储System.in的值.(Java语言不熟悉)
3. Object 有String 和Character.A Java String is an object of the class java.lang.
对象有方法.比C++的String更加方面(单纯char的数组).
This Character class also offers a number of useful class (i.e., static) methods
for manipulating characters.
* 时间复杂度:
* 空间复杂度:*/
public static void decimalBaseToNBaseConvertor(int M, int N, Stack<Character> S, String table) {
if (M == 0) {
return;
}
//入栈
S.push(table.charAt(M % N));
//下一次
decimalBaseToNBaseConvertor(M / N, N, S, table);
}
}
import java.util.Scanner;
import java.util.Stack;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int tar = sc.nextInt();
convert(num,tar);
}
public static void convert(int num, int tar){
boolean simple = true;
if (num < 0) {
num = -num;
simple = false;
}
String[] strs = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
Stack stack = new Stack();
while((num)!=0){
stack.push(strs[(num%tar)]);
num = num/tar;
}
if (!simple){
System.out.print("-");
}
while (stack.size() != 0){
System.out.print(stack.pop());
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int x = scanner.nextInt();
String string = "";
boolean flag = false;
if (num < 0) {
flag = true;
num = -1 * num;
}
while (num / x != 0 || num % x != 0) {
int tmp = num % x;
if (tmp < 10) {
string = num % x + string;
}else {
char ch = (char) ('A' + (tmp - 10));
string = ch + string;
}
num /= x;
}
if (flag)
string = "-" + string;
System.out.println(string);
}
}