H.House Lawn---Gym101933(字符串的处理)

House Lawn

Time Limit: 1 Sec Memory Limit: 128 Mb

上面的链接是比赛的,已经交不了了,如果想练一练的话可以在这里:
练习链接https://codeforces.com/gym/101933/problem/H

You have just bought a new house, and it has a huge, beautiful lawn. A lawn that needs cutting. Several times. Every week. The whole summer.

After pushing the lawnmower around the lawn during the hottest Saturday afternoon in history, you decided that there must be a better way. And then you saw the ads for the new robotic lawnmovers. But which one should you buy? They all have different cutting speeds, cutting times and recharge times, not to mention different prices!

According to the advertisement, a robotic lawnmover will spend all its time either cutting the lawn or recharging its battery. Starting from a full battery, it will cut the lawn at a given rate of c square meters per minute for a cutting time of t minutes, after which it has run out of battery. Once out of battery, it will immediately start recharging. After recharging for r minutes the battery is full again and it immediately starts cutting.

You decide that in order for your lawn to look sufficiently prim and proper, the lawnmower that you buy must be powerful enough to cut your whole lawn at least once a week on average. Formally, if we start the mower fully charged at the beginning of the week and run it for exactly T weeks, it needs to cut the whole lawn at least T times, for all positive integers T. But apart from this, you have no specific requirements, so among the ones that satisfy this requirement, you will simply go for the cheapest option. For the purposes of cutting your lawn, you may make the simplifying assumption that a week is always exactly 10 080 minutes long.

Input
The first line of input contains two integers ℓ and m (1 ≤ ℓ ≤ 10^6, 1 ≤ m ≤ 100), the size of your lawn in square meters, and the number of lawnmowers to consider, respectively.

Then follow m lines, each containing a string n and 4 integers p, c, t, and r, separated by commas, describing a lawnmower as follows:

n is the name of the lawnmower, a string of at most 60 printable characters (ASCII 32 to 126) excluding ‘,’, neither starting nor ending with a space,

1 ≤ p ≤ 100 000 is the price of the lawnmover,

1 ≤ c ≤ 100 is the cutting rate in square meters per minute,

1 ≤ t ≤ 10 080 is the cutting time in minutes, and

1 ≤ r ≤ 10 080​ is the recharge time in minutes.

Output
Output the name of the cheapest lawnmower capable of cutting your whole yard at least once a week on average. If several lawnmovers share the same lowest price, output all of their names, in the same order they were given in the input. If there is no such mower, output “no such mower”.

Sample Input
7000 4
Grass Slayer 2000,9999,10,120,120
Slow-Mowe,999,1,120,240
Eco-cut X2,5499,2,25,35
Mowepower,5499,3,25,35


100000 4
Grass Slayer 2000,9999,10,120,120
Slow-Mowe,999,1,120,240
Eco-cut X2,5499,2,25,35
Mowepower,5499,3,25,35

Sample Output
Eco-cut X2
Mowepower


no such mower


这题的题目不难读懂,大意是给你你家草坪的大小,以及割草机的数目m,接下来m行,每行分别为:名字,价格,割草的速度(平方米/min),能工作的时间,一次充电所需的时间。让你找到一款最便宜的割草机使得它能在一周之内割完整个草坪。

存储数据的详解请看代码,这里就不解释了,当然你也可以换一种比较容易看懂的方法(如果你的字符串没有过关的话,不懂strtok(s, d)和atoi (p)等也只能用这种笨办法了)。

	scanf ("%lld%lld",&l,&m);
	gets(s);   //读掉回车
	for (int i=1; i<=m; i++){
		gets(s);
		ll nb=0,ss=0;
		ll len=strlen(s);
		for (ll j=0; j<len; j++) {
			if (s[j]!=',' && nb<1){
				mash[i].name[j]=s[j];
			}	
			else if (s[j]==',') {
				if (nb==1) mash[i].p=ss;
				else if (nb==2) mash[i].c=ss;
				else if (nb==3) mash[i].t=ss;
				ss=0;
				nb++;
			}
			else ss=ss*10+s[j]-'0';	
		}
		mash[i].r=ss;
		mash[i].id=i;
	}

接下来对于x平方米的草坪,一台割草机所需的时间是t=x/c(c是割草的速度),那么需要充电的次数就是nb=t/ti(ti代表能工作的时间),那么充电时间就是:nb*recharge;
总的时间就是t+nb * recharge
当然需要注意的是,进行计算的时候用double,我本天真的以为int或long long 可以搞定,结果WA了好几发,要不是YD大佬在最后5分钟改成double的话就GG了。。。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<stack>
using namespace std;
typedef long long ll;
const int N = 100000 + 10;
const int MOD = 1e9 + 7;
const double EXP = 1E-8;
struct aaa {
    char name[70];
    int price;
    int rate;
    int work;
    int rest;
    int id;
} temp[155];
int cmp(aaa a, aaa b) {
    if(a.price == b.price)
        return a.id < b.id;
    return a.price < b.price;
}
const int INF = 1e9;
int l, m;
int w = INF;
char s[200];
int main() {
    const char *d = ",";
    char *p;
    scanf("%d %d", &l, &m);
    for(int i = 0; i <= m; ++i) {
        int j = 0;
        memset(s, '\0', sizeof(s));
        gets(s);
        p = strtok(s, d);    //strtok(s,d)将字符d前后的字符串分割开 
        temp[i].id = i;
        while(p) {
            if(j == 0) {
                strcpy(temp[i].name, p);
            } else if(j == 1) {
                temp[i].price = atoi(p);   //atoi(p)将字符串p里的数字字符转化为整形数。返回整形值。 
            } else if(j == 2) {
                temp[i].rate = atoi(p);
            } else if(j == 3) {
                temp[i].work = atoi(p);
            } else {
                temp[i].rest = atoi(p);
            }
            p = strtok(NULL, d);      //当没有标记d时则返回空字符NULL。
            ++j;
        }
    }
    sort(temp + 1, temp + m + 1, cmp);   //对费用进行排序。 
    for(int i = 1; i <= m; ++i) {
        if((double)((double)10080 / (double)(temp[i].work + temp[i].rest )) * (double)(temp[i].rate * temp[i].work) >= (double)l ) {
            if(w == INF || w == temp[i].price) {   //可能会有多种方案,按id大小输出 
                printf("%s\n", temp[i].name);
                w = temp[i].price;
            }
        }
    }
    if(w == INF) {
        printf("no such mower\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值