有一个数列为1,12,123,1234,12345,123456,1234567,12345678,..12345678910111213,... 计算第1个到第N个有多少个数能被3整除的。

 

有一个数列为1,12,123,1234,12345,123456,1234567,12345678,..12345678910111213,...

计算第1个到第N个有多少个数能被3整除的。

 

 

代码如下:Python

def demo(n):

   demo_list=[]

   demo_value=""

   count=0

   while n>0:

       for i in range(1,1+n):

           demo_value=demo_value+str(i)

           n-=1

           demo_list.append(demo_value)

   for j in demo_list:

       if(int(j)%3==0):

           count=count+1    

   print (demo_list)

print (count)

 demo(29)

下面实测运行结果(ubuntu16.04,python2.7):

['1', '12', '123', '1234', '12345','123456', '1234567', '12345678', '123456789', '12345678910', '1234567891011','123456789101112', '12345678910111213', '1234567891011121314','123456789101112131415', '12345678910111213141516','1234567891011121314151617', '123456789101112131415161718','12345678910111213141516171819', '1234567891011121314151617181920','123456789101112131415161718192021', '12345678910111213141516171819202122','1234567891011121314151617181920212223','123456789101112131415161718192021222324','12345678910111213141516171819202122232425','1234567891011121314151617181920212223242526','123456789101112131415161718192021222324252627', '12345678910111213141516171819202122232425262728','1234567891011121314151617181920212223242526272829']

19

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭