2023年秋季学期《算法分析与设计》练习8 OJ-1394 算法分析与设计练习8,使用python

X星数列

题目描述

爱冒险的X星人在一艘海底沉船上发现了一串神秘数列,这个数列的前8项如下:
5, 8, 18, 34, 70, 138, 278, 554
X星人对这串数列产生了浓厚的兴趣,他希望你能够帮他发现这个神秘数列中所蕴含的规律,并且编写一个程序输出该数列前N项的和。
当输入一个正整数N时,请输出这个神秘数列前N项的和。

输入

单组输入,每组输入一个正整数N(N<=20)。

输出

输出一个正整数,对应这个数列前N项的和。

样例输入 Copy
4
样例输出 Copy
65
count = int(input())
sum = 0

def func(count):
    if count == 1:
        return 5
    elif count == 2:
        return 8
    else:
        return func(count - 2) * 2 + func(count - 1)

for i in range(count):
    sum += func(i + 1)
print(sum)

X星计数

题目描述

热爱数学的X星人又发现了一个有趣的游戏,游戏名叫做:1的个数。
具体游戏规则为:给定一个十进制正整数,计算对应的二进制整数中有多少个1。

输入

多组输入。

第一行输入一个整数T(1<=T<=100),表示总共有T组数据。

接下来T行,每行一个十进制正整数n,其中1<=n<=1e^9。

输出

对于每一组输入,输出十进制正整数对应的二进制整数中包含的1的个数。

样例输入 Copy
3
1
9
12
样例输出 Copy
1
2
2
count = int(input())
for x in range(count):
    a = int(input())
    flag = ""
    while a != 0:
        flag = flag + str(a % 2)
        a = int(a // 2)
    # print(flag)
    count_1 = 0
    for i in range(len(flag)):
        if int(flag[i]) == 1:
            count_1 += 1
    print(count_1)

X星宿舍

题目描述

X星大学的宿舍很有意思,男生都是6人间,女生都是5人间。
现在已知N个学生的性别,性别用'0'或者'1'表示,其中'0'表示男生,'1'表示女生。
请你编写一个程序计算最少需要多少间男生宿舍?多少间女生宿舍?
(注:不要求每一间宿舍都住满)。

输入

单组输入。
输入N个0或1,两两之间用英文空格隔开,且N不超过1000。

输出

输出两个整数,分别表示最少需要的男生宿舍和女生宿舍数量,两者之间用英文空格隔开。

样例输入 Copy
0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 1 0 0 1
样例输出 Copy
2 2
data = list(input().split())
girl_count = 0
boy_count = 0

for i in data:
    if int(i) == 1:
        girl_count += 1
    elif int(i) == 0:
        boy_count += 1

girl_room = girl_count // 5
boy_room = boy_count // 6

if boy_count % 6 != 0:
    boy_room += 1
if girl_count % 5 != 0:
    girl_room += 1

print(boy_room, girl_room)

# str = input()
# count = 0
# man = 0
#
# for i in range(len(str)):
#     if str[i] == '1':
#         count += 1
#     elif str[i] == '0':
#         man += 1
#
# x = man // 6
# y = count // 5
#
# if man % 6 != 0:
#     x += 1
# if count % 5 != 0:
#     y += 1
#
# print(x, y)

Welcome

题目描述

”How happy we are, To meet friends from afar!”
Welcome to Hunan University of Chinese Medicine!
Hope all of you can enjoy the competition ^ v ^
Now your task is to read an integer w and output the character painting of ”HNUCM”, there are w
space(s) (space are represented by dot) between two letters. Please refer to the sample for the specific
format.

输入

There are several test files and each contains one case.
The input contains only 1 integer w (1 ≤ w ≤ 2018).

输出

The output has 5 lines, each line has 25+4w characters which only contains ’o’(lowercase letter ’o’) and
’.’(English period ’.’)

样例输入 Copy
1
样例输出 Copy
o...o.o...o.o...o.ooooo.o...o
o...o.oo..o.o...o.o.....oo.oo
ooooo.o.o.o.o...o.o.....o.o.o
o...o.o..oo.o...o.o.....o...o
o...o.o...o.ooooo.ooooo.o...o
while 1 < 2:
    w = int(input())
    a = '.' * w
    print("o...o" + a + "o...o" + a + "o...o" + a + "ooooo" + a + "o...o")
    print("o...o" + a + "oo..o" + a + "o...o" + a + "o...." + a + "oo.oo")
    print("ooooo" + a + "o.o.o" + a + "o...o" + a + "o...." + a + "o.o.o")
    print("o...o" + a + "o..oo" + a + "o...o" + a + "o...." + a + "o...o")
    print("o...o" + a + "o...o" + a + "ooooo" + a + "ooooo" + a + "o...o")

第k大元素

题目描述

输入一个整数数组,请求出该数组的第k大元素。要求时间复杂度为O(n)。
 

输入

每组输入包括两行,第一行为k值;第二行为一个整数数组,两个数字之间用空格隔开。数组中元素个数小于1000。

输出

输出第k大元素的值,每个结果占一行

样例输入 Copy
2 
3 2 1 5 6 4
样例输出 Copy
5
while True:
    index = int(input())
    data = list(input().split())
    data.sort()
    # print(data)
    print(data[len(data) - index])

整数划分问题之备忘录法

题目描述

使用备忘录法编写一个程序,求一个正整数n的所有划分个数。 
例如,输入3,输出3;输入4,输出5。 

输入

多组输入,每一组是一个正整数n。

输出

输出划分数。

样例输入 Copy
3
4
样例输出 Copy
3
5
def solve(n, m):
    if n < 1 or m < 1:
        return 0
    elif n == 1 or m == 1:
        return 1
    elif n < m:
        return solve(n, n)
    elif n == m:
        return solve(n, n - 1) + 1
    elif n > m:
        return solve(n, m - 1) + solve(n - m, m)

while True:
    n = int(input())
    print(solve(n, n))
    """
// 备忘录法不知道为什么oj报错,我自己运行是对的
def solve(n, m, memory):
    if memory[0][n] > 0:
        return memory[0][n]
    elif n < 1 or m < 1:
        memory[0][n] = 0
        return memory[0][n]
    elif n == 1 or m == 1:
        memory[0][n] = 1
        return memory[0][n]
    elif n < m:
        memory[0][n] = solve(n, n, memory)
        return
    elif n == m:
        memory[0][n] = solve(n, n - 1, memory) + 1
        return memory[0][n]
    elif n > m:
        memory[0][n] = solve(n, m - 1, memory) + solve(n - m, m, memory)
        return memory[0][n]

while True:
    memory = np.zeros((1, 1000))
    n = int(input())
    print(int(solve(n, n, memory)))
"""

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值