zoukankan      html  css  js  c++  java
  • Python 使用for...in...和 while 循环 实现8种格式的 九九乘法表

    #九九乘法表 for...in ..
    
    #左下角
    for i in range(1,10):
        for j in range(1,i+1):
            print(' %d×%d=%2d'%(j,i,i*j), end = '')
        print()
    print('-------------------------------------------------------------------------')
    
    #右下角
    for i in range(1,10):
        for k in range(i,9):
            print(end='       ')
        for j in range(1,i+1):
            print("%2d×%d=%2d" %(j,i,i*j), end = '')
    
        print()
    print('-------------------------------------------------------------------------')
    
    #左上角
    for i in range(9,0,-1):
        for j in range(1,i+1):
            print("%2d×%d=%2d" %(j,i,i*j), end = '')
        print()
    print('-------------------------------------------------------------------------')
    
    #右上角
    for i in range(1,10):
        for k in range(1,i):
            print(end ='       ')
        for j in range(9,i-1,-1):
    
            print("%2d×%d=%2d" %(j,i,i*j), end = '')
        print()
    
    
    #九九乘法表 (while 循环)
    
    #左下角
    i = 1
    while i <= 9:
        j = 1
        while j <= i:
            print(' %d×%d=%2d'%(i,j,i*j),end = '')
            j += 1
        i += 1
        print()
    print('-------------------------------------------------------------------------')
    
    #右下角
    i = 1
    while i <=9:
        k = 9
        while k > i:
            print(end='       ')
            k -= 1
        j = 1
        while j <= i:
            print(' %d×%d=%2d'%(j,i,i*j),end = '')
            j += 1
        i += 1
        print()
    print('-------------------------------------------------------------------------')
    
    
    
    #左上角
    i = 9
    while i >= 1:
        j = 1
        while j <= i:
            print(' %d×%d=%2d'%(j,i,i*j),end = '')
            j +=1
        i -=1
        print()
    print('-------------------------------------------------------------------------')
    
    #右上角
    
    i = 9
    while i >=1:
        k = 9
        while k > i:
            print(end='       ')
            k -= 1
        j = 1
        while j <= i:
            print(' %d×%d=%2d'%(j,i,i*j),end = '')
            j += 1
        i -= 1
        print()
    print('-------------------------------------------------------------------------')
    

      

  • 相关阅读:
    .NET 动态脚本语言
    webParts与Web部件
    比较JqGrid与XtraGrid
    XtraGrid滚轮翻页
    Python------继承
    Python 私有化类的属性
    Python print 输出不换行,只有空格
    Python--函数参数类型
    手推FP-growth (频繁模式增长)算法------挖掘频繁项集
    Python 返回多个值+Lambda的使用
  • 原文地址:https://www.cnblogs.com/Fantinai/p/7806534.html
Copyright © 2011-2022 走看看