2024/5/8

所花时间:1小时

代码行:70行

博客量:1篇

了解到的知识点:

def kaprekar(num):

    while num != 6174:

        digits = [int(x) for x in str(num)]

        digits.sort()

        asc_num = int(''.join(map(str, digits)))

        digits.sort(reverse=True)

        desc_num = int(''.join(map(str, digits)))

        num = desc_num - asc_num

        print(num, end=' ')

 

# 读取输入的4位数

num = input("请输入一个任意各位数字不相同的4位数:")

num = int(num)

 

# 验证6174猜想

print(num, end=' ')

kaprekar(num)

def lru(page_sequence, n):

    page_frames = []  # 存储主存块页面

    page_faults = 0  # 缺页次数

    page_order = []  # 存储页面的访问顺序

    for page in page_sequence:

        if page not in page_frames:

            page_faults += 1

            if len(page_frames) < n:

                page_frames.append(page)

                page_order.append(page)

            else:

                # 找到最早访问的页面并替换

                replace_page = page_order.pop(0)

                page_frames[page_frames.index(replace_page)] = page

                page_order.append(page)

        else:

            # 更新页面的访问顺序

            page_order.remove(page)

            page_order.append(page)

    return page_faults

 

# 读取输入的主存块数量和页面访问次序

n = int(input())

page_sequence = list(map(int, input().split()))

 

# 计算缺页次数

page_faults = lru(page_sequence, n)

 

# 输出结果

print(page_faults)

 

posted @ 2024-05-08 21:42  为20岁努力  阅读(65)  评论(0编辑  收藏  举报