BUAA Python Programming(2020级·2021夏·小学期)——第一次作业

看前须知

要点介绍和简要声明.

题目内容

1.Classification average

题面

【Description】

Given n and k, all positive integers from 1 to n can be divided into two classes: class A numbers can be divisible by k, while class B numbers can not. Please output the average of the two types of numbers, accurate to 1 decimal place, separated by spaces.

【Input】

n k

【Output】

two average number

【Example Input】

100 16
【Example Output】

56.0 50.1
【Notes】

1<n≤10000

1<k≤100

Data guarantees that the number of two types of numbers will not be zero.

题解

就是简单的分类指定范围内的数,这里我们可以采用两种方法:

  1. 采用列表解析式,快速分类至列表中,方便后续操作
  2. 采用传统的for循环,遍历一次找到需要的数

参考代码

列表解析式

List = input().split()
n = int(List[0])
k = int(List[1])
List_mod = [x for x in range(1,n+1) if (x % k)==0]
List_notmod = [x for x in range(1,n+1) if (x % k)!=0]
print("%.1f %.1f" % (sum(List_mod)/len(List_mod),sum(List_notmod)/len(List_notmod)))

传统的for循环

str = input()
list = str.split(' ')
n = int(list[0])
k = int(list[1])


sum1 = 0
sum2 = 0
cnt1 = 0
cnt2 = 0

for i in range(1, n + 1):
    if i % k == 0:
        sum1 += i
        cnt1 += 1
    else:
        sum2 += i
        cnt2 += 1

r1 = sum1/cnt1
r2 = sum2/cnt2

print('%.1f %.1f' % (r1, r2))

2.Substring

题面

【Description】

Given two strings s s s and t t t, returns the number of times that t t t appears in s s s. Note that each character cannot be calculated twice.

【Input】

Two lines, string s s s and t t t.

【Output】

If s s s contains t t t, outputs the number of times that t t t appears in s s s. Otherwise outputs False.

【Example Input1】

abababa
aba
【Example Output1】

2
【Example Input2】

ab
aba
【Example Output2】

False
【Notes】

The question is to count how many T does s have, not how many T appear in S

题解

就是简单的对子串计数,这里我们可以采用两种方法:

  1. 采用字符串里内置方法count
  2. 引入正则表达式的库,使用库中的findall方法

参考代码

字符串里内置方法count

first = input()
second = input()
if first.count(second)>0:
    print(first.count(second))
else:
    print('False')

使用正则表达式的库中的findall方法

import re

str1 = input()
str2 = input()

cnt = re.findall(str2, str1)

if len(cnt) == 0:
    print('False')
else:
    print(len(cnt))


3.X’s Study Plan

题面

【Background】

In order to pass the final exam, X has set a study plan for himself. He decided to give himself a target learning time every day. He could not relax until he completed the target learning time, but he would ensure enough sleep. If the end of the learning time was later than 23:30, he would finish the day’s learning at 23:30. He asked you to write a program to calculate when he will finish the day’s study, given the starting time and target learning time.

【Input】

Two lines. The first line has two space separated integers, representing the hour and minute of the starting time. The second line is an integer, which is the target learning time in minutes.

【Output】

A line, xx:xx denoting the end of learning time. If hour or minute is less than two characters, fill it with 0.

【Example Input1】

22 30
50
【Example Output1】

23:20
【Example Input2】

23 00
40
【Example Output2】

23:30

题解

就是简单的比较时间以及时间的加法运算,这里我们可以采用两种方法:

  1. 采用分钟超过60就减60,小时+1,直至分钟小于60
  2. 统一采用分钟计算,最后化简

参考代码

采用分钟超过60就减60,小时+1,直至分钟小于60

Time = input().split()
Hour = int(Time[0])
Minute = int(Time[1])
LearningTime = int(input())
TotalTime = LearningTime + Minute
if TotalTime >=60:
    while TotalTime >=60:
        TotalTime -= 60
        Hour +=1
    Minute = TotalTime
else :
    Minute += LearningTime
if (Hour >= 23) and (Minute >= 30) :
    print("23:30")
else:
    print(str(Hour)+':'+str(Minute).zfill(2))

统一采用分钟计算,最后化简

time = input()
list_time = time.split(' ')
hour = int(list_time[0])
minute = int(list_time[1])

add = int(input())

total = hour * 60 + minute + add
deadline = 23 * 60 + 30

if total > deadline:
    total = deadline

hour_new = total // 60
minute_new = total % 60
print('%02d:%02d' % (hour_new, minute_new))

4. Terrifying Morning Class

题面

X always sleeps at morning classes. One day, his English teacher cannot bear him anymore, and asks he to do a presentation on the harms of staying up late. Despite of his reluctancy, X collects a bunch of materials for the presentation, and is parised by the teacher. His teacher asks a strange question: how many english characters and digits are in his materials?

【Description】

Given an English sentence, returns the number of digits and English characters in the sentence.

【Input】

A line. The sentence to be calculated.

【Output】

Two lines. The first line is the number of digits in the string. The second line if the number of English characters in the string.

【Example Input】

81% of Chinese people sleep less than 8 hours.
【Example Output】

3
33
【Example Description】

  1. For 100% of the data, the length of the sentence is no greater than 1000 1000 1000.

  2. s.isdigit() judges whether s is made of digits.

  3. s.isalpha() judges whether s is made of English characters.

题解

就是简单的统计字母和数字

参考代码

cnt_alpha = 0
cnt_digit = 0
str_input = input()
for i in str_input:
    if i.isalpha():
        cnt_alpha += 1
    elif i.isdigit():
        cnt_digit +=1
print(("%d\n%d") % (cnt_digit,cnt_alpha))

5.Perimeter of Triangle

题面

【Description】

Given the coordinates of three vertices of a triangle in Cartesian coordinate system, returns the perimeter of the triangle.

【Input】

Three lines. The i i i-th line is x i x_i xi and y i y_i yi seperated by space, representing the coordinate of the i i i-th vertice.

【Output】

A line, the perimeter of the triangle with two decimal places.

【Example Input】

0 0
0 3
4 0
【Example Output】

12.00
【Example Description】

  1. Use math.sqrt(x) from package math to calculate the square root of x.

  2. Coordinates can be decimals. They can also be negative numbers.

  3. We guarantee that the three vertices can form a triangle.

题解

就是简单的使用math库中的sqrt方法,这里我们可以采用两种方法:

  1. 使用函数化简重复操作
  2. 不怕麻烦全部写出来

参考代码

使用函数化简重复操作

import math
def Calaulate(x1,y1,x2,y2):
    return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
List_input = input().split()
x1 = int(List_input[0])
y1 = int(List_input[1])
List_input = input().split()
x2 = int(List_input[0])
y2 = int(List_input[1])
List_input = input().split()
x3 = int(List_input[0])
y3 = int(List_input[1])
print("%.2f" % (Calaulate(x1,y1,x2,y2) + Calaulate(x3,y3,x2,y2) +Calaulate(x1,y1,x3,y3)))

不怕麻烦全部写出来

import math

str = input()
nums = str.split(' ')
x1, y1 = int(nums[0]), int(nums[1])

str = input()
nums = str.split(' ')
x2, y2 = int(nums[0]), int(nums[1])

str = input()
nums = str.split(' ')
x3, y3 = int(nums[0]), int(nums[1])

l1 = math.sqrt((x1 - x3)**2 + (y1 - y3)**2)
l2 = math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
l3 = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
num = l1 + l2 + l3
print('%.2f' % num)

6. Triangle

题面

【Description】

Given a character, please use this character to draw an isosceles triangle of base length 5 and height 3 with the character. See output example for details.

【Input】

A line, one character.

【Output】

Three lines, an isosceles triangle satisfying the requirement.

【Example Input】

@
【Example Output】

@
@@@
@@@@@
【Example Description】

None

题解

就是简单的画图,目的是要求会使用字符串的乘法

参考代码

ch = input()
print('  ' + ch + '  ')
print(' ' + ch * 3 + ' ')
print(ch * 5)

7. Division Opearion

题面

【Description】

We all know that there are more than one division operators in Python. For two given integers a and b, output the quotients of division between a and b and true division (Euclidean division) between a and b.

【Input】

A line, intergers a and b seperated by space.

【Output】

Two lines. The first line is the quotient of a divided by b. The second line is the quotient of the true division between a and b, with two decimal places(By rounding).

【Examples Input】

3 2

【Examples Output】

1

1.50

【Examples Description】

3/2 = 1

3/2 = 1.50
【Notes】

None

题解

就是要求会使用正常除法和保留整数的除法

参考代码

str = input()
nums = str.split(' ')
x = int(nums[0])
y = int(nums[1])
print(x // y)
print('%.2f' % (x / y))

8. Bear’s birthday

题面

【Background】

Bear has just learned about a programming language Python. He wants to print out his birthday with computer.

【Description】

Output the birthday in the following format.

【Input】

Three lines, each is an integer, representing year, month and date respectively.

【Output】

A line, My birthday is xxxx.xx.xx. If month or date is less than two characters, fill it with 0.

【Examples Input】

2000

5

16

【Examples Output】

My birthday is 2000.05.16

【Examples Description】

None
【Notes】

None

题解

就是要求会使用格式化输出字符串

参考代码

year = int(input())
month = int(input())
day = int(input())

print('My birthday is %04d.%02d.%02d' % (year, month, day))

9. Swimming

题面

【Description】

Xiaoyu is swimming happily, but she is soon sad to find that her strength is not enough, swimming is very tired. It is known that Xiaoyu can swim 2 meters in the first step, but as she gets more and more tired, her strength becomes smaller and smaller, and her next step can only swim 98% of the distance of the previous step. Now Xiaoyu wants to know how many steps she needs to swim to a distance of X meters. Please program to solve this problem.

【Input】

target distance to swim.

【Output】

how many steps Xiaoyu needs to swim

【Example Input】

4.3
【Example Output】

3
【Notes】

target distance <=100-1e^-8, and may not be an integer

题解

就是要求会循环和循环退出

参考代码

distance = float(input())
cnt = 0
totalX = 0

while True:
    if totalX > distance:
        break
    totalX += 2*(0.98**cnt)
    cnt += 1

print(cnt)

10. Group

题面

【Description】

Gives two integers a and b as input, returns the value of a+b+a*b.

【Input】

A line contains two integers, seperated by space, representing a and b.

【Output】

An integer, the value of a+b+a*b.

【Examples Input】

1 1
【Examples output】

3
【Example description】

1+1+1*1=3
【Notes】

For 100% of test cases, a,b in [1, 10000].

题解

不说多了,会英语的都知道

参考代码

str = input()
nums = str.split(' ')

a = int(nums[0])
b = int(nums[1])

print(a + b + a * b)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值