Python:每日一题 7
本帖最后由 新手·ing 于 2017-3-26 19:52 编辑题目:输出 9*9 乘法口诀表。
程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
欢迎小伙伴们,一起答题!
如果你有能力,欢迎加入我们!
已经上车老司机:@ooxx7788
{:10_298:}点我上车 {:10_298:} @冬雪雪冬 @ooxx7788 @jerryxjr1220
来新题目啦 #输出 9*9 乘法口诀表
for i in range(1,10):
for j in range(1,i + 1):
print('%s * %s = %s' % (i,j,i * j))
我的解答,等待大佬们的可怕回答 新手·ing 发表于 2017-3-26 19:51
我的解答,等待大佬们的可怕回答
你这个打印不标准啊
99乘法表的标准输出是这样的:
1 x 1 =1
1 x 2 =2 2 x 2 =4
1 x 3 =3 2 x 3 =6 3 x 3 =9
1 x 4 =4 2 x 4 =8 3 x 4 = 12 4 x 4 = 16
1 x 5 =5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 =6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
1 x 7 =7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49
1 x 8 =8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64
1 x 9 =9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
***Repl Closed***
total = ''
for i in range(1, 10):
for j in range(1, i + 1):
total += '%d x %d = %2d ' % (j, i, i * j) if i != j else '%d x %d = %2d \n' % (j, i, i * j)
print total
如果全部用空格分隔的话,一行代码:
print ' '.join(['%d x %d = %2d' % (j, i, i * j) for i in range(1, 10) for j in range(1, i + 1)]) 本帖最后由 ooxx7788 于 2017-3-26 20:37 编辑
新手·ing 发表于 2017-3-26 19:51
我的解答,等待大佬们的可怕回答
你赖皮哦,人家要求的是9行9列,你弄个一顺。
for j in range(1, 10):
for i in range(1, 10):
print('%d * %d = %2d' % (i, j, i*j), end=' ')
print()
没有j大佬那么花哨。
1*1= 1 2*1= 2 3*1= 3 4*1= 4 5*1= 5 6*1= 6 7*1= 7 8*1= 8 9*1= 9
1*2= 2 2*2= 4 3*2= 6 4*2= 8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18
1*3= 3 2*3= 6 3*3= 9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27
1*4= 4 2*4= 8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 jerryxjr1220 发表于 2017-3-26 20:26
你这个打印不标准啊
99乘法表的标准输出是这样的:
1 x 1 =1
大佬佬 for j in range(1, 10):
for i in range(1, 10):
if i<=j:
print('%d * %d = %2d' % (i, j, i*j), end=' ')
print()
优雅的我不会 for i in range(1,10):
for j in range(1,i+1):
print('%d*%d=%d'%(j,i,j*i),end='\t')
print('\n') m = ''
for i in range(1, 10):
c = 1
for j in range(1, i+1):
if i==j :
m += '%d x %d = %2d \n' % (j, i, i * j)
c = 0
if c!=0 :
m += '%d x %d = %2d ' % (j, i, i * j)
print(m) 后面加一句就换行了啊
for i in range(1,10):
for j in range(1,i+1):
print("%s * %s = %s" % (j,i,i*j), end='')
print() # 换行
一行也是可以的,可读性没那么好就是了
# 写成一句可以是这样,推导式包含推导式就可以把换行也带上了
print('\n'.join([''.join(["%s * %s = %s" % (j,i,i*j)for j in range(1,i+1)]) for i in range(1,10)]))
效果
1 * 1 = 1
1 * 2 = 22 * 2 = 4
1 * 3 = 32 * 3 = 63 * 3 = 9
1 * 4 = 42 * 4 = 83 * 4 = 124 * 4 = 16
1 * 5 = 52 * 5 = 103 * 5 = 154 * 5 = 205 * 5 = 25
1 * 6 = 62 * 6 = 123 * 6 = 184 * 6 = 245 * 6 = 306 * 6 = 36
1 * 7 = 72 * 7 = 143 * 7 = 214 * 7 = 285 * 7 = 356 * 7 = 427 * 7 = 49
1 * 8 = 82 * 8 = 163 * 8 = 244 * 8 = 325 * 8 = 406 * 8 = 487 * 8 = 568 * 8 = 64
1 * 9 = 92 * 9 = 183 * 9 = 274 * 9 = 365 * 9 = 456 * 9 = 547 * 9 = 638 * 9 = 729 * 9 = 81 for i in range(1,10):
for j in range(1,i+1):
print('{0}*{1}={2}\t'.format(j,i,i*j),end = '')
print()
与楼上大佬雷同 for i in range (9):
for j in range (i+1):
temp = (i+1)*(j+1)
print (j+1,'*',i+1,'=',temp,end=' ')
print ()
i = 1
j = 1
while i<10:
while j<10:
if j < i:
print(' ',end = '')
else:
if i*j <10:
print('%d*%d= %d'%(i,j,i*j),end = '')
else :
print('%d*%d= %d '%(i,j,i*j),end = '')
j +=1
i += 1
print()
j = 1
1*1= 11*2= 21*3= 31*4= 41*5= 51*6= 61*7= 71*8= 81*9= 9
2*2= 42*3= 62*4= 82*5= 10 2*6= 12 2*7= 14 2*8= 16 2*9= 18
3*3= 93*4= 12 3*5= 15 3*6= 18 3*7= 21 3*8= 24 3*9= 27
4*4= 16 4*5= 20 4*6= 24 4*7= 28 4*8= 32 4*9= 36
5*5= 25 5*6= 30 5*7= 35 5*8= 40 5*9= 45
6*6= 36 6*7= 42 6*8= 48 6*9= 54
7*7= 49 7*8= 56 7*9= 63
8*8= 64 8*9= 72
9*9= 81 class Question7:
def T99(self):
for i in range(1,10):
print('\n')
for j in range(1,i+1):
print('%s * %s = %s'%(j,i,(i*j)),end ='')
Q7 = Question7()
Q7.T99() for i in range(1,10):
for j in range(1,10):
print('%s*%s=%s'%(i,j,i*j),end = ' ')
print() for x in range(1,10):
for y in range(1,x+1):
print('%d * %d = %2d'%(y, x, x*y),end = '')
print() for i in range(1,10):
for j in range(1,i+1):
print('%dx%d=%d' % (j,i, i*j), end=' ')
print()
for i in range(1,10):
for j in range(1,i + 1):
print('%d × %d = %d' % (i,j,i * j),end = '')
print('\n')
{:10_243:} print("------------输出9*9乘法表------------")
for i in range(1,10):
for j in range(1,i+1):
print("%d*%d=%d" %(j,i,i*j), end=" ")
print("\n")
for i in range(1, 10):
for j in range(i):
print "%dx%d=%2d " % (j + 1, i, (j + 1) * i),