Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

83 Zeilen
1.7 KiB

vor 2 Jahren
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "生活中编码无处不在,如身份证号、银行卡号、邮政编 码、学籍号、车牌号及条形码、二维码等,都是按照一定的规则产生的编码。"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "__下面的代码定义了十进制转化为二进制的功能,动手验证一下吧!__"
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 1,
  20. "metadata": {},
  21. "outputs": [
  22. {
  23. "name": "stdout",
  24. "output_type": "stream",
  25. "text": [
  26. "请输入一个数字: 9\n"
  27. ]
  28. },
  29. {
  30. "data": {
  31. "text/plain": [
  32. "'1001'"
  33. ]
  34. },
  35. "execution_count": 1,
  36. "metadata": {},
  37. "output_type": "execute_result"
  38. }
  39. ],
  40. "source": [
  41. "def my_bin(num):\n",
  42. " la = []\n",
  43. " if num < 0:\n",
  44. " return '-' + my_bin(abs(num))\n",
  45. " while True:\n",
  46. " num, remainder = divmod(num, 2)\n",
  47. " la.append(str(remainder))\n",
  48. " if num == 0:\n",
  49. " return ''.join(la[::-1])\n",
  50. "c = input(\"请输入一个数字: \")\n",
  51. "my_bin(int(c))"
  52. ]
  53. },
  54. {
  55. "cell_type": "code",
  56. "execution_count": null,
  57. "metadata": {},
  58. "outputs": [],
  59. "source": []
  60. }
  61. ],
  62. "metadata": {
  63. "kernelspec": {
  64. "display_name": "Python 3",
  65. "language": "python",
  66. "name": "python3"
  67. },
  68. "language_info": {
  69. "codemirror_mode": {
  70. "name": "ipython",
  71. "version": 3
  72. },
  73. "file_extension": ".py",
  74. "mimetype": "text/x-python",
  75. "name": "python",
  76. "nbconvert_exporter": "python",
  77. "pygments_lexer": "ipython3",
  78. "version": "3.7.3"
  79. }
  80. },
  81. "nbformat": 4,
  82. "nbformat_minor": 2
  83. }