解题报告 之 SOJ2714 Mountains(II)
来源:互联网 发布:可以听pdf的软件 编辑:程序博客网 时间:2024/05/22 12:52
解题报告 之 SOJ2714 Mountains(II)
Description
A mountain consists of one or more hills, each of which consists of upwards, which we denote with `/', and downwards, which we denote with '\'. We call upwards and downwards together as wards. Let /n be an upward of length n, and in the same way, 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 is 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 is a mountain, and if M and N are 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 are met:
- index begins with 1
- the index of a mountain with larger width is larger
- if two mountains have the same width, then the index of the mountain whose leftmost different ward with the other mountain is an upward is larger
For example, portion of the mountains and their indices are:
/\ /\ /\ /\/\ /\/\ / \ /\/\/\ /\/ \ ... /\/\/ \ /\/ \/\ ...1 2 3 4 5 9 10
In this problem, you are print the mountain from the given index.
Input
The input contains several lines, each of which contains an integer N<2^32. The last line contains 0, which you should not proceed.
Output
For each N, your program should print the mountain, and you need make the mountain climbing.
The output of two consecutive instances should be separated by a blank line.
Sample Input
690
Sample Output
/\/ \/\ /\/\/\/ \
Author: fgjlwj
#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!
- 解题报告 之 SOJ2714 Mountains(II)
- 解题报告 之 SOJ1678 Mountains
- 机器人II解题报告
- 弹球II 解题报告
- Backpack II 解题报告
- leetCode解题报告之SingleNumberI,II(知识点:位运算)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- 【LeetCode】Subsets II 解题报告
- 【LeetCode】Permutations II 解题报告
- [LeetCode] Permutations II 解题报告
- [LeetCode] Subsets II 解题报告
- Subset II [Leetcode 解题报告]
- Permutations II [Leetcode 解题报告]
- Word Search II解题报告
- Word Search II 解题报告
- 解题报告之DisjointSet
- 从 Yii 1.1升级到 Yii2
- android常见的事件
- Bitwise AND of Numbers Range
- 以按键事件为例阐述消息传递机制
- hdu4407 Sum(容斥原理)
- 解题报告 之 SOJ2714 Mountains(II)
- 死锁
- C# + Xamarin 开发应用-- Call API时遇到错误: Error: NameResolutionFailure
- java 死锁及避免死锁
- Android实现自定义AlertDialog的自下向上的动画效果(并消除dialog边框)
- Test
- 自制操作系统-最简单的系统hello World
- data-type,data-value
- LeetCode 208 - Implement Trie (Prefix Tree)