百练1019:Number Sequence

举报
用户已注销 发表于 2021/11/19 03:50:58 2021/11/19
【摘要】 题目:   描述 A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...Sk. Each group...

题目:

 

描述

A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...Sk. Each group Sk consists of a sequence of positive integer numbers ranging from 1 to k, written one after another.
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910

输入

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by one line for each test case. The line for a test case contains the single integer i (1 ≤ i ≤ 2147483647)

输出

There should be one output line per test case containing the digit located in the position i.

样例输入


  
  1. 2
  2. 8
  3. 3

样例输出


  
  1. 2
  2. 2

 

 

代码:

 


  
  1. n=int(input())
  2. def sum(i):#从1到i一共多少个数字
  3. if(i<10):
  4. return i
  5. if(i<100):
  6. return i*2-9
  7. if(i<1000):
  8. return (i-99)*3+sum(99)
  9. if(i<10000):
  10. return (i-999)*4+sum(999)
  11. return (i-9999)*5+sum(9999)
  12. while n:
  13. n=n-1
  14. k=int(input())
  15. i=1
  16. while sum(i)<k:
  17. i=i+1
  18. k=k-sum(i-1)
  19. i=1
  20. while sum(i)<k:
  21. i=i+1
  22. k=k-sum(i-1)
  23. #i的第k
  24. print(str(i)[k-1])

 

 

这个代码十分有意思,第-10行到第-7行 和 第-6行到第-3行 只有缩进不一样

文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/75201323

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。