#include
#include
#include
#include
#include
#include
using namespace std;
class bigInteger
{
private:
string number; //用string类来存放大整数
public:
bigInteger(string s=“0”){ number = s; } //带默认值的构造函数
void setNumber(string s){ number = s; }
string getNumber(){return number;}
friend istream& operator>>(istream& input, bigInteger& bigInt); //输入运算符重载,友元函数
friend ostream& operator<<(ostream& output, bigInteger& bigInt); //输出运算符重载,友元函数
bool operator!=(bigInteger bigInt); // "!=“运算符的重载
void operator+=(bigInteger bigInt); //”+=“运算符的重载
bigInteger operator+(bigInteger bigInt); //”+“运算符的重载
bigInteger operator*(int n); //”*"运算符的重载
};
istream& operator>>(istream&input, bigInteger& bigInt) //输入运算符重载,友元函数
{
input >> bigInt.number;
return input;
}
ostream& operator<<(ostream& output, bigInteger& bigInt) //输出运算符重载,友元函数
{
output << bigInt.number;
return output;
}
bool bigInteger::operator!=(bigInteger bigInt) //"!="运算符的重载
{
if (number != bigInt.number)
return true;
return false;
}
void bigInteger::operator+=(bigInteger bigInt) //"+="运算符的重载
{
reverse(number.begin(), number.end());
reverse(bigInt.number.begin(), bigInt.number.end()); //将两个大整数倒置,计算时从低位到高位(number[0],number[1])计算
int c = 0, f = 0, sum0; //c表示位置,f表示进位符初值为0,sum0表示每一次相加的和
while (number[c] != ‘\0’ && bigInt.number[c] != ‘\0’)
{
sum0 = (number[c]-‘0’) + (bigInt.number[c]-‘0’)+f;
number[c] = sum0 % 10+‘0’;
if (sum0 <= 9) //如果前一位的和大于10,下一位的进位符置1,否则为零
f = 0;
else
f = 1;
c++;
}
if (number[c] == ‘\0’) //如果第一个长整数较短,将第二个加到第一个上
{
while (bigInt.number[c] != ‘\0’)
{
sum0 = (bigInt.number[c] - ‘0’) + f; //此时仍要考虑进位符
number+= sum0 % 10 + ‘0’;
if (sum0 <= 9)
f = 0;
else
f = 1;
c++;
}
}
else
{
while (number[c] != ‘\0’) //如果第一个长整数较长,考虑进位符直至结束
{
sum0 = (number[c] - ‘0’)+f;
number[c] = sum0 % 10 + ‘0’;
if (sum0 <= 9)
f = 0;
else
f = 1;
c++;
}
}
if (f == 1) //当两个数相加完成后,如果进位符为1,增加一位
{
number += ‘1’;
}
reverse(number.begin(), number.end()); //倒置长整数
}
bigInteger bigInteger::operator+(bigInteger bigInt) //"+“运算符的重载 相加过程类似”+="的重载
{
string sum; //两个长整数相加的和存入sum中,最后返回长整数对象时用
reverse(number.begin(), number.end());
reverse(bigInt.number.begin(), bigInt.number.end());
int c = 0, f = 0, sum0;
while (number[c] != ‘\0’ && bigInt.number[c] != ‘\0’)
{
sum0 = (number[c] - ‘0’) + (bigInt.number[c] - ‘0’) + f;
sum += sum0 % 10 + ‘0’;
if (sum0 <= 9)
f = 0;
else
f = 1;
c++;
}
if (number[c] == ‘\0’)
{
while (bigInt.number[c] != ‘\0’)
{
sum0 = (bigInt.number[c] - ‘0’) + f;
sum += sum0 % 10 + ‘0’;
if (sum0 <= 9)
f = 0;
else
f = 1;
c++;
}
}
else
{
while (number[c] != ‘\0’)
{
sum0 = (number[c] - ‘0’) + f;
sum += sum0 % 10 + ‘0’;
if (sum0 <= 9)
f = 0;
else
f = 1;
c++;
}
}
if (f == 1)
{
sum += ‘1’;
}
reverse(number.begin(), number.end()); //将原来两个长整数对象倒置,还原为原来的值
reverse(bigInt.number.begin(), bigInt.number.end());
reverse(sum.begin(), sum.end());
return bigInteger(sum); //返回相加和的对象
}
bigInteger bigInteger::operator*(int n) //"*"运算符的重载
{
bigInteger sum;
string sum0;
int count = 0; //表示n的第几位
reverse(number.begin(), number.end());
int m, c = 0, f = 0, num; //m用来表示n分解的每一位数
while (n)
{
c = 0;
f = 0;
m = n % 10;
n = n / 10; //n从低位到高位取
sum0 = “”;
for (int i = 0; i < count; i++)
sum0 += ‘0’;
sum0 += number; //在倒置的number前加0,相当于未倒置前乘10,100…
count++; //n每除一次10,count加一
while (sum0[c] != ‘\0’)
{
num = (sum0[c] - ‘0’)*m + f;
sum0[c] = num % 10 + ‘0’;
f = num / 10; //因为是相乘,标志位范围为0~9
c++;
}
if (f)
sum0 += f+‘0’; //如果标志位不为零,进位。
reverse(sum0.begin(), sum0.end()); //反转之后为这一位相乘后的结果
bigInteger temp(sum0);
sum += temp; //加到sum中去
}
reverse(number.begin(), number.end()); //最后将用到对象的number再倒置
return sum; //返回相乘的结果
}
int main() //uva424主函数
{
bigInteger sum(“0”);
bigInteger end(“0”);
bigInteger b;
while (cin >> b && b != end) //">>","!=“运算符重载
{
sum += b; //”+=“运算符的重载
}
cout << sum << endl; //”<<"运算符的重载
return 0;
}
int main() //uva495主函数
{
int n;
bigInteger fib[5003]; //用来存放斐波那契数列的大整数对象数组
fib[0].setNumber(“0”);
fib[1].setNumber(“1”);
for (int i = 2; i < 5002; i++)
{
fib[i] = fib[i - 1] + fib[i - 2]; //"+"运算符的重载
}
while (cin >> n)
{
cout << "The Fibonacci number for " << n << " is " << fib[n] << endl;;
}
return 0;
}
int main() //uva485主函数
{
int flag=0;
cout << “1” << endl << “1 1” << endl;
bigInteger a[300][300]; //建立一个二维数组最多输出到205行(用vector效率会更高)
a[1][0].setNumber(“1”);
a[1][1].setNumber(“1”);
for(int i=2;flag==0;i++)
{
a[i][0].setNumber(“1”);
for(int j=1;j {
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
a[i][i].setNumber(“1”);
for(int j=0;j {
cout << a[i][j] << " ";
if(a[i][j].getNumber().size()>60)
flag=1; //如果长度大于60,输完这一行停止输出
}
cout << a[i][i] << endl;
}
return 0;
}
int main() //uva324主函数
{
int n;
string s0;
int count[10];
bigInteger fac[370]; //数组用来存每一个数的阶乘
fac[1].setNumber(“1”);
for (int i = 2; i < 369; i++)
fac[i] = fac[i - 1] * i; //把1-368每一个数的阶乘计算好
while (cin >> n && n) //while循环为读入输入和调整输出格式
{
memset(count, 0, sizeof(count));
s0 = fac[n].getNumber();
int c=0;
while (s0[c] != ‘\0’)
count[s0[c++] - ‘0’]++;
cout << n << “! --” << endl << " ";
for (int i = 0; i <= 9; i++)
{
cout << “(” << i << “)” << " ";
if (i != 4 && i != 9)
cout << count[i] << " ";
else if (i == 4)
cout << count[i] << endl << " ";
else
cout << count[i] << endl;
}
}
return 0;
}