C语言实现竖条转横条:给定高度列表生成横向条形及位置方案求助
Alright, let's figure out how to build this C program. The key here is to shift our focus from vertical bars to horizontal layers—each horizontal bar we need to output represents a continuous stretch of vertical bars that are tall enough to reach a specific height. Let's break this down step by step.
1. 输入处理
First, we need to read the input list of numbers (like "1 5 3") and store them in an array. In C, you can do this easily with a loop using scanf, which will keep reading integers until it hits a non-numeric character (like a newline). We'll also keep track of how many numbers we've read (the length of the array).
2. 确定最大高度
Next, find the maximum height in the array. This tells us how many horizontal layers we need to process—we'll check every height from 1 up to this maximum value.
3. 遍历每一层高度
For each height h from 1 to the maximum height:
- Scan through the vertical bar heights to find all contiguous ranges where the vertical bar's height is at least
h. - To find these ranges:
- Start at the beginning of the array, skip over any vertical bars that are shorter than
h. - When you find a bar that's tall enough, mark its index as the start of the range.
- Keep moving forward until you hit a bar that's too short (or the end of the array)—this index is the end of the range.
- Output the range in the format
[start, end)@h. - Repeat until you've scanned the entire array for this height.
- Start at the beginning of the array, skip over any vertical bars that are shorter than
伪代码
// Step 1: Read input into an array Initialize an array to hold heights n = 0 while input has integers left: read integer into heights[n] n += 1 if n == 0: exit (no input to process) // Step 2: Find maximum height max_h = heights[0] for i from 1 to n-1: if heights[i] > max_h: max_h = heights[i] // Step 3: Process each height layer for h from 1 to max_h: i = 0 while i < n: // Skip bars too short for current height while i < n and heights[i] < h: i += 1 if i >= n: break // No more bars for this height start = i // Find end of the contiguous tall bars while i < n and heights[i] >= h: i += 1 // Output the formatted string print "[%d,%d)@%d" % (start, i, h)
C语言实现细节
- Input handling: You can use a fixed-size array (like
int heights[100];) for simplicity, or dynamically allocate memory withmallocif you need to handle arbitrary input sizes. Here's a quick snippet for reading input:int heights[100]; int n = 0; // Keep reading integers until scanf fails (e.g., newline is entered) while (scanf("%d", &heights[n]) == 1) { n++; } - Finding max height: A straightforward loop will do the trick—initialize
max_hto the first element, then iterate through the rest of the array to update it if you find a larger value. - Output formatting: Use
printfwith the format stringprintf("[%d,%d)@%d\n", start, i, h);to get the required output style. - Edge cases: Don't forget to handle empty input (n=0) by exiting early, or input with all zeros (though the problem example uses positive integers, it's good practice to account for this).
Let's test this logic with your example input "1 5 3":
- Max height is 5, so we process h=1 to 5.
- h=1: All 3 bars are tall enough → output
[0,3)@1 - h=2: Bars 1 and 2 are tall enough → output
[1,3)@2 - h=3: Bars 1 and 2 are tall enough → output
[1,3)@3 - h=4: Only bar 1 is tall enough → output
[1,2)@4 - h=5: Only bar 1 is tall enough → output
[1,2)@5
Perfect, that matches exactly what you need!
内容的提问来源于stack exchange,提问作者Danielvanheuven

