c++世界生存手册(开始——2)

故事

过了6天之后,你和小D终于醒了过来,发现任务单已经自动打了一个√
——————————————————

  • 1,2,3报数
  • 排队接水
  • 队员分组

——————————————————
于是决定继续完成任务,为了避免再次遭雷劈,把小D五花八绑了起来。并用了他的银行卡买了通关秘籍(叫你坑我(︶.̮︶✽))。

既然有了通关秘籍,那赶紧看看题目吧!
题目描述
有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小。
输入输出格式
输入格式:

输入文件共两行,第一行为n;第二行分别表示第1个人到第n个人每人的接水时间T1,T2,…,Tn,每个数据之间有1个空格。
输出格式:
输出文件有两行,第一行为一种排队顺序,即1到n的一种排列;第二行为这种排列方案下的平均等待时间(输出结果精确到小数点后两位)。

有点懵,看看通关秘籍吧!
打开通关秘籍,上面写着:
把时间长的放在后面接水,那么就较少人等,所以排序+贪心即可。对于每一个人,在场剩余每个人都要经历一次他的打水时间。故遍历整个数组求其他人在这个人打水时花费的总时间累加到ans上,最后ans除以n。

现在思路清楚了,就开始写代码吧!

#include<stdio.h>
#include<algorithm>
using std::sort;
struct water {
    int num,time;
     } p[1002];
double ans;
inline bool comp(water a,water b) {   
    if(a.time!=b.time) return a.time<b.time;
    return a.num<b.num;
}
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {  
        scanf("%d",&p[i].time);
        p[i].num=i;
    }
    sort(p+1,p+n+1,comp);
    for(int i=1; i<=n; i++)     
        printf("%d ",p[i].num);
    printf("\n");
    for(int i=1; i<=n; i++)
        ans+=i*p[n-i].time;     
    ans/=n;
    printf("%.2f",ans);
    return 0;
}

终于完成第2个任务了,可第3个任务还得等小H把分组牌带回来…(﹂_﹂)…。

———————————————————分割线—————————————————

博主受伤骨折,给点赞可怜可怜吧!

———————————————————————————————————————

English

Story

Six days later, you and little D finally woke up and found that the task list had been √
——————————————————

  • Number 1,2,3
  • Queuing for water
  • Team grouping

——————————————————
So decided to continue to complete the task, in order to avoid being hit by thunder again, small D was tied up in all sorts. And he used his bank card to buy the customs clearance secrets.(HA!Let you fool me(︶.̮︶✽))
Now that we have the secret books for customs clearance, let’s take a look at the title.

Title description
There are n people waiTing in front of a faucet to receive water. if everyone receives water for ti, please program to find out the order in which the n people queue so as to minimize the average waiting time of the n people.
Input format
The input file consists of two lines, the first line being n; The second line respectively represents the water receiving time T1, T2, …, Tn from the first person to the n-th person, with a space between each data.
output format
The output file has two lines. The first line is a queuing order, i.e. an arrangement of 1 to N; The second behavior is the average waiting time under this arrangement scheme (the output result is accurate to two decimal places).

A little confused, look at the customs clearance secrets!
Open the customs clearance secret book, which reads: After a long period of time, there will be fewer people waiting to receive water, so sorting+greed is enough. For everyone, everyone else present will have to go through his water-fetching time. Therefore, the whole array is traversed to find out that the total time spent by others in fetching water by this person is added to ans, and ans is divided by N at last. Now that you have a clear idea, start writing code!

#include<stdio.h>
#include<algorithm>
using std::sort;
struct water {
    int num,time;
     } p[1002];
double ans;
inline bool comp(water a,water b) {   
    if(a.time!=b.time) return a.time<b.time;
    return a.num<b.num;
}
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {  
        scanf("%d",&p[i].time);
        p[i].num=i;
    }
    sort(p+1,p+n+1,comp);
    for(int i=1; i<=n; i++)     
        printf("%d ",p[i].num);
    printf("\n");
    for(int i=1; i<=n; i++)
        ans+=i*p[n-i].time;     
    ans/=n;
    printf("%.2f",ans);
    return 0;
}

Finally done the second one,but the third one have to wait little H bring the grounding tap…(﹂_﹂)…

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值