等待时间问题(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;
}

运行截图:

相关推荐
混分巨兽龙某某19 小时前
基于Qt Creator的Serial Port串口调试助手项目(代码开源)
c++·qt creator·串口助手·serial port
西红柿维生素19 小时前
JVM相关总结
java·jvm·算法
小冯记录编程19 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
C_Liu_20 小时前
C++:类和对象(下)
开发语言·c++
coderxiaohan20 小时前
【C++】类和对象1
java·开发语言·c++
阿昭L20 小时前
MFC仿真
c++·mfc
ChillJavaGuy21 小时前
常见限流算法详解与对比
java·算法·限流算法
sali-tec21 小时前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
你怎么知道我是队长1 天前
C语言---循环结构
c语言·开发语言·算法
艾醒1 天前
大模型面试题剖析:RAG中的文本分割策略
人工智能·算法