Python练习题 判断语句(19~36)三

Demo31
**

(科学问题: -周的星期几)泽勒的一致性是- -个由泽勒开发的算法,用于计算-周的星期儿。 这个公式是

●这里的h是指一間的星期几(0:星期六; 1:星期天; 2:星期一; 3:星期二; 4:星期三; 5:
星期四: 6;星期五)。
●q是一个月的哪一天。
●m是月份(3:三月; 4:四月; .; 12:十二月)。一月和二月都是按照前一年的13月和14.
月来计数的。
●j是世纪数(即year/100)
●k是一个世纪的某- -年(即year % 100)。
编写程序提示用户输人一个年份、月份以及这个月的某天,然后它会显示它是一周的星期
几。下面是一些示例运行。
Enter year: (e.g… 2008): 2013
Enter month: 1-12: 1
Enter the day of the month: 1-31: 25
Day of the week is Friday

Enter year: (e.g., 2008): 2012
Enter month: 1-12: 5
Enter the day of the month: 1-31: 12
Day of the week is Saturday
(提示: ["]=n//1其中n 是一个正数。一月和二月在公式中是以13和14来计算的,所以你
需要将用户输人的月份1转换为13和将用户输人的2转换为14,将它们的年份改变为前一-年。)
在这里插入图片描述
程序编辑:

# 数据:年份,,月份,日期,现在的日期
# 步骤:
# 1.提示用户根据要求输入今天的日期,例如(星期六是0,星期天是1,星期一是2,
#   星期二是3,星期三是4,星期四是5,星期五是6)
# 2.提示用户输入想要查询的年份和日期
# 3.计算出周几
# 4.输出周几
import math
year = eval(input("Enter year:(e,g.,2008):"))
month = eval(input("Enter month:1-12:"))
q = eval(input("Enter the day of the month:1-31:"))
m = 0
if month == 1 or month == 2:
    m = 12 + month
    year = year - 1
j = year // 100
k = year % 100
h = (q + ((26 * (m + 1)) // 10) + k + k // 4 + j // 4 + 5 * j) % 7
if h == 0:
    print("Today is Sunday and the future day is 星期六")
elif h == 1:
    print("Today is Sunday and the future day is 星期天")
elif h == 2:
    print("Today is Sunday and the future day is 星期一")
elif h == 3:
    print("Today is Sunday and the future day is 星期二")
elif h == 4:
    print("Today is Sunday and the future day is 星期三")
elif h == 5:
    print("Today is Sunday and the future day is 星期四")
elif h == 6:
    print("Today is Sunday and the future day is 星期五")

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/31.py
Enter year:(e,g.,2008):2020
Enter month:1-12:8
Enter the day of the month:1-31:6
Today is Sunday and the future day is 星期四

Process finished with exit code 0

在这里插入图片描述

Demo32
**

(几何问题:点在圆内吗? )编写一个程序提示用户输入一个点(x.,),然后检测这个点是否在圆 心为(0,0)半径为10的圆内。例如:点(4,5)在圆内而(9,9)在圆外,如图4-8a所示。

**
a)圆内和圆外的点
(提示:如果一个点到(0,0)之间的距离小于或等于10,那它就在圆内。计算距离的公式是
V(x2-x) +02-)户。测试你的程序考虑所有的情况)。下面是两个示例运行。

Enter a point with two coordinates: 4,5
Point (4.0, 5.0) is in the circle
Enter a point with two coordinates: 9, 11
Point (9.0, 9.0) is not in the circle
在这里插入图片描述
程序编辑:

x, y =eval((input("Enter a point with two coordinates:")))
if x <= 10 and y <= 10:
    print("Point (%.1f,%.1f) is in the circle"%(x,y))
else:
    print("Point (%.1f,%.1f) is not in the circle"%(x,y))

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/32.py
Enter a point with two coordinates:9,11
Point (9.0,11.0) is not in the circle

Process finished with exit code 0

在这里插入图片描述

Demo33
**

(几何问题:点在矩形内吗? )编写程序提示用户输入点(x.J),然后检测这个点是否在以(0,0)为 中心而宽为10高为5的矩形内。例如: (2,2) 在矩形内而(6,4)在矩形外,如图4-8b所示。(提 示:如果一个点到(0,0)的水平距离小于或等于10/2 而到(0,0)的垂直距离小于或等于5.0/2。测 试你的程序覆盖所有的情况。)下面是两个示例运行。

**
在这里插入图片描述
程序编辑:

x, y = eval(input("Enter a point with two coordinates:"))
if x >= -5 and x <= 5 and y <= 2.5 and y >= -2.5:
    print("Point (%.1f,%.1f) is in the rectangle"%(x,y))
else :
    print("Point (%.1f,%.1f) is not in the rectangle" % (x, y))

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/33.py
Enter a point with two coordinates:2,2
Point (2.0,2.0) is in the rectangle

Process finished with exit code 0
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/33.py
Enter a point with two coordinates:6,4
Point (6.0,4.0) is not in the rectangle

Process finished with exit code 0

在这里插入图片描述
Demo34
**

(回文数)编写程序提示用户输入一个三位整数,然后决定它是否是-一个回文数。如果一个数从 左向右和从右向左读取时是一-样的,那么这个数就是回文数。下面是 这个程序的示例运行。

**
Enter a three-digit integer: 121
121 is a palindrome

Enter a three-digit integer: 123
123 is not a palindrome

程序编辑:

number = int(input("Enter a three-digit integer:"))
a = number % 10
b = number // 100
if a == b:
    print("%d is a palindrome" %number)
else :
    print("%d not is a palindrome" %number)

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/34.py
Enter a three-digit integer:121
121 is a palindrome

Process finished with exit code 0
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/34.py
Enter a three-digit integer:123
123 not is a palindrome

Process finished with exit code 0

在这里插入图片描述
Demo35

(几何问题:点在三角形内吗? )假设一个直角三角形被放在个水平面上,如下图所示。直角点是在(0,0)而另外两个点在(200,0)和(0,100)处。编写程序提示用户输入一个带x坐标和y坐标的点,然后决定这个点是否在三角形内。下面是一些示例运行。

Enter a point’s x- and y-coordinates: 100.5, 25.5
The point is in the triangle
Enter a point’s x- and y-coordinates: 100.5, 50.5
The point is not in the triangle
在这里插入图片描述

程序编辑:

x, y = eval(input("Enter a point's x- and y-coordinates:"))
# 先限定xy的范围
if x >= 0 and x <= 200 and y >= 0 and y <= 100:
    k = y / (200 - x)
    if k <= 100 / 200:
        print("The point is in the triangle")
    else:
        print("The point is not in the triangle")
else:
    print("The point is not in the triangle")

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/35.py
Enter a point's x- and y-coordinates:100.5,25.5
The point is in the triangle

Process finished with exit code 0

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/35.py
Enter a point's x- and y-coordinates:100.5,50.5
The point is not in the triangle

Process finished with exit code 0

在这里插入图片描述

Demo36
**

(几何问题:两个圆)编写程序提示用户输人两个圆的中心的坐标以及它们的半径,然后判断第二个圆是在第一-个圆内还是和第一个圆有重叠部分, 如图4-11所示。(提示:如果两个中心的距 离≤|rl - r2|那么circle2 在circlel内,如果两个中心的距离≤rl + r2那么circle2是和circlel有重叠的。测试你的程序覆盖所有的情况。)

**
在这里插入图片描述

程序编辑:

x1, y1, r1= eval(input("Enter circlel's_ center x1,y1coordinates,and radius:"))
x2, y2, r2= eval(input("Enter circlel's_ center x2,y2coordinates,and radius:"))
d = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5

if d <= abs(r1 - r2):
    print("circle2 is inside circle1")
elif  d <= r1 + r2:
    print("circle2 overlaps circle1")
else:
    print("circle2 does not overlap circle")

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/36.py
Enter circlel's_ center x1,y1coordinates,and radius:0.5,5.1,13
Enter circlel's_ center x2,y2coordinates,and radius:1,1.7,4.5
circle2 is inside circle1

Process finished with exit code 0


J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/36.py
Enter circlel's_ center x1,y1coordinates,and radius:0.5,5.1,13
Enter circlel's_ center x2,y2coordinates,and radius:1,1.7,4.5
circle2 is inside circle1

Process finished with exit code 0


J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/36.py
Enter circlel's_ center x1,y1coordinates,and radius:4.4,5.5,1
Enter circlel's_ center x2,y2coordinates,and radius:5.5,7.2,1
circle2 does not overlap circle

Process finished with exit code 0

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值