RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1420342
Accepted
hellog888
hellog888
Asked:2022-08-15 03:51:16 +0000 UTC2022-08-15 03:51:16 +0000 UTC 2022-08-15 03:51:16 +0000 UTC

如何将线条向右对齐?

  • 772
a = int(input())
b = 1

for i in range(a):
    hex1 = str('{0:X}'.format(b))
    binar = str('{0:b}'.format(b))
    Octal = str('{0:o}'.format(b))
    print(" ".join((f'{b:>}', f'{Octal:>}', f'{hex1:>}', f'{binar:>}')))
    b += 1

如何对齐输出结果得到:

    1     1     1     1
    2     2     2    10
    3     3     3    11
    4     4     4   100
    5     5     5   101
    6     6     6   110
    7     7     7   111
    8    10     8  1000
    9    11     9  1001
   10    12     A  1010
   11    13     B  1011
   12    14     C  1100
   13    15     D  1101
   14    16     E  1110
   15    17     F  1111
   16    20    10 10000
   17    21    11 10001
      1       1       1       1
      2       2       2      10
      3       3       3      11
      4       4       4     100
      5       5       5     101
      6       6       6     110
      7       7       7     111
      8      10       8    1000
      9      11       9    1001
     10      12       A    1010
     11      13       B    1011
     12      14       C    1100
     13      15       D    1101
     14      16       E    1110
     15      17       F    1111
     16      20      10   10000
     17      21      11   10001
     18      22      12   10010
     19      23      13   10011
     20      24      14   10100
     21      25      15   10101
     22      26      16   10110
     23      27      17   10111
     24      30      18   11000
     25      31      19   11001
     26      32      1A   11010
     27      33      1B   11011
     28      34      1C   11100
     29      35      1D   11101
     30      36      1E   11110
     31      37      1F   11111
     32      40      20  100000
     33      41      21  100001
     34      42      22  100010
     35      43      23  100011
     36      44      24  100100
     37      45      25  100101
     38      46      26  100110
     39      47      27  100111
     40      50      28  101000
     41      51      29  101001
     42      52      2A  101010
     43      53      2B  101011
     44      54      2C  101100
     45      55      2D  101101
     46      56      2E  101110
     47      57      2F  101111
     48      60      30  110000
     49      61      31  110001
     50      62      32  110010
     51      63      33  110011
     52      64      34  110100
     53      65      35  110101
     54      66      36  110110
     55      67      37  110111
     56      70      38  111000
     57      71      39  111001
     58      72      3A  111010
     59      73      3B  111011
     60      74      3C  111100
     61      75      3D  111101
     62      76      3E  111110
     63      77      3F  111111
     64     100      40 1000000
     65     101      41 1000001
     66     102      42 1000010
     67     103      43 1000011
     68     104      44 1000100
     69     105      45 1000101
     70     106      46 1000110
     71     107      47 1000111
     72     110      48 1001000
     73     111      49 1001001
     74     112      4A 1001010
     75     113      4B 1001011
     76     114      4C 1001100
     77     115      4D 1001101
     78     116      4E 1001110
     79     117      4F 1001111
     80     120      50 1010000
     81     121      51 1010001
     82     122      52 1010010
     83     123      53 1010011
     84     124      54 1010100
     85     125      55 1010101
     86     126      56 1010110
     87     127      57 1010111
     88     130      58 1011000
     89     131      59 1011001
     90     132      5A 1011010
     91     133      5B 1011011
     92     134      5C 1011100
     93     135      5D 1011101
     94     136      5E 1011110
     95     137      5F 1011111
     96     140      60 1100000
     97     141      61 1100001
     98     142      62 1100010
     99     143      63 1100011
python python-3.x
  • 3 3 个回答
  • 71 Views

3 个回答

  • Voted
  1. Best Answer
    Stanislav Volodarskiy
    2022-08-15T04:20:12Z2022-08-15T04:20:12Z

    表是由代表不同数字格式的字符串创建的。考虑最大宽度。以计算宽度打印:

    a = int(input())
    table = []
    for b in range(1, a + 1):
        table.append((f'{b}', f'{b:o}', f'{b:X}', f'{b:b}'))
    
    width = max(len(word) for row in table for word in row)
    for row in table:
        print(*(f'{word:>{width}}' for word in row))
    
    • 3
  2. S. Nick
    2022-08-15T04:38:22Z2022-08-15T04:38:22Z

    作为一种选择

    a = input('Введите число: ')
    n1 = len(a)                                                               # +++
    n2 = len(str('{0:b}'.format(int(a))))                                     # +++
    b = 1
    
    for i in range(int(a)):
        hex1 = str('{0:X}'.format(b))
        binar = str('{0:b}'.format(b))
        Octal = str('{0:o}'.format(b))
    #    print(" ".join((f'{b:>}', f'{Octal:>}', f'{hex1:>}', f'{binar:>}')))
        print(f" {b:>{n1}} {Octal:>{n1}}{hex1:>{n1}} {binar:>{n2}}")
        b += 1
    

    在此处输入图像描述

    • 3
  3. CrazyElf
    2022-08-15T12:32:52Z2022-08-15T12:32:52Z

    主题的另一种变体。为了确定最大长度,只取最后一行的表示:

    a = int(input())
    
    def get_data(x):
        return f'{x}', f'{x:o}', f'{x:X}', f'{x:b}'
    
    def print_data(xx, nn):
        for x, n in zip(xx, nn):
            print(f'{x:>{n}}', end=' ')
        print()
    
    nn = list(map(len, get_data(a)))
    
    for i in range(1, a + 1):
        print_data(get_data(i), nn)
    

    输入输出:

    10
     1  1 1    1 
     2  2 2   10 
     3  3 3   11 
     4  4 4  100 
     5  5 5  101 
     6  6 6  110 
     7  7 7  111 
     8 10 8 1000 
     9 11 9 1001 
    10 12 A 1010 
    
    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5