Exercise(1):计数的梦

/*
    张德志
            RQNOJ
    PID11 / 计数的梦
        题目描述
    Bessie 处于半梦半醒的状态。过了一会儿,她意识到她好像在数羊,不能入睡。Bessie的大脑反应灵敏,仿佛真实地看到了她数过的一个又一个数。她开始注意每一个数码:每一个数码在计数的过程中出现过多少次?
    给出两个整数 M 和 N (1 <= M <= N <= 2,000,000,000 以及 N-M <= 500,000),求每一个数码出现了多少次。
    例如考虑序列 129..137: 129, 130, 131, 132, 133, 134, 135, 136, 137。统计后发现:
    1x0 1x5
    10x1 1x6
    2x2 1x7
    9x3 0x8
    1x4 1x9
    输入格式
    共一行,两个用空格分开的整数 M 和 N
    输出格式
    共一行,十个用空格分开的整数,分别表示数码(0..9)在序列中出现的次数。

    样例输入:
    129 137

    样例输出:
    1 10 2 9 1 1 1 1 0 1

*/
#include <iostream>
using namespace std;
int main()
{
    long 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言实现的程序,实现了Exercise 1至6的要求: ```c #include <stdio.h> #include <stdlib.h> #define MAX_PROCESS_NUM 5 // 最大进程数 #define MAX_TIME_SLICE 5 // 最大运行时间片 // 进程控制块 typedef struct PCB { int pid; // 进程ID int priority; // 优先数 int time_required; // 要求运行时间 int dynamic_priority; // 动态优先数 struct PCB *next; // 指向下一个进程控制块的指针 } PCB; // 初始化进程控制块 void init_pcb(PCB *pcb, int pid, int priority, int time_required) { pcb->pid = pid; pcb->priority = priority; pcb->time_required = time_required; pcb->dynamic_priority = priority; pcb->next = NULL; } // 将进程插入就绪队列 void insert_ready_queue(PCB **head, PCB *pcb) { PCB *p, *q; if (*head == NULL || pcb->dynamic_priority > (*head)->dynamic_priority) { pcb->next = *head; *head = pcb; } else { p = *head; q = p->next; while (q != NULL && pcb->dynamic_priority <= q->dynamic_priority) { p = q; q = q->next; } pcb->next = q; p->next = pcb; } } // 从就绪队列中删除进程 PCB *remove_ready_queue(PCB **head) { PCB *pcb = *head; if (*head != NULL) { *head = (*head)->next; } return pcb; } // 动态地调整进程的优先级和要求运行时间 void adjust_priority_and_time(PCB *pcb) { pcb->dynamic_priority--; pcb->time_required--; if (pcb->dynamic_priority < 0) { pcb->dynamic_priority = 0; } } // 显示就绪队列中的进程 void display_ready_queue(PCB *head) { if (head == NULL) { printf("Ready queue is empty.\n"); return; } printf("Ready queue:"); while (head != NULL) { printf(" P%d(%d,%d,%d) ->", head->pid, head->priority, head->dynamic_priority, head->time_required); head = head->next; } printf(" NULL\n"); } int main() { int i; PCB *pcb[MAX_PROCESS_NUM]; PCB *ready_queue = NULL; char process_name[MAX_PROCESS_NUM][3] = {"P0", "P1", "P2", "P3", "P4"}; // 输入每个进程的优先数和要求运行时间 for (i = 0; i < MAX_PROCESS_NUM; i++) { int priority, time_required; printf("%s:\n", process_name[i]); printf("Priority: "); scanf("%d", &priority); printf("Time required: "); scanf("%d", &time_required); pcb[i] = (PCB *)malloc(sizeof(PCB)); init_pcb(pcb[i], i, priority, time_required); } // 将五个进程按给定的优先数从大到小连成就绪队列 for (i = 0; i < MAX_PROCESS_NUM; i++) { insert_ready_queue(&ready_queue, pcb[i]); } // 处理机调度总是选队首进程运行 while (ready_queue != NULL) { PCB *running_process = remove_ready_queue(&ready_queue); printf("Running process: %s\n", process_name[running_process->pid]); adjust_priority_and_time(running_process); if (running_process->time_required > 0) { insert_ready_queue(&ready_queue, running_process); } else { printf("Process %s is finished.\n", process_name[running_process->pid]); free(running_process); } display_ready_queue(ready_queue); } return 0; } ``` 运行程序后,依次输入五个进程的优先数和要求运行时间,程序会将它们按照优先数从大到小的顺序连成就绪队列,并开始动态优先数算法的调度。程序会输出每次运行的进程名,以及进程控制块的动态变化过程。当某个进程的运行时间为零时,程序会将其状态置为“结束”,且从就绪队列中删除。最后程序结束时,会释放所有进程控制块的内存空间。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值