zoukankan      html  css  js  c++  java
  • UVA 10303

    Problem D

    How Many Trees?

    Input: standard input

    Output: standard output

    Memory Limit: 32 MB

    A binary search tree is a binary tree with root k such that any node v in the left subtree of k has label (v) <label (k) and any node w in the right subtree of k has label (w) > label (k).

    When using binary search trees, one can easily look for a node with a given label x: After we compare x to the label of the root, either we found the node we seek or we know which subtree it is in. For most binary search trees the average time to find one of its n nodes in this way is O(log n).

    Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree?

    Input and Output

    The input will contain a number 1 <= i <= 1000 per line representing the number of elements of the set. You have to print a line in the output for each entry with the answer to the previous question.

    Sample Input

     
    1
    2
    3

     

    Sample Output

    1
    2
    5

    题意:给定n个结点,求有几种2叉搜索树。

    思路:分别取第n个点做根节点。如此图


    代码:

    #include <stdio.h>
    #include <string.h>
    #define max(a,b) (a)>(b)?(a):(b)
    #define min(a,b) (a)<(b)?(a):(b)
    const int N = 1005;
    const int MAXBIGN = 1005;
    
    
    struct bign {
        int s[MAXBIGN];
        int len;
        bign() {
    		len = 1;
    		memset(s, 0, sizeof(s));
        }
    	
        bign operator = (const char *number) {
    		len = strlen(number);
    		for (int i = 0; i < len; i++)
    			s[len - i - 1] = number[i] - '0';
    		return *this;
        }
        bign operator = (const int num) {
    		char number[N];
    		sprintf(number, "%d", num);
    		*this = number;
    		return *this;
        }
    	
        bign (int number) {*this = number;}
        bign (const char* number) {*this = number;}
    	
        bign operator + (const bign &c){  
    		bign sum;
    		int t = 0;
    		sum.len = max(this->len, c.len);
    		for (int i = 0; i < sum.len; i++) {
    			if (i < this->len) t += this->s[i];
    			if (i < c.len) t += c.s[i];
    			sum.s[i] = t % 10;
    			t /= 10;
    		}
    		
    		while (t) {
    			sum.s[sum.len++] = t % 10;
    			t /= 10;
    		}
    		
    		return sum;  
        }
    	
    	bign operator * (const bign &c){  
    		bign sum; bign zero;
    		if (*this == zero || c == zero)
    			return zero;
    		int i, j;
    		sum.len = this->len + c.len;
    		for (i = 0; i < this->len; i++) {
    			for (j = 0; j < c.len; j ++) {
    				sum.s[i + j] += this->s[i] * c.s[j];
    			}
    		}
    		for (i = 0; i < sum.len; i ++) {
    			sum.s[i + 1] += sum.s[i] / 10;
    			sum.s[i] %= 10;
    		}
    		sum.len ++;
    		while (!sum.s[sum.len - 1]) {
    			sum.len --;
    		}
    		return sum;  
        }	
    	bign operator * (const int &num) {
    		bign c = num;
    		return *this * c;
    	}
    	bign operator / (const int &num) {
    		bign ans; int k = 0;
    		ans.len = len;
    		for (int i = ans.len - 1; i >= 0; i --) {
    			ans.s[i] = (k * 10 + s[i]) / num;
    			k = (k * 10 + s[i]) % num;
    		}
    		while (!ans.s[ans.len - 1]) {
    			ans.len --;
    		}
    		return ans;
    	}
        bign operator - (const bign &c) {
    		bign ans;
    		ans.len = max(this->len, c.len);
    		int i;
    		
    		for (i = 0; i < c.len; i++) {
    			if (this->s[i] < c.s[i]) {
    				this->s[i] += 10;
    				this->s[i + 1]--;
    			}
    			ans.s[i] = this->s[i] - c.s[i];
    		}
    		
    		for (; i < this->len; i++) {
    			if (this->s[i] < 0) {
    				this->s[i] += 10;
    				this->s[i + 1]--;
    			}
    			ans.s[i] = this->s[i];
    		}
    		while (ans.s[ans.len - 1] == 0) {
    			ans.len--;
    		}
    		if (ans.len == 0) ans.len = 1;
    		return ans;
        }
    	
        void put() {
    		if (len == 1 && s[0] == 0) {
    			printf("0");
    		} else {
    			for (int i = len - 1; i >= 0; i--)
    				printf("%d", s[i]);
    		}
        }
    	
        bool operator < (const bign& b) const {
    		if (len != b.len)
    			return len < b.len;
    		
    		for (int i = len - 1; i >= 0; i--)
    			if (s[i] != b.s[i])
    				return s[i] < b.s[i];
    			return false;
        }
        bool operator > (const bign& b) const { return b < *this; }
        bool operator <= (const bign& b) const { return !(b < *this); }
        bool operator >= (const bign& b) const { return !(*this < b); }
        bool operator != (const bign& b) const { return b < *this || *this < b;}
        bool operator == (const bign& b) const { return !(b != *this); }
    };
    
    bign f[1005];
    int n; 
    
    void init() {
    	f[1] = 1;
    	for (int i = 2; i <= 1000; i ++) {
    		f[i] = f[i - 1] * (4 * i - 2) / (i + 1);
    	}
    }
    
    int main() {
    	init();
    	while (~scanf("%d", &n) && n) {
    		f[n].put();
    		printf("
    ");
    	}
    	return 0;
    }
    


  • 相关阅读:
    jQuery图片翻转弹出动画特效
    HTML5来了,7个混合式移动开发框架
    10款很好用的 jQuery 图片滚动插件
    JS图片自动和可控的轮播切换特效
    CSS3扇形动画菜单 鼠标滑过扇形展开动画
    css里面的几个你不知道的属性
    JS判断是否是微信打开页面
    js的escape()、encodeURI()、encodeURIComponent()区别详解
    使用HTML5的十大原因
    Hybrid App开发 四大主流移平台分析
  • 原文地址:https://www.cnblogs.com/pangblog/p/3424164.html
Copyright © 2011-2022 走看看