SDUT:Python实验二——程序流程控制编程题(参考答案)

7-1 sdut-sel-10 直角坐标系象限判断

输入:2 3
           -2 -3

输出:1
           3

while 1:
    try:
        a,b=map(int, input().split())
        if a>0 and b>0:
            print(1)
        elif a<0 and b>0 :
            print(2)
        elif a<0 and b<0 :
            print(3)
        elif a>0 and b<0:
            print(4)
    except:
        break

7-2 sdut-sel-2 汽车超速罚款(选择结构)

输入:40 39

输出:Congratulations, you are within the speed limit!

n,m=map(int,input().split())
d,f=0,0
if m<n :
    print("Congratulations, you are within the speed limit!")
elif m>n :
    d=m-n
    if 1<=d<=20 :
        f=100
    elif 21<=d<=30 :
        f=270
    elif d>=31 :
        f=500
    print("You are speeding and your fine is %d."%(f))

7-3 sdut-运输计费问题

输入:1.9  200.3

输出:381

n,m=map(float,input().split())
sum=0
if m<250 :
    sum=(n*m)
elif 250<=m<500 :
    sum=(n*m*0.98)
elif 500<=m<1000 :
    sum=(n*m*0.95)
elif 1000<=m<2000 :
    sum=(n*m*0.92)
elif 2000<=m<3000 :
    sum=(n*m*0.9)
elif m>=3000 :
    sum=(n*m*0.85)
print(round(sum))

7-4 sdut-阶梯电价

输入:10 0.05

输出:cost=5.30

n,m=map(float,input().split())
cost=0
if n<=0 :
    cost=0.00
elif n<=50 :
    cost=n*0.53
else:
    cost=n*0.53+(n-50)*m
print("cost=%.2f"%(cost))

7-5 sdut-分段计算居民水费

输入:12

输出:16.00

x=float(input())
y=0
if x<=0 :
    y=0.00
elif 0<x<=15 :
    y=(4*x)/3
else:
    y=2.5*x-17.5
print("%.2f"%(y))

7-6 sdut-循环-乘法运算

输入:4

输出:1*4=4
           2*4=8
           3*4=12
           4*4=16

n=int(input())
for i in range(1,n+1):
    print("%d*%d=%d"%(i,n,i*n))

7-7 sdut-求π的近似值

输入:0.00000001

输出:3.141497

import math
n=float(input())
sum,h,i=1,1,2
while h>n :
    h=1/(i*i)
    sum+=h
    i+=1
    c= math.sqrt(sum * 6)
print("%.6f"%(c))

7-8 sdut-求误差小于输入值的e的近似值(保留6位小数)

输入:0.01

输出:2.716667

def jc(n):
    sum=1
    for i in range(1,n+1):
        sum*=i
    return sum

error=float(input())
sum,i=0,0
while 1/jc(i)>error:
    sum+=1/jc(i)
    i+=1
sum+=1/jc(i)
print("%.6f"%sum)

7-9 sdut-入门-2 A+B for Input-Output Practice (II)

输入:2
           1 5
           10 20

输出:6
           30

n=int(input())
while n>0:
    a,b=map(int,input().split())
    print(a+b)
    n-=1

7-10 sdut0-入门-3 A+B for Input-Output Practice (III)

输入:1 5
           10 20
            0 0

输出:6
           30

while 1:
    try:
        a,b=map(int,input().split())
        if a==0 and b==0 : break
        else:print(a+b)
    except:
        break

7-11 sdut-入门-4 A+B for Input-Output Practice (IV)

输入:4 1 2 3 4
           5 1 2 3 4 5
           0

输出:10
           15

while 1:
    l=[int(num) for num in input().split()]
    sum=0
    if l[0]==0 : break
    else:
        for i in l:
            sum+=int(i)
    print(sum-l[0])

7-12 sdut-入门-5 A+B for Input-Output Practice (V)

输入:2
           4 1 2 3 4
           5 1 2 3 4 5

输出:10
           15

n = int(input())
while n>0:
    l=[int(num) for num in input().split()]
    sum = 0
    for i in l:
        sum+=int(i)
    print(sum-l[0])
    n-=1

7-13 sdut-九九乘法表

输入:2
           3

输出:1*1=1
           1*2=2 2*2=4
           1*1=1
           1*2=2 2*2=4
           1*3=3 2*3=6 3*3=9

while 1:
    try:
        n = int(input())
        for i in range(1,n+1):
            for j in range(1,i):
                print("%d*%d=%d"%(j,i,i*j),end=' ')
            print("%d*%d=%d"%(i,i,i*i),end='\n')
    except:
        break

7-14 sdut-平方数(I)

输入:3
           1 4
           3 10
           17 20

输出:5
           13
           0

import math
def pfs(n):
    n=int(n)
    if math.floor(math.sqrt(n))==math.ceil(math.sqrt(n)):#开平方后分别向下取整和向上取整
        return 1
    else: return 0

n=int(input())
while n>0:
    a,b=map(int,input().split())
    a,b=min(a,b),max(a,b)
    sum=0
    for i in range(a,b+1):
        if pfs(i)==1:
            sum+=i
    print(sum)
    n-=1

7-15 sdut-求交错序列前N项和

输入:5

输出:0.917

n=int(input())
sum,m=0.0,1.0
for i in range(1,n+1):
    if i&1:
        sum+=float(i/m)
    else:
        sum-=float(i/m)
    m+=2.0
print("%.3f" %(sum) )

7-16 sdut-生成输入数的乘方表

输入:2.5  3

输出:2.5**0=1.00
           2.5**1=2.50
           2.5**2=6.25
           2.5**3=15.62

n,m=input().split()
n=float(n)
m=int(m)
for i in range(0,m+1):
    print("%.1f**%d=%.2f"%(n,i,n**i))

7-17 sdut-水仙花数

输入:3

输出:153
           370
           371
           407

def shuixian(n,x):
    m=n
    sum=0
    while m>0:
        ans=1
        for i in range(x):
            ans*=m%10
        m//=10
        sum+=ans
    if sum==n:
        return 1
    return 0

n=int(input())
for i in range(10**(n-1),10**n):
    if shuixian(i,n)==1:
        print(i)

7-18 sdut-最大公约数和最小公倍数

输入:18 12
           20 15
           39 26
           5 76
           45 25
           1993 343

输出:6 36
           5 60
           13 78
           1 380
           5 225
           1 683599

def gcd(a,b):#最大公约数(余数变小数,小数变大数)
    a=int(a)
    b=int(b)
    if b:
        return gcd(b,a%b)
    return a
def lcm(a,b):
    a=int(a)
    b=int(b)
    return a*b/gcd(a,b)
while 1:
    try:
        n,m=map(int,input().split())
        print(int(gcd(n,m)),int(lcm(n,m)))
    except:
        break

7-19 sdut-求满足条件的斐波那契数

输入:10

输出:13

n=int(input())
a,b=1,1
while 1:
    if(a+b>n):
        print(a+b)
        break
    x=a
    y=b
    a=y
    b=x+y
    

7-20 sdut-冒泡排序中数据交换的次数

输入:3
           5 1 2 3 4 5
           4 5 3 7 1
           2 2 1

输出:0
           4
           1

n=int(input())
while n>0:
    l=[int(num) for num in input().split()]
    del l[0]#列表第一个元素是每行元素个数
    cut=0
    for i in range(len(l)-1):
        for j in range(len(l)-i-1):
            if l[j]>l[j+1]:
                l[j],l[j+1]=l[j+1],l[j]
                cut+=1
    print(cut)
    n-=1

  • 22
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值