By Python6 ways: given an integer of more than 5, it is determined by several

Method One: compare

a=int(input(">>>>"))
if a<10:            
    print(1)
elif a<100:   #第一个条件已经过滤了大于9,所以这里区间是11到100
    print(2)
elif a<1000:
    print(3)
elif a<10000:
    print(4)
else:
    print(5)

Method Two: Use divisible achieve, except after if was not a 0 or 0, this method is introduced to calculate the efficiency will be reduced, it is possible to add, do not subtract, multiply it can not in addition to, not counting can not be calculated

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
i = int(intput('>>>')
if i // 10000:
    print(5):
elif i // 1000:
    print(4)
elif i // 100:
    print(3)
elif i // 10:
    print(2)
else:
    print(1)

Analysis: The assumption is that in five cases, regardless of other conditions

In [1]: 6666 // 10000
Out[1]: 0                除以10000为零证明是小于5位数

In [2]: 6666 // 1000
Out[2]: 6                但是如果能被1000整除,它就是一个4位数

In [3]: 6666 // 100
Out[3]: 66

In [4]: 6666 // 10
Out[4]: 666

In [5]: 6666 // 1
Out[5]: 6666

Method three:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
a=int(input(">>>"))
if a<0:
    print("Format is wrong")
elif a<100000:    ##限定5位
    if a<10:
        print(1)
    elif a<100:
        print(2)
    elif a<1000:
        print(3)
    elif a<10000:
        print(4)
    else:  
        print(5)
else:
    print("请输入一个不超过5位的数")

Method four: string processing implemented

#!/usr/bin/python3
nnumber=input(">>>>")
length=len(nnumber)
if length>4:
    print(5)
elif length>3:
    print(4)
elif length>2:
    print(3)
elif length>1:
    print(2)
else:
    print(1)

Method five: binary realization

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#!/usr/bin/python3
number = int(input("number >> "))
if number >= 100:       ##直接从100开始折
    if number >= 10000:
        print("5")
    elif number >= 1000:
        print("4")
    else:
        print("3")
else:
    if number >= 10:
        print("2")
    else:
        print("1")

Method six: math realized, this method is slower than the division, if the cycle 100 million times quite clear

number=int(input("输入一个不超过5位的正整数: ")
if a<=0 or a>=100000:
    print('请输入一个不超过5位的正整数')
else:
    import math
    b=int(math.log10(a)+1)
    print(b)

Guess you like

Origin blog.51cto.com/14246112/2459108