OJ求指导

C语言 码拜 8年前 (2016-06-05) 1138次浏览
杭电OJ1008,题目如下:
Problem Description
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.

Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.

Output
Print the total time on a single line for each test case.

Sample Input
1 2
3 2 3 1
0

Sample Output
17
41
问题代码如下:

#include <iostream>
using namespace std; 
int main(int argc, char *argv[]) {
	int temp=0;
	while(cin>>temp)
	{
		if(temp!=0)
		{
			int before=0,rear=0,sum=0;
			for(int i=0;i<temp;i++)
			{
				cin>>rear;
				if(before<rear)
				{
					sum+=(rear-before)*6+5;
				}
				else if(before>rear)
				{
					sum+=(before-rear)*4+5;
				}
				//sum+=5;
				before=rear;
			}
			cout<<sum<<endl;
		}
		else
			break;
	}

	return 0;
}

AC代码如下:

#include <iostream>
using namespace std; 
int main(int argc, char *argv[]) {
	int temp=0;
	while(cin>>temp)
	{
		if(temp!=0)
		{
			int before=0,rear=0,sum=0;
			for(int i=0;i<temp;i++)
			{
				cin>>rear;
				if(before<rear)
				{
					sum+=(rear-before)*6;
				}
				else if(before>rear)
				{
					sum+=(before-rear)*4;
				}
				sum+=5;
				before=rear;
			}
			cout<<sum<<endl;
		}
		else
			break;
	}

	return 0;
}

问一下里面求和放在外面和里面有什么区别,问题问的比较细,希望有心人给出回答。

解决方案

30

本人来解释一波,第一种情况,题主没有考虑,when elevator in the same level continuous
test case as  4  1 2 2 3,通不过
本人给你改了一下代码
题主,记得结贴给分,本人也是被迫的,有时候给别人调个程序,解答了他的问题,就没见了
我们辛辛苦苦回答问题,啥也没得到OJ求指导OJ求指导

#include <iostream>
using namespace std; 
 
int main(int argc, char *argv[]) {  //agrc 和 argv 可以不写,当你要用命令行参数的时候写
    int temp=0;
    while(cin>>temp)
    {
        if(temp!=0)
        {
            int before=0,rear=0,sum=0;
            for(int i=0;i<temp;i++)
            {
                cin>>rear;
                if(before<rear)
                {
                    sum=sum + (rear-before)*6+5;
                }
                else  //把if删了
                {
                    sum= sum + (before-rear)*4+5; // 把before = rear 的那种情况加进去了
                }
                //sum+=5;
                before=rear;
            }
            cout<<sum<<endl;
        }
        else
            break;
    }
     
    return 0;
}

10

代码功能归根结底不是别人帮本人看或讲解或注释出来的;而是被本人静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生本人领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明OJ求指导
喜欢 (0)
[1034331897@qq.com]
分享 (0)