【ACM】HDU1008 Elevator 新手題前後不同的代碼版本

【前言】

    很久沒有純粹的寫寫小代碼,偶然想起要回爐再來,就去HDU隨便選了個最基礎的題,也不記得曾經AC過;最後喫驚的發現,思路完全不一樣了,代碼風格啥的也有不小的變化。希望是成長了一點點吧。後面定期去做做題、保持青春的活力!

1、青年回爐

Problem : 1008 ( Elevator )  

Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
9098607 2013-09-03 00:52:34 Accepted 1008 0MS 200K 881 B G++ Neo.Nengrong.Qu
864320 2008-10-31 14:14:02 Accepted 1008 0MS 0K 752 B C++ Neo.Nengrong.Qu

 

2、從代碼中發現成長的軌跡

2.1 曾經的代碼

Problem : 1008 ( Elevator )     Judge Status : Accepted
RunId : 864320    Language : C++    Author : Quner
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<iostream>
using namespace std;

int main(void)
{
    int a[100],i,j,n,flag,down;
    long total;
    while(cin>>n&&n)
    {
        a[0]=total=flag=down=0;
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
            if(!down)
            {
                if(a[i]>a[i-1])continue;
                total+=6*(a[i-1]-a[flag]);
                flag=i-1;down=1;
            }
            else
            {
                if(a[i]<a[i-1])continue;
                total+=4*(a[flag]-a[i-1]);
                flag=i-1;down=0;
            }
        }
        if(a[n]>a[n-1])total+=(a[n]-a[flag])*6;
        else    total+=(a[flag]-a[n])*4;
        total+=5*n;
        cout<<total<<endl;
    }
    return 0;
}

 

2.2 現在的風格

Problem : 1008 ( Elevator )     Judge Status : Accepted
RunId : 9098607    Language : G++    Author : Quner
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include <stdio.h>

int main(){
        int count=0;
        int cur_floor=0;
        int last_floor=0;
        int total_time=0;
        int i=0;
        scanf("%d", &count);
        while (count != 0){
                total_time = 0;
                last_floor = 0;
                for (i=0; i<count; i++){
                        scanf("%d", &cur_floor);
                        if (cur_floor > last_floor){
                                total_time += 6*(cur_floor-last_floor);
                        }
                        else{
                                total_time += 4*(last_floor-cur_floor);
                        }
                        total_time += 5;
                        last_floor = cur_floor;
                }
                printf("%d\n", total_time);
                scanf("%d", &count);
        }
        return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章