• 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;
    }
    


  • 相关阅读:
    解决Data truncation: Truncated incorrect DOUBLE value: 'xxxxxxx';
    免费LOGO在线生成+免费图床 (亲测可用)
    解决问题:bat脚本手动执行没问题,自动执行不成功
    flask上线部署————“WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.”
    钉钉机器人自动推送测试报告
    Python 自动化测试报告通过钉钉机器人推送通知
    转载:如何将新建的项目完整的提交到gitlab上?
    GIT推送时,空文件夹无法添加
    ddt+excel接口自动化测试-HtmlTestRunner报告中用例名称显示excel中自定义的名称
    Exp9 Web安全基础 20154301仉鑫烨
  • 原文地址:https://www.cnblogs.com/pangblog/p/3424164.html
Copyright © 2020-2023  润新知