You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

83 lines
1.7 KiB

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"生活中编码无处不在,如身份证号、银行卡号、邮政编 码、学籍号、车牌号及条形码、二维码等,都是按照一定的规则产生的编码。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__下面的代码定义了十进制转化为二进制的功能,动手验证一下吧!__"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"请输入一个数字: 9\n"
]
},
{
"data": {
"text/plain": [
"'1001'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def my_bin(num):\n",
" la = []\n",
" if num < 0:\n",
" return '-' + my_bin(abs(num))\n",
" while True:\n",
" num, remainder = divmod(num, 2)\n",
" la.append(str(remainder))\n",
" if num == 0:\n",
" return ''.join(la[::-1])\n",
"c = input(\"请输入一个数字: \")\n",
"my_bin(int(c))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}