美文网首页
【Python】输入一个整数N,求1到N个整数的十进制表示中某个

【Python】输入一个整数N,求1到N个整数的十进制表示中某个

作者: 玉米fight | 来源:发表于2020-04-17 10:40 被阅读0次

题目:输入一个整数N,求1到N个整数的十进制表示中某个字符出现的次数
输入:12 1
输出:5 (1,10,11,12→5个1)
思路:1、整数N循环输出字符串A【12345...12】
   2、字符串A循环+判断输入的字符,计算总数(1、循环计数 2、使用count()方法)

解答:

import sys

b=sys.stdin.readline().strip().split()
num2=b[1]
num1=int(b[0])
str1=""
coun=0

for i in range(1,num1+1):
     str1=str1+str(i)
#第一种
for j in range(len(str1)):
     #print (str1[j])
     if str1[j]==num2:
        coun+=1
print(coun)
#第二种
print(str1.count(num2))

相关文章

网友评论

      本文标题:【Python】输入一个整数N,求1到N个整数的十进制表示中某个

      本文链接:https://www.haomeiwen.com/subject/xccevhtx.html