Python:每日一题 162
本帖最后由 冬雪雪冬 于 2018-3-14 20:36 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
我们知道1的阶乘是1,2的阶乘是2,3的阶乘是6,4的阶乘是24,将前4个数字的阶乘排在一起是12624,现在要求将1~50的阶乘排在一起打印出来,要求,每40个数字1行,当本行超过40个时换行到下一行,另外在每行的开头打印行号。最终的结果如下所示:
def ladder(number):
result = 1
for i in range(1,number+1):
result = result * i
return result
if __name__ == '__main__':
result = ''
for i in range(1,51):
result = result + str(ladder(i))
line_number = 1
cnt = 0
print('(%d)'%line_number,end = '')
for each in result:
cnt = cnt + 1
print(each,end = '')
if cnt % 40 == 0:
print('')
line_number = line_number + 1
print('(%d)'%line_number,end = '')
运行结果:
(1)1262412072050404032036288036288003991680
(2)0479001600622702080087178291200130767436
(3)8000209227898880003556874280960006402373
(4)7057280001216451004088320002432902008176
(5)6400005109094217170944000011240007277776
(6)0768000025852016738884976640000620448401
(7)7332394393600001551121004333098598400000
(8)0403291461126605635584000000108888694504
(9)1835216076800000030488834461171386050150
(10)4000000884176199373970195454361600000026
(11)5252859812191058636308480000000822283865
(12)4177922817725562880000000263130836933693
(13)5301672180121600000008683317618811886495
(14)5181944012800000002952327990396041408476
(15)1860964352000000010333147966386144929666
(16)6513375232000000003719933267899012174679
(17)9944815083520000000013763753091226345046
(18)3159795815809024000000005230226174666011
(19)1176000722410007429120000000020397882081
(20)1974433586402817399028973568000000008159
(21)1528324789773434561126959611589427200000
(22)0000334525266131638071081700620534407516
(23)6515200000000014050061177528798985431426
(24)0624451156993638400000000060415263063373
(25)8356373551320685139975072645120000000002
(26)6582715747884487680436258110146158903196
(27)3852800000000011962222086548019456196316
(28)1495657715064383733760000000000550262215
(29)9812088949850305428800254892961651752960
(30)0000000002586232415111681806429643551536
(31)1197996919763238912000000000012413915592
(32)5360726708622890473733750385214863546777
(33)6000000000060828186403426756087225216332
(34)1295376887552831379210240000000000304140
(35)9320171337804361260816606476884437764156
(36)8960512000000000000
***Repl Closed***
def factorial(n):
if n ==1:
return 1
else:
return factorial(n-1)*n
list1=[]
for i in range(1,51):
list1.append(str(factorial(i)))
W=''.join(list1)
m = len(W)//40
for i in range(m+1):
if i*40+40<len(W):
print('(%d)'% (i+1), W)
else:
print('(%d)'% (i+1), W) 本帖最后由 fkmol 于 2018-3-11 18:53 编辑
i=1
q=0
print ('(1)',end='\t')
for n in range(50):
g = i * (n+1)
i = g
print (i,end="")#单纯的打印数字
h = len(str(i))#不知道怎么得出40
if (h%10==0):#想做出40个一换行但并没有用
q += 1
print ()
print ('(%d)' % q,end='\t')
弃疗 感觉思维完全跟不上
def sisi():
i=1
an=''#定义一个空的字符串
for n in range(50):
g = i * (n+1)
i = g
an = an+str(i)#利用迭代赋值
return an#返还的值是字符串
ms = sisi()
for shi in range((len(ms)//40+1)):#整除加一可得出循环多少遍
print ('(%2d)'%(shi+1),end='\t')
print (ms)#打印40的倍数每次打印40
仔细思考了一下 发现会了{:5_95:} 本帖最后由 凌九霄 于 2018-3-11 17:01 编辑
import math
resultadd = ''
id = 1
for i in range(1, 51):
result = math.factorial(i)
resultadd += str(result)
while len(resultadd) > 40:
print("({0:>2d}) {1}".format(id, resultadd))
id += 1
resultadd = resultadd
print("({0:>2d}) {1}".format(id, resultadd))
t = lambda x: x*t(x-1) if x > 1 else x
m = ''.join()
for i in range(len(m)//40+1):
k = '('+' '*(2-len(str(i+1)))+str(i+1) +') '+m
print(k)
勉强一行?
from functools import reduce
print(''.join() +'\n' for i in range(len(''.join())//40+1)]]))
或者二行的
t = lambda x:x*t(x-1) if x > 1 else x
print(''.join() +'\n' for i in range(len(''.join())//40+1)]])) 本帖最后由 塔利班 于 2018-3-11 17:05 编辑
text=''
fac=1
for i in range(1,51):
fac*=i
text+=str(fac)
a=0
for each in text:
if a%40==0:
print('\n(%2d)'%((a//40)+1),end='')
a+=1
print(each,end='') def fact(num):
product = 1
for i in range(1, num + 1):
product *= i
return product
def get_num(num):
number = []
while num:
number.append(str(num % 10))
num /= 10
number.reverse()
return number
all_number = []
for i in range(1, 51):
all_number += get_num(fact(i))
all_number.reverse()
indexNum = 1
line_number = ''
count = 0
for j in range(len(all_number)):
line_number += all_number.pop()
count += 1
if count == 40:
print '(', '%2d' % indexNum, ')',' ', line_number
line_number = ''
indexNum += 1
count = 0
print '(', '%2d' % indexNum, ')', ' ', line_number def fact(n):
if n == 1:
return 1
else:
return fact(n-1) * n
def myPrint(num):
#用str1存储1~num的阶乘结果
str1 = ''
for i in range(1,num+1):
str1 += str(fact(i))
#计算打印结果的行数
if len(str1)%40 == 0:
line_count = len(str1)//40
else:
line_count = len(str1)//40 + 1
#分行打印
for i in range(0,line_count):
if i < 9:
print('( ' + str(i+1) + ')',end = ' ')
else:
print('(' + str(i+1) + ')',end = ' ')
#用str2存储阶乘结果的每一行
str2 = ''
str2 += str1
print(str2)
#打印1~50的阶乘结果
myPrint(50) 本帖最后由 8306最硬 于 2018-3-11 17:54 编辑
from math import factorial
strng = ''.join( str(factorial(i)) for i in range(1, 51) )
for i in range(0, len(strng), 40):
print( '({:>2}) {}'.format(i//40+1, strng) ) def f(n):
if n==1:
return 1
else:
return f(n-1)*n
r=''
for i in range(1, 51):
r=r+str(f(i))
for i in range((len(r)+40-1)//40):
print('('+str(i+1)+')', r)
结果:
(1) 1262412072050404032036288036288003991680
(2) 0479001600622702080087178291200130767436
(3) 8000209227898880003556874280960006402373
(4) 7057280001216451004088320002432902008176
(5) 6400005109094217170944000011240007277776
(6) 0768000025852016738884976640000620448401
(7) 7332394393600001551121004333098598400000
(8) 0403291461126605635584000000108888694504
(9) 1835216076800000030488834461171386050150
(10) 4000000884176199373970195454361600000026
(11) 5252859812191058636308480000000822283865
(12) 4177922817725562880000000263130836933693
(13) 5301672180121600000008683317618811886495
(14) 5181944012800000002952327990396041408476
(15) 1860964352000000010333147966386144929666
(16) 6513375232000000003719933267899012174679
(17) 9944815083520000000013763753091226345046
(18) 3159795815809024000000005230226174666011
(19) 1176000722410007429120000000020397882081
(20) 1974433586402817399028973568000000008159
(21) 1528324789773434561126959611589427200000
(22) 0000334525266131638071081700620534407516
(23) 6515200000000014050061177528798985431426
(24) 0624451156993638400000000060415263063373
(25) 8356373551320685139975072645120000000002
(26) 6582715747884487680436258110146158903196
(27) 3852800000000011962222086548019456196316
(28) 1495657715064383733760000000000550262215
(29) 9812088949850305428800254892961651752960
(30) 0000000002586232415111681806429643551536
(31) 1197996919763238912000000000012413915592
(32) 5360726708622890473733750385214863546777
(33) 6000000000060828186403426756087225216332
(34) 1295376887552831379210240000000000304140
(35) 9320171337804361260816606476884437764156
(36) 8960512000000000000 追求格式的输出。。。
def fac(n):
if n == 0:
return 1
else:
return n*fac(n-1)
def generate_list_fac(n):
string = ''
for i in range(1,n+1):
string += str(fac(i))
return string
def print_list_fac(n,k):
string_fac = generate_list_fac(n)
hangshu_1 = len(string_fac) // k
flag = len(string_fac) % k
for i in range(1,hangshu_1+1):
if i//10 == 0:
string_line = string_fac[(i-1)*k:i*k]
print("(%d)"%i,end=' ')
print(string_line)
else:
string_line = string_fac[(i-1)*k:i*k]
print("(%d)"%i,end='')
print(string_line)
if flag != 0:
print("(%d)"%(hangshu_1+1),end='')
print(string_fac[-flag:])
>>> print_list_fac(50,40)
(1) 1262412072050404032036288036288003991680
(2) 0479001600622702080087178291200130767436
(3) 8000209227898880003556874280960006402373
(4) 7057280001216451004088320002432902008176
(5) 6400005109094217170944000011240007277776
(6) 0768000025852016738884976640000620448401
(7) 7332394393600001551121004333098598400000
(8) 0403291461126605635584000000108888694504
(9) 1835216076800000030488834461171386050150
(10)4000000884176199373970195454361600000026
(11)5252859812191058636308480000000822283865
(12)4177922817725562880000000263130836933693
(13)5301672180121600000008683317618811886495
(14)5181944012800000002952327990396041408476
(15)1860964352000000010333147966386144929666
(16)6513375232000000003719933267899012174679
(17)9944815083520000000013763753091226345046
(18)3159795815809024000000005230226174666011
(19)1176000722410007429120000000020397882081
(20)1974433586402817399028973568000000008159
(21)1528324789773434561126959611589427200000
(22)0000334525266131638071081700620534407516
(23)6515200000000014050061177528798985431426
(24)0624451156993638400000000060415263063373
(25)8356373551320685139975072645120000000002
(26)6582715747884487680436258110146158903196
(27)3852800000000011962222086548019456196316
(28)1495657715064383733760000000000550262215
(29)9812088949850305428800254892961651752960
(30)0000000002586232415111681806429643551536
(31)1197996919763238912000000000012413915592
(32)5360726708622890473733750385214863546777
(33)6000000000060828186403426756087225216332
(34)1295376887552831379210240000000000304140
(35)9320171337804361260816606476884437764156
(36)8960512000000000000 本帖最后由 shigure_takimi 于 2018-3-11 21:23 编辑
def fun(n = 50, count = 40):
s = ''
result = 1
for i in range(1, n+1):
result *= i
s += str(result)
length = len(s)
totalCol = length//count
totalCol = totalCol if length%count == 0 else totalCol+1
for i in range(totalCol):
num = ' '+str(i+1) if i < 9 else str(i+1)
a = '('+num+')'+s
print(a)
fun() import math
string = ''
for i in range(1, 51):
string += str(math.factorial(i))
index = 0
line = 1
length = len(string)
while index < length:
print('(' + '{0:2d}'.format(line) + ') ' + string)
index += 40
line += 1
import textwrap
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
result = ''
for i in range(1, 51):
result += str(factorial(i))
resultList = textwrap.wrap(result, 40)
for i in range(len(resultList)):
print('({:2}) {}'.format(i + 1, resultList)) 本帖最后由 victor.xu 于 2018-3-11 20:28 编辑
之前学格式化字符串时觉得特别难,现在能完成这样的作业,也别有成就感。
import math
lis = []
for i in range(1, 51):
lis.append(math.factorial(i))
tt = tuple(lis)
st = "%s" * 50 % tt
i, j = 0, 0
lis1 = []
while j < len(st):
if j - i == 39:
lis1.append(st)
i, j = j + 1, j + 1
else:
j += 1
lis1.append(st)
for i in range(len(lis1)):
print('(%2d)' % (i + 1), lis1)
结果如下:
( 1) 1262412072050404032036288036288003991680
( 2) 0479001600622702080087178291200130767436
( 3) 8000209227898880003556874280960006402373
( 4) 7057280001216451004088320002432902008176
( 5) 6400005109094217170944000011240007277776
( 6) 0768000025852016738884976640000620448401
( 7) 7332394393600001551121004333098598400000
( 8) 0403291461126605635584000000108888694504
( 9) 1835216076800000030488834461171386050150
(10) 4000000884176199373970195454361600000026
(11) 5252859812191058636308480000000822283865
(12) 4177922817725562880000000263130836933693
(13) 5301672180121600000008683317618811886495
(14) 5181944012800000002952327990396041408476
(15) 1860964352000000010333147966386144929666
(16) 6513375232000000003719933267899012174679
(17) 9944815083520000000013763753091226345046
(18) 3159795815809024000000005230226174666011
(19) 1176000722410007429120000000020397882081
(20) 1974433586402817399028973568000000008159
(21) 1528324789773434561126959611589427200000
(22) 0000334525266131638071081700620534407516
(23) 6515200000000014050061177528798985431426
(24) 0624451156993638400000000060415263063373
(25) 8356373551320685139975072645120000000002
(26) 6582715747884487680436258110146158903196
(27) 3852800000000011962222086548019456196316
(28) 1495657715064383733760000000000550262215
(29) 9812088949850305428800254892961651752960
(30) 0000000002586232415111681806429643551536
(31) 1197996919763238912000000000012413915592
(32) 5360726708622890473733750385214863546777
(33) 6000000000060828186403426756087225216332
(34) 1295376887552831379210240000000000304140
(35) 9320171337804361260816606476884437764156
(36) 8960512000000000000 import itertools as it
import math
def fun(start=1, stop=50, width=40):
string = "".join(str(math.factorial(i)) for i in range(start, stop+1))
rows = math.ceil(len(string)/width)
num_width = len(str(rows))
result = ''
for i in range(rows):
result = it.chain(result, '({}) '.format(str(i+1).rjust(num_width)),
it.islice(string, i*width, (i+1)*width), '\n')
return "".join(result)[:-1] import re
list1=[]
jiechen=1
test=''
for i in range (1,51):
jiechen=jiechen*i
list1.append(jiechen)
for i in list1:
test=test+str(i)
if len(test)%40==0:
for i in range (1,(len(test)//40)+1):
print(str(i)+" "+test[(i-1)*40:40*i])
else:
for i in range (1,(len(test)//40+2)):
print(str(i)+" "+test[(i-1)*40:40*i])
def jc(n):
answer = 1
for i in range(n):
answer = answer * (i + 1)
return answer
def main(n = 50):
str1 = ''
for i in range(50):
str1 += str(jc(i + 1))
for i in range(len(str1)//40 + 1):
str2 = str1
print ("(%2d)"%(i+1),str2)
main()