POJ3982 序列
来源:互联网 发布:联想网络销售授权店 编辑:程序博客网 时间:2024/05/23 12:00
问题链接:POJ3982 序列。
问题简述:参见上述链接。
问题分析:这只是一个大整数数列的问题,有一个大整数类就解决了。数列类似于斐波拉契数列,只是稍微复杂一些。
程序说明:这里使用自己的功能简洁的大整数类。这个类是参考其他人的程序改造的。
AC的C++语言程序如下:
/* POJ3982 序列 */#include <cstdio>#include <iostream>#include <string>#include <sstream>using namespace std;/* 无符号整数类,整数放在字符串中,可以用整数初始化。 * 只有以下功能: * 1.数组 * 2.加运算。 */class UBigInt {private: string num;public: UBigInt(); UBigInt(int n); void setNumber(string s); const string& getNumber(); // retrieves the number UBigInt operator + (UBigInt b);private: string add(string number1, string number2); UBigInt& operator [] (int n);};UBigInt::UBigInt() { // empty constructor initializes zero num = "0";}UBigInt::UBigInt(int n) { stringstream ss; string s; ss << n; ss >> s; setNumber(s);}void UBigInt::setNumber(string s) { num = s;}const string& UBigInt::getNumber() { // retrieves the number return num;}UBigInt UBigInt::operator + (UBigInt b) { UBigInt addition; addition.setNumber( add(getNumber(), b.getNumber() ) ); return addition;}string UBigInt::add(string number1, string number2) { string add = (number1.length() > number2.length()) ? number1 : number2; int diffLength = abs( (int) (number1.size() - number2.size()) ); if(number1.size() > number2.size()) number2.insert(0, diffLength, '0'); // put zeros from left else// if(number1.size() < number2.size()) number1.insert(0, diffLength, '0'); char carry = 0; for(int i=number1.size()-1; i>=0; --i) { add[i] = (carry+(number1[i]-'0')+(number2[i]-'0')) + '0'; if(i != 0) { if(add[i] > '9') { add[i] -= 10; carry = 1; } else carry = 0; } } if(add[0] > '9') { add[0]-= 10; add.insert(0,1,'1'); } return add;}UBigInt& UBigInt::operator [] (int n) { return *(this + (n*sizeof(UBigInt)));}int main(){ UBigInt t[99+1]; int a, b, c; while(scanf("%d%d%d", &a, &b, &c) != EOF) { t[0] = a; t[1] = b; t[2] = c; for(int i=3; i<=99; i++) t[i] = t[i-3] + t[i-2] + t[i-1]; cout << t[99].getNumber() << endl; } return 0;}
1 0
- POJ3982:序列
- POJ3982 序列
- poj3982——序列
- POJ3982《序列》方法:高精度
- poj3982 序列(java大数)
- [POJ3982]序列[字符串][大整数]
- 北大ACM poj3982
- 大数模板 poj3982
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- 序列
- SparkStreaming---wordCount源码解读
- log4cxx
- jquery zclip 复制插件的使用
- c 字符串分割函数
- method to The entitlements specified in your application’s Code Signing Entitlements file do not mat
- POJ3982 序列
- Spark定制班第17课:Spark Streaming资源动态申请和动态控制消费速率原理剖析
- 更改python编码
- 字典序顺序加减 c++
- 排序
- Android中GridView使用
- CORDIC计算余弦的误差与旋转次数的关系
- 一起talk C栗子吧(第一百五十七回:C语言实例--基于AF_UNIX域的数据报套接字通信)
- Android Manifest 权限描述大全