题解 | #进制转换# | Rust
进制转换
https://www.nowcoder.com/practice/2cc32b88fff94d7e8fd458b8c7b25ec1
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 进制转换 * @param M int整型 给定整数 * @param N int整型 转换到的进制 * @return string字符串 */ pub fn solve(&self, M: i32, N: i32) -> String { let mut ans = String::new(); let mut M = M; let mut positive = true; if M < 0 { positive = false; M = -M; } while M > 0 { let m = M%N; M/=N; if m < 10 { ans.push(('0' as u8 + m as u8)as char); } else { ans.push(('A' as u8 + (m - 10) as u8) as char); } } if !positive { ans.push('-'); } ans.chars().rev().collect() } }