等待时间问题(C++)

代码:

cpp 复制代码
//
// Created by LWJ on 2024-11-07 9:01.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    // 输入顾客数量和每个顾客的服务时间:
    int n;
    cout << "请输入顾客数量:";
    cin >> n;
    vector<int> ti(n);
    cout << "请输入每个顾客的服务时间:";
    for (int i = 0; i < n; ++i) {
        cin >> ti[i];
    }

    // 按照服务时间从小到大排序
    sort(ti.begin(), ti.end());

    // 等待时间问题按照活动用时的顺序为:
    cout << "等待时间问题按照活动用时的顺序为:" << endl;
    for (int i = 0; i < n; ++i) {
        cout << ti[i] << " ";
    }
    cout << endl;

    int t = 0;
    int current_time = 0;
    for (int i = 0; i < n; ++i) {
        t += current_time;
        current_time += ti[i];
    }

    cout << "最优服务次序下的总的等待时间为:" << endl;
    cout << t << endl;

    return 0;
}

运行截图:

相关推荐
信奥卷王1 小时前
[GESP202503 五级] 原根判断
java·数据结构·算法
兮山与1 小时前
算法4.0
算法
nju_spy1 小时前
力扣每日一题(二)任务安排问题 + 区间变换问题 + 排列组合数学推式子
算法·leetcode·二分查找·贪心·排列组合·容斥原理·最大堆
初听于你1 小时前
高频面试题解析:算法到数据库全攻略
数据库·算法
翟天保Steven2 小时前
ITK-基于Mattes互信息的二维多模态配准算法
算法
代码对我眨眼睛2 小时前
226. 翻转二叉树 LeetCode 热题 HOT 100
算法·leetcode·职场和发展
黑色的山岗在沉睡3 小时前
LeetCode 494. 目标和
算法·leetcode·职场和发展
Predestination王瀞潞5 小时前
IO操作(Num22)
开发语言·c++
haoly19895 小时前
数据结构和算法篇-线性查找优化-移至开头策略
数据结构·算法·移至开头策略
宋恩淇要努力6 小时前
C++继承
开发语言·c++