HDOJ——简单题2(1008,1012)

1008:Elevator

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

题目的大概意思是:那幢楼只有一架电梯,一个请求列表由N个正整数组成。这些数字暗示电梯会在哪里停,并且它们按照特定的顺序。

这花费6s使电梯上一层楼,并且4s使电梯下一层楼。电梯在每一层都会停5s。

对于每组样例,算出你要花多少时间去完成列表上的所有要求。电梯一开始在0层并且不需要返回最下面当所有的要求被完成后。

对于这道题,其他没什么,就按照它说的算,并且按照样例算一遍判断是否正确就可以了。

但是这样做的话还是WA。

要考虑一种实际情况,就是当你在同一楼层摁的次数超过1次时,每次要加5s,这里要进行特判。

#include<stdio.h>
#include<string.h>
int main()
{
    int n,i,a[101];
    __int64 sum;
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        if(n==0) break;
        memset(a,0,sizeof(a));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]>a[i-1]) sum+=6*(a[i]-a[i-1])+5;
            else if(a[i]<a[i-1]) sum+=4*(a[i-1]-a[i])+5;  //题目看仔细,看它什么时候是+5的;就只是碰到那些数字时才要加5 
            else sum+=5;        //坑爹了,输入相同的楼层也要算时间哇 
        }
        printf("%I64d\n",sum);
    }
}


1012 :

u Calculate e

这道题,实际上就是让你改变输出格式的问题,还有就是求阶乘的问题。

#include<stdio.h>
#include<math.h>
int main()
{
    int i,j;
    double e,m;
    for(i=0;i<=9;i++)
    {
         m=1;
        if(i==0) e=1.0;
        else {
            for(j=1;j<=i;j++)
                m=m*j;
            e+=1.0/m;
        }
        if(i==0){printf("n e\n"); printf("- -----------\n");}    
        if(i==0||i==1) printf("%d %.0lf\n",i,e);
        else if(i==2) printf("%d %.1lf\n",i,e);
        else printf("%d %.9lf\n",i,e);
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值