poj 2084 Game of Connections
2011-07-22 19:12
253 查看
#include <iostream> //高精度 组合数学,Catalan数,公式:An=A(n-1)*(4n-2)/(n+1) #include <string> #include<algorithm> using namespace std; int compare(string str1, string str2) { while(str1[0]=='0') { str1.erase(0,1); } while(str2[0]=='0') { str2.erase(0,1); } if(str1.size() > str2.size()) //长度长的整数大于长度小的整数 return 1; else if(str1.size() < str2.size()) return -1; else return str1.compare(str2); //若长度相等,从头到尾按位比较,compare函数:相等返回0,大于返回1,小于返回-1 } int to_int(char ch) { return ch-48; } char to_char(int i) { return (char)(i+48); } void dipose_head(string& str,int sign) //str为引用类型 { str.erase(0, str.find_first_not_of('0')); //去除结果中的前导0 if(str.empty()) str = "0"; if((sign == -1) && (str[0] != '0')) //处理符号位 ,若str[0] == '0'说明结果是0,就不必要在前面加上- str = '-' + str; } //高精度加法 string ADD_INT(string str1, string str2) //计算str1 + str2的值,处理成 str1 ,str2 >= 0 { string MINUS_INT(string str1, string str2); //要事先声明MINUS_INT string str; if(str1[0] == '-') { if(str2[0] == '-') { str = ADD_INT(str1.erase(0, 1), str2.erase(0, 1)); if(str[0] != '0') str = "-" + str; } else str = MINUS_INT(str2, str1.erase(0, 1)); } else { if(str2[0] == '-') str = MINUS_INT(str1, str2.erase(0, 1)); else //str1 ,str2 >= 0的情况 { string::size_type l1, l2; int i; l1 = str1.size(); l2 = str2.size(); if(l1 < l2) //把两个整数对齐,短整数前面加0补齐 for(i = 1; i <= l2 - l1; i++) str1 = '0' + str1; else for(i = 1; i <= l1 - l2; i++) str2 = '0' + str2; int int1 = 0, int2 = 0; //int2 记录进位 for(i = str1.size() - 1; i >= 0; i--) { int1 = ( str1[i]+str2[i] - 96 + int2 ) % 10; int2 = ( str1[i]+str2[i] - 96 + int2 ) / 10; str = to_char(int1) + str; } if(int2 != 0) str = to_char(int2) + str; //处理最后的进位 } } return str; } //高精度减法 string MINUS_INT(string str1, string str2) //计算str1 - str2的值,先处理成 str1 ,str2 >= 0,再处理成 str1 > str2 >= 0 { int sign = 1; //sign 为符号位 string str; if(str2[0] == '-') { if(str1[0]!='-') str = ADD_INT(str1, str2.erase(0, 1)); else str=MINUS_INT(str2.erase(0,1),str1.erase(0,1)); } else { if(str1[0]=='-') { str = ADD_INT(str1.erase(0, 1), str2); if(str[0] != '0') str = "-" + str; } else //str1 ,str2 >= 0的情况 { int res = compare(str1, str2); if(res == 0) return "0"; //二者相等 if(res < 0) { sign = -1; str1.swap(str2); } string::size_type temp_int; temp_int = str1.size() - str2.size(); for(int i = str2.size() - 1; i >= 0; i--) { if(str1[i + temp_int] < str2[i]) { str1[i + temp_int - 1] = str1[i + temp_int - 1] - 1; // '5'='6'-1 str = to_char(str1[i + temp_int] - str2[i] + 10) + str; } else str =to_char(str1[i + temp_int] - str2[i]) + str; } str = str1.substr(0,temp_int) + str; //str1[i] 从i=0 到 i=temp_int - 1 dipose_head(str,sign); } } return str; } //高精度乘法 string MULTIPLY_INT(string str1, string str2) { int sign = 1; //sign 为符号位 if(str1[0] == '-') { sign *= -1; str1 = str1.erase(0, 1); } if(str2[0] == '-') { sign *= -1; str2 = str2.erase(0, 1); } if(str1=="0"||str2=="0") return "0"; int i, j; string str; string::size_type len1, len2; len1 = str1.size(); len2 = str2.size(); for(i = len2 - 1; i >= 0; i --) //实现手工乘法 { string temp_str; int int1 = 0, int2 = 0, int3 = to_int(str2[i]); if(int3 != 0) { for(j = 1; j <= len2-1 - i; j++) temp_str = '0' + temp_str; for(j = len1 - 1; j >= 0; j--) { int1 = (int3 * to_int(str1[j]) + int2) % 10; int2 = (int3 * to_int(str1[j]) + int2) / 10; temp_str = to_char(int1) + temp_str; } if(int2 != 0) temp_str = to_char(int2) + temp_str; } str = ADD_INT(str, temp_str); } if((sign == -1) && (str[0] != '0')) str = '-' + str; return str; } //高精度除法 string DIVIDE_INT(string str1, string str2, int flag) //flag = 1时,返回商; flag = 0时,返回余数 { string quotient, residue; //定义商和余数,sign1,sign2判断商和余数的符号 int sign1 = 1, sign2 = 1; if(str2 == "0") //判断除数是否为0 return "ERROR!"; if(str1 == "0") //判断被除数是否为0 return "0"; if(str1[0] == '-') { str1 = str1.erase(0, 1); sign1 *= -1; sign2 = -1; //余数的符号由被除数的符号决定 } if(str2[0] == '-') { str2 = str2.erase(0, 1); sign1 *= -1; } int res = compare(str1, str2); if(res < 0) { quotient = "0"; residue = str1; } else if(res == 0) { quotient = "1"; residue = "0"; } else //str1>str2 { string::size_type len1, len2; len1 = str1.size(); len2 = str2.size(); string temp_str; temp_str.append(str1, 0, len2 - 1); //添加从str1[0]到str1[len2 - 2] for(int i = len2 - 1; i < len1; i++) //模拟手工除法 { temp_str = temp_str + str1[i]; for(char ch = '9'; ch >= '0'; ch --) //试商 { string str; str = str + ch; if(compare(MULTIPLY_INT(str2, str), temp_str) <= 0) //在compare增添的两个while()语句在这里发挥作用,倘若缺失,比如计算10/1,第二次的temp_str=00,按照compare函数的定义,00就会比1大了 { quotient = quotient + ch; temp_str = MINUS_INT(temp_str, MULTIPLY_INT(str2, str)); break; } } } residue = temp_str; //residue就不用判断前面是否带0,因为temp_str = MINUS_INT(...),MINUS_INT把0处理了 if(quotient[0]=='0') quotient.erase(0,1); //商前面最多只有一个0 } if((sign1 == -1) && (quotient!="0")) quotient = "-" + quotient; if((sign2 == -1) && (residue != "0")) residue = "-" + residue; if(flag == 1) return quotient; else return residue; } string to_str(int n) { string str; while(n) { str.push_back(n%10+48); n=n/10; } reverse(str.begin(),str.end()); return str; } int main() { string num[105]={"1"}; for(int i=1;i<=100;++i) { num[i]=DIVIDE_INT(MULTIPLY_INT(num[i-1],to_str(4*i-2)),to_str(i+1),1); } int n; while(cin>>n&&n!=-1) cout<<num <<endl; return 0; }
相关文章推荐
- POJ 2084 Game of Connections 笔记
- POJ 2084:Game of Connections 大数+卡特兰数
- POJ-2084-Game of Connections
- POJ 2084 Game of Connections
- POJ2084—Game of Connections(c++高精度)
- POJ2084 Game of Connections(catalan数)
- POJ 2084 Game of Connections 卡特兰数+JAVA -
- poj 2084 Game of Connections
- 卡特兰数:poj 2084 Game of Connections+hdu 1023
- [POJ] 2084 -> Game of Connections
- POJ2084——Game of Connections
- poj 2084 Game of Connections
- POJ 2084 Game of Connections
- POJ 2084 Game of Connections
- POJ 2084 Game of Connections(JAVA练习)
- poj2084 Game of Connections
- poj2084 Game of Connections(Catalan)
- POJ2084 Game of Connections(数学,dp)
- NYOJ---164 &&HDOJ1134&&POJ2084 --> Game of Connections
- POJ_2084_Game of Connections