博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python第一周 学习笔记(2)
阅读量:5766 次
发布时间:2019-06-18

本文共 7618 字,大约阅读时间需要 25 分钟。

习题解析


0.打印10以内偶数:位运算

for i in range(10):    if not i & 0x01:        print(i)

1.给定一个不超过5位的正整数,判断其有几位(使用input函数)

方法一:正常逻辑处理

a = int(input("Please enter a numer: "))if a < 0:    print('Error')if a < 10:    print(1)elif a < 100:    print(2)elif a < 1000:    print(3)elif a < 10000:    print(4)elif a < 100000:    print(5)else:    print('Error')

方法二:折半

a=int(input("Please enter a numer: "))if a < 0:    print('Error')elif a < 1000:    if 9 < a < 100:        print(2)    elif a < 10:        print(1)    else:        print(3)elif a < 10000:    print(4)elif a < 100000:    print(5)else:    print('Error')

方法三:字符串处理

a = str(int(input("Please enter a number: ")))print(len(a))

2.给定一个不超过5位的正整数,判断该数的位数,依次打印出个位、十位、百位、千位、万位的数字

方法一:

a = int(input("Please enter a number: "))length = 0if a < 0:    print('Error')elif a < 1000:    if 9 < a < 100:        length = 2    elif a < 10:        length = 1    else:        length = 3elif a < 10000:    length = 4elif a < 100000:    length = 5else:    print('Error')print(length)for i in range(length):    print(a % 10)    a = a // 10

方法二:

a = input("Please enter a number: ")b = int(a)print(len(a))for i in range(len(a)):    print(b % 10)    b = b // 10

方法三:

a = int(input("Please enter a number: "))length = 0if a < 0:    print('Error')elif a < 1000:    if 9 < a < 100:        length = 2    elif a < 10:        length = 1    else:        length = 3elif a < 10000:    length = 4elif a < 100000:    length = 5else:    print('Error')print(length)for i in range(length):    print(a - (a // 10) * 10)    a = a // 10

方法四:

a=int(input("Please enter a number: "))a *= 10while (a // 10) != 0:    a //= 10    print(a % 10)

3.给定一个不超过5位的正整数,判断该数的位数,依次打印出万位、千位、百位、十位、个位的数字

方法一:

a=input('Please enter a number: ')b=int(a)for i in range(len(a),0,-1):    te = 10 ** (i - 1)    c = b // te    print(c)    b -= (c * te)print(len(a))

方法二:

a = int(input("Please enter a number: "))length = 0if a < 0:    print('Error')elif a < 1000:    if 9 < a < 100:        length = 2    elif a < 10:        length = 1    else:        length = 3elif a < 10000:    length = 4elif a < 100000:    length = 5else:    print('Error')print(length)pre = 0for i in range(length,0,-1):    cur = a // (10 ** (i - 1))    print(cur - pre * 10)    pre = cur

方法三:

a = int(input("Please enter a number: "))length = 0if a < 0:    print('Error')elif a < 1000:    if 9 < a < 100:        length = 10    elif a < 10:        length = 1    else:        length = 100elif a < 10000:    length = 1000elif a < 100000:    length = 10000else:    print('Error')while length:    print(a // length)    a %= length    length //= 10

方法四:

c = int(input("Please enter a number: "))w = 10000length = 5flag = Falsewhile w:    t = c // w    if flag:          print(t)    else:             if t:            print(t)            flag = True        else:            length -= 1    c = c % w     w //= 10print("The length of number is", length)

4.打印一个边长为n的正方形

方法一:

a = int(input("Please enter a number: "))for i in range(a):    if i == 0 or i == a-1:        print('*' * a )    else:        print('*' + ' ' * (a - 2) + '*')

方法二:

a = int(input("Please enter a number: "))print('*' * a)for i in range(a - 2):    print('*' + ' ' * (a - 2) + '*')else:    print('*' * a)

方法三:

a = int(input("Please enter a number: "))top = mid = '*'for i in range(a - 1):    top += '\t*'    mid += '\t'else:    mid += '*'    print(top)for i in range(a - 2):    print('\n')    print(mid)else:    print('\n')    print(top)

5.求100内所有奇数的和(2500)

方法一:

sum = 0for i in range(1,100,2):    sum += iprint(sum)

6.判断学生成绩,成绩等级A~E。其中,90分以上为'A',80~89分为'B',70~79分为'C',60~69分为'D',60分以下为'E'

方法一:

a = int(input('Please enter a score: '))if a > 79:    if a >= 90:        print('A')    else:        print('B')elif a >= 70:    print('C')elif 60 <= a < 69:    print('D')else:    print('E')

7.求1到5阶乘之和

方法一:

sum = 0for i in range(1,6):    mul = 1    for j in range(1,i + 1):        mul *= j    sum += mulprint(sum)

方法二:效率高

sum = 0mul = 1for i in range(1,6):    mul *= i    sum += mulprint(sum)

8.给一个数,判断它是否是素数(质数)

a=int(input("Please enter a number: "))for i in range(2,int(a ** 0.5) + 1):    if a % i != 0:        continue    elif a % i == 0:        print(("N: %d") % (i))        break        else:    print("Yes,prime")

9.打印九九乘法表

for i in range(1,10):    line = ''    for j in range(1,i + 1):        line = line + ('%d*%d=%2d ') % (j,i,i*j)    print(line)

10.打印菱形

方法一:

a = int(input(">>>"))b = (a - 1) / 2for i in range(a):    if i <= b:        print(' ' * ((a - (2 * i + 1)) // 2) +'*' * (i * 2 + 1) + ' ' * (( a - (2 * i + 1)) // 2 ))    else:        m=int(2 * b - i)        print(' '*((a - (2 * m + 1)) // 2) +'*' * (m * 2 + 1) +' ' * ((a - (2 * m + 1)) // 2))

方法二:

a = int(input(">>>"))b=(a - 1) // 2for i in range(-b,b + 1):    c = abs(i)    print(' ' * c + '*' * (a - 2 * c) + ' ' * c)

11.打印100以内的斐波那契数列

a = 1b = 1while True:    ne = a + b    if ne >= 100:        break    a = b    b = ne    print(ne)

12.求斐波那契数列第101项 573147844013817084101

a = 1b = 1cnt = 2while True:    ne = a + b    a = b    b = ne    cnt += 1    if cnt == 101:        print(b)        break

13.求10万内的所有素数 9592

方法一:

import datetimestart=datetime.datetime.now()cnt=1for i in range(3,100000,2):#优化点1:排除偶数    for j in range(3,int(i ** 0.5) + 1,2):#优化点2:排除含有因子2的数并只循环到原数的开方加1        if i % j == 0:            break    else:        #print(i)        cnt += 1end=(datetime.datetime.now() - start).total_seconds() print(end)print(cnt)

方法二:

a = int(input('>>>'))import datetimestart=datetime.datetime.now()cnt = 2for i in range(4,a):    if i % 6 != 1 and i % 6 != 5:#质数必与6的倍数相邻        continue    else:        for j in range(5,int(i ** 0.5) + 1,2):            if i % j == 0:                break        else:            cnt += 1end=(datetime.datetime.now()-start).total_seconds() print(end)            print(cnt)

方法三:

x = 5step = 2count = 2while x < 100000:    for i in range(3,int(x ** 0.5) + 1,2):        if not x % i:            break    else:        count += 1    x += step    step = 4 if step == 2 else 2  print(count)

方法四:

prime=[2]for i in range(3,100000,2):    flag=False    up=int(i**0.5)+1    for j in prime:        if i%j==0:            flag=False            break        if j>=up:            flag=True            break        if flag:        prime.append(i)print(len(prime))

方法五:

筛选法待补充

14.输入若干整数,打印最大值:

temp = 0 while True:    a = input('>>>')     if not a:        temp = a        break     else:         b = int(a)     if b >= temp:        temp = bprint(temp)

15.输入n个数,求每次输入后的算数平均数

sum = 0cnt = 0while True:    a = input('>>>')    if not a:        break     else:        sum += float(a)        cnt += 1        print(sum / cnt)

16.反向乘法表:

方法一:

for i in range(1,10):    line = ''    for j in range(i,10):            line += '%d*%d=%2d ' % (i,j,j * i)    print(' ' * (7 * i - 6) + line)

方法二:

for i in range(1,10):    line = ''    for j in range(i,10):            line += '%d*%d=%2d ' % (i,j,j * i)    print('{:>63}'.format(line))

方法三:

for i in range(1,10):    line = ''    for j in range(i,10):            line += '{}*{}={:<{}} '.format(i,j,j * i,2 if j<4 else 3)    print('{:>70}'.format(line))

17.打印对顶三角形:

a = int(input(">>>"))b = (a - 1) // 2for i in range(-b,b + 1):     c = int((a - (2 * abs(i) + 1)) / 2)    print(' ' * c + '*' * (2 * abs(i) + 1) + '  ' * c)

18.打印闪电:

num = int(input(">>>"))if num % 2 == 0:    num += 1half = (num - 1) // 2for i in range(- half,half + 1):    if i < 0:        print(' ' * (-i) + '*' * (half + i + 1))    elif i == 0:        print('*' * num)    else:        print(' ' * half + '*' * (half - i + 1))

19.猴子吃桃:

x = 1for i in range(9):    x = (x + 1) * 2print(x)

转载于:https://blog.51cto.com/11281400/2090633

你可能感兴趣的文章
php5编译安装常见错误和解决办法集锦
查看>>
Linux远程访问及控制
查看>>
MongoDB实战系列之五:mongodb的分片配置
查看>>
Unable to determine local host from URL REPOSITORY_URL=http://
查看>>
ORACLE配置,修改tnsnames.ora文件实例
查看>>
Workstation服务无法启动导致无法访问文件服务器
查看>>
ant中文教程
查看>>
Linux常用命令(一)
查看>>
【VMCloud云平台】SCAP(四)租户(一)
查看>>
基于 Android NDK 的学习之旅----- C调用Java
查看>>
Windows 10 技术预览
查看>>
Tomcat http跳转https
查看>>
一个自动布署.net网站的bat批处理实例
查看>>
我的友情链接
查看>>
Centos6.6安装选包及基础场景说明
查看>>
java基础面试题-1
查看>>
深克隆与序列化效率的比较
查看>>
lamp+nginx代理+discuz+wordpress+phpmyadmin搭建一
查看>>
nagios监控使用139邮箱报警
查看>>
Windows Phone 7 中各种Task解说(启动器与选择器)
查看>>