1008. Elevator (20)

来源:互联网 发布:开源 数据上报 编辑:程序博客网 时间:2024/06/11 06:00

1008. Elevator (20)

目录

  • Elevator 20
    • 目录
    • 原题
    • 思路
    • 代码
    • 代码fread版速度略有提升

原题

1008. Elevator (20)链接

思路

1. 设置now=0,存储现在的楼层。go,存储将要去的楼层。time=0,存储需要运行的时间。count,存储序列元素个数。读入N,存入count。2.  循环读取数据count次,每次读入一个元素,存储于go。    1.  如果go大于等于now, time += (go - now) * 6 + 5。    2.  如果go小于now,time += (now - go) * 4 + 5。    3.  now = go。3. 输出time。  

代码

#include <iostream>#include <cstdio>int main(void) {    setvbuf(stdin, new char[1 << 20], _IOFBF, 1 << 20);    setvbuf(stdout, new char[1 << 20], _IOFBF, 1 << 20);    int now = 0, go, time = 0;    int count;    scanf("%d", &count);    for (int i = 0; i < count; i++) {        scanf("%d", &go);        if (go >= now) {            time += (go - now) * 6 + 5;        }        else {            time += (now - go) * 4 + 5;        }        now = go;    }    printf("%d\n", time);    return 0;}

代码(fread版)速度略有提升

#include <iostream>#include <cstdio>#include <cstdlib>int main(void) {    setvbuf(stdin, new char[1 << 20], _IOFBF, 1 << 20);    setvbuf(stdout, new char[1 << 20], _IOFBF, 1 << 20);    int now = 0, go, time = 0;    int count;    scanf("%d", &count);    char *array = new char[4 * count + 10];    fread(array, sizeof(char), 4 * count + 10, stdin);    char *endptr = array;    for (int i = 0; i < count; i++) {        go = strtol(endptr, &endptr, 10);        if (go >= now) {            time += (go - now) * 6 + 5;        }        else {            time += (now - go) * 4 + 5;        }        now = go;    }    printf("%d\n", time);    return 0;}
0 0
原创粉丝点击