【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; }

 

你可能感兴趣的:(ACM)