Java / C++ /Python 代码展示
Java 有内置函数
- public class Main {
-
- public static void main(String[] args) {
- int n = 8;
- System.out.println(Integer.toBinaryString(n));
- }
- }
-
- // 1000
C++ 的实现方式
需要自己结合下内置函数
-
-
- #include
- #include
-
- using namespace std;
-
- string getBinary(int n) {
-
- int index = 0;
- const int size = 32;
- string binary = bitset
(n).to_string(); - for(int i=0;i
size();i++) { - if(binary[i]=='1'){
- index = i;
- break;
- }
- }
- string res = binary.substr(index);
- return res;
- }
-
- int main() {
-
- cout<<getBinary(22)<
- cout<<getBinary(8)<
-
- return 0;
- }
Python 的实现方式:
- num = 123
- binary_str = bin(num)[2:] # 使用bin()函数将整数转换成二进制字符串,并去除前缀'0b'
- print(binary_str)
-
相关阅读:
oracle 与mysql兼容日期(格式:YYYY年MM月DD日)
LLM Saturation与多模态AI的崛起
区块链实验室(25) - FISCO中PBFT耗时特征
编译原理:编译原理简明教程知识点梳理(应对考试版)
《微信小程序-基础篇》带你了解小程序的路由系统(二)
分享5款有趣的软件,你都知道吗?
STM32进阶笔记——复位、时钟与滴答定时器
Opengl之多光源
如何避免JavaScript中的内存泄漏?
小技巧-mac切换修改wifi密码
-
原文地址:https://blog.csdn.net/IRON_MAN_LEVEL1/article/details/133994292