贪心1006
2016-03-25 17:26
204 查看
题目:
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Print the total time on a single line for each test case.
[align=left]Sample Input[/align]
1 2
3 2 3 1
0
[align=left]Sample Output[/align]
17
41
代码:#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n&&n<100)
{
int a
;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>=100) return 0;
}
int sum;
sum=a[0]*6+5;
for(int i=0;i<n-1;i++)
{
if(a[i+1]>a[i]) sum+=(a[i+1]-a[i])*6+5;
if(a[i+1]<a[i]) sum+=(a[i]-a[i+1])*4+5;
if(a[i+1]==a[i]) sum+=5;
}
printf("%d\n",sum);
}
return 0;
}
感想:这个题不难思路比较清晰
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Print the total time on a single line for each test case.
[align=left]Sample Input[/align]
1 2
3 2 3 1
0
[align=left]Sample Output[/align]
17
41
代码:#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n&&n<100)
{
int a
;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>=100) return 0;
}
int sum;
sum=a[0]*6+5;
for(int i=0;i<n-1;i++)
{
if(a[i+1]>a[i]) sum+=(a[i+1]-a[i])*6+5;
if(a[i+1]<a[i]) sum+=(a[i]-a[i+1])*4+5;
if(a[i+1]==a[i]) sum+=5;
}
printf("%d\n",sum);
}
return 0;
}
感想:这个题不难思路比较清晰
相关文章推荐
- hdu 2874(LCA + 节点间距离)
- Android4.42-Setting源码分析之蓝牙模块Bluetooth(下)
- JVM调优总结:一些概念
- PowerDesigner表创建脚本双引号问题
- 分析JAVA应用CPU占用过高的问题
- tomcat性能调优
- [C#]C++与C#数据类型总结
- (p226)最长单调递增子序列
- 【慕课笔记】2-2 数据类型与操作数据表—数据类型之整形
- 初探Java8中的HashMap(转)
- “单例”的属性访问器
- 对于EL表达式和ONGL表达式区别的相关理解
- shell中exit命令不退出脚本
- android蓝牙技术
- 使用SOAPUI测试WEBAPI接口
- Hibernate缓存问题
- java反射获取字段以及获取字段值和设置字段值
- cocos2dx之C++求360度定长向量的xy方向的分量
- 学习笔记——Python」Python中的类(classes)
- 删除链表中倒数第n个节点