[英]translating vertical bars to horizontal bars in c programming
我想制作一個程序,在其中發送一個指示條形高度的數字列表。 例如,“1 5 3”表示長度為 1 的條,然后是高度為 5 的條,然后是 3。我希望將其轉換為橫向條及其位置。 “1 5 3”將是長度為 3 的條形,然后是長度為 2 的條形、另一個 2、一個 1 和另一個 1。這將表示為“[0,3)@1”,其中 1 是高度, 0是開始,3是結束。 其他 4 個是“[1,3)@2”、“[1,3)@3”、“[1,2)@4”和“[1,2)@5”。
有人對如何做到這一點有一個粗略的想法嗎? 像偽代碼這樣的想法已經很棒了,所以我知道從哪里開始。
偽代碼:
Read inputs, producing: W, the number of inputs. H, the maximum input. Heights, an array with the height of each bar, starting with index 0. For y from 0 to H-1: Set CurrentState to Empty. For x from 0 to W-1: If CurrentState is Empty: If y < Heights[x]: Set CurrentState to Occupied. Print "[", x, and ",". Else: If Heights[x] <= y: Set CurrentState to Empty. Print x, ")@", y, and end-of-line or other desired delimiter.
基本思想是掃描每一行,然后:在條形開始的地方打印起始坐標。 在條形結束的地方,打印結束坐標以及當前的垂直位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.