Problem Solving report SOJ2714 Mountains (II)
Description
A Mountain consists of one or more hills, each of the which consists of upwards, which we denote with '/', and do Wnwards, which we denote with ' \ '. We call upwards and downwards together as wards. let/n is an upward of length n, and in the same-to, let \ n be a downward of length n. For example, the following upward IS/2:
//
And the following downward is \ 3:
\
The set of all hills are given in the following:
Hills =def {/n\n | n is a natural number}
Now it comes to the definition of a mountain (in BNF):
Mountain :: = Hill | Mountain Mountain
That's to say, a hill was a mountain, and if M and N were mountains, then the concatenation of m and N is also a mountain.
For example,
//\/\/ \
is a mountain with 3 hills.
We define the width of a mountain as the number of upwards and downwards. We define the height of a mountain as the maximum length of upwards(or equivalently downwards).
We can give each mountain a unique number, called index, such that the following conditions is met:
- index begins with 1
- The index of a mountain with larger width is larger
- If the mountains has the same width, then the index of the mountain whose leftmost different W ARD with the other mountain are an upward is larger
For example, portion of the mountains and their indices is:
/\ /\ /\ //\ /\/\ / \ /\/\/\ /\/ \ ... /\/\/ \ /\/ \/\ ... 1 2 3 4 5 9 10
In this problem, your is print the mountain from the given index.
Input
The input contains several lines, each of which which contains an integer N<2^32. The last line contains 0, which your should not proceed.
Output
For each N, your program should print the mountain, and your need make the mountain climbing.
The output of the consecutive instances should is separated by a blank line.
Sample Input
690
Sample Output
// \/ //\/\/
Author: fgjlwj
The main idea: Each mountain is made up of some hills, and each mountain has a ranking, if a mountain width is small or width as large but the dictionary small (that is, the hill width dictionary order), then its ranking is more forward. Ask you what the mountain range of the nth rank looks like?
Analysis: This problem is SOJ1678 upgrade version of the topic are the same only the output of this problem is slightly more complex, the principle of interest please refer to another blog post SOJ1678. then this problem output how to do, I am layered output, scan each layer, if a hill height enough, then output the corresponding level of characters (space-/-space-\-space), if not enough on the output num[j]*2 space.
then directly on the code bar, the need for the principle of the subject to see SOJ1678.
#include <iostream> #include <algorithm> #include <string> #include <cmath>using namespace std; int Num[50];long long Q_pow (long long A, long long B) {long long ans = 1; while (b) {if (b & 1) {ans *= A; } b/= 2; A *= A; } return ans; int main () {long long n; while (CIN >> n&&n) {int w = log2 (n) +1; Long Long Loc =n-(Q_pow (2, W-1)-1); int h = 1; int cnt = 1; int digit = 1; Long Long L = 1, R = Q_pow (2, w-1); while (L < R) {Long Long mid = (L + r)/2; if (Loc <= mid) {num[cnt++] = digit; h = max (h, digit); digit = 1; R = Mid; } else {digit++; L = mid + 1; }} num[cnt] = digit; h = max (h, digit); for (int i = h;I >=1; i--) {for (int j = 1; J <= CNT; j + +) {if (Num[j] >= i) { int width = (Num[j]-i+1) * 2; cout << String (num[j]-WIDTH/2, '); cout << "/" << string (width-2, ') << "\ \"; cout << String (num[j]-WIDTH/2, '); } else cout << string (2 * num[j], "); } cout << Endl; } cout << Endl; } return 0;}
See you Next illusion!
Problem Solving report SOJ2714 Mountains (II)