题解 | #数制转换#

数制转换

https://www.nowcoder.com/practice/8ef02ef8571b417d8c311a87861f7a03

//Java版代码
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int n = Integer.parseInt(scanner.next(), a);
        int b = scanner.nextInt();
        System.out.println(Integer.toString(n, b).toUpperCase());
    }
}
#Python版代码
a, n, b = input().split()
a = int(a)
n = int(n, a)
b = int(b)  
ans = []
while n:
    temp = n % b
    if temp >= 10:
        ans.append(chr(temp % 10 + ord("A")))
    else:
        ans.append(chr(temp + ord("0")))
    n //= b
print("".join(ans[::-1]))

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务