问题链接: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; }