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

来源:互联网 发布:健身大数据 编辑:程序博客网 时间:2024/05/17 01:56

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