某城市最高的楼只有一部电梯。该电梯和一般电梯不同的是它依照输入楼层数的先后次序运行。电梯最初在0层。运行完一个输入序列后就停止在该楼层,不返回0层。编写程序计算电梯运行一个序列的时间。每次都假设电梯在0层开始,无论上一次运行到几层。电梯每上1层需要6秒。每下1层需要4秒。如在某层停留,无论上下人多少,均停留5秒。
输入:楼层的值大于等于1,小于100 ,N=0表示结束输入。
输出:计算每个序列电梯运行的时间。
输入:?
2 1 0
输出:
26
题目分析:
1.先读入输入的字符串,也就是电梯的楼层数,
2.将读入字符串进行分割,分割字符用空格“ ”就可以了
3.用for循环进行遍历字符串数组,计算时间,当循环到0层时,就退出循环
4.输出计算的用时
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { int preLevel = 0; int Seconds = 0; string str = Console.ReadLine(); string[] strarr = str.Split(' '); for(int i=0;i< strarr.Length; i++) { int Level = Convert.ToInt32(strarr[i]); if (Level == 0) break; int LevNum = Level - preLevel; if (LevNum != 0) Seconds += 5; if (LevNum > 0) Seconds += 6 * Math.Abs(LevNum); else if(LevNum < 0) Seconds += 4 * Math.Abs(LevNum); preLevel = Convert.ToInt32(strarr[i]); } Console.WriteLine(Seconds); Console.ReadKey(); } } } |