uva 424 高精度加法。
代码1:
#include <stdio.h>
#include <string.h>
const int Maxn = 110;
char num[Maxn];
int sum[Maxn];
void add()
{
int len = strlen(num);
for (int i = 0; i < len; i++)
{
sum[i] += num[len - i - 1] - '0';
if (sum[i] > 9)
{
sum[i] = sum[i] - 10;
sum[i + 1] += 1;
}
}
}
int main()
{
while (scanf("%s", num) != EOF)
{
getchar();
if (num[0] == '0')
{
int i = Maxn;
while (!sum[i])
i--;
for (; i >= 0; i--)
printf("%d", sum[i]);
printf("\n");
break;
}
else
{
add();
}
}
return 0;
}
代码2:
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int Maxn = 1000;
struct BigInt
{
int len, s[Maxn];
//无参构造函数
BigInt()
{
memset(s, 0, sizeof(s));
len = 1;
}
//参数为数字类型的构造函数
BigInt(int num)
{
*this = num;
}
//参数为字符数组类型的构造函数
BigInt(const char* num)
{
*this = num;
}
//重载运算符'='用于赋值字符数组
BigInt operator = (const char* num)
{
len = strlen(num);
for (int i = 0; i < len; i++)
s[i] = num[len - i - 1] - '0';
return *this;
}
//重载运算符'='用于赋值数字
BigInt operator = (int num)
{
char s[Maxn];
sprintf(s, "%d", num);
*this = s;
return *this;
}
//将字符数组转化成字符串
string str() const
{
string res = "";
for (int i = 0; i < len; i++)
{
res = (char)(s[i] + '0') + res;
}
if (res == "")
{
res = "0";
}
return res;
}
//'+' 运算符重载
BigInt operator + (const BigInt& b) const
{
BigInt c;
c.len = 0;
for (int i = 0, g = 0; g || i < max(len, b.len); i++)
{
int x = g;
if (i < len)
x += s[i];
if (i < b.len)
x += b.s[i];
c.s[c.len++] = x % 10;
g = x / 10;
}
return c;
}
BigInt operator * (const BigInt& b) const
{
}
//'+='运算符重载
BigInt operator += (const BigInt& b)
{
*this = *this + b;
return *this;
}
//'<'运算符重载
bool operator < (const BigInt& 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 BigInt& b) const
{
return b < *this;
}
//'<='运算符重载
bool operator <= (const BigInt& b) const
{
return !(b < *this);
}
//'>='运算符重载
bool operator >= (const BigInt& b) const
{
return !(b > *this);
}
//'!='运算符重载
bool operator != (const BigInt& b) const
{
return b < *this || b > *this;
}
//'=='运算符重载
bool operator == (const BigInt& b) const
{
return !(b < *this) && !(b > *this);
}
};
istream& operator >> (istream& in, BigInt& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const BigInt& x)
{
out << x.str();
return out;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
BigInt num = 0;
BigInt ans = 0;
while (cin >> num)
{
if (num == 0)
break;
ans += num;
}
cout << ans << endl;
return 0;
}
uva 10106 高精度乘法。
代码1:
#include <stdio.h>
#include <string.h>
const int Maxn = 251;
char A[Maxn], B[Maxn];
int a[Maxn], b[Maxn];
int c[Maxn * Maxn];
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
while (scanf("%s%s", A, B) != EOF)
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
int lena = strlen(A);
int lenb = strlen(B);
for (int i = 0; i < lena; i++)
{
a[i] = A[lena - i - 1] - '0';
}
for (int i = 0; i < lenb; i++)
{
b[i] = B[lenb - i - 1] - '0';
}
for (int i = 0; i < lena; i++)
{
for (int j = 0; j < lenb; j++)
{
c[i + j] = a[i] * b[j] + c[i + j];
}
}
int jinwei = 0;
int lenc = lena + lenb;
for (int i = 0; i < lenc; i++)
{
int t = c[i] + jinwei;
c[i] = t % 10;
jinwei = t / 10;
}
while (c[lenc] == 0 && lenc > 0)//debug
lenc--;
for (int i = lenc; i >= 0; i--)
printf("%d", c[i]);
printf("\n");
}
return 0;
}
代码2:
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int Maxn = 1000;
struct BigInt
{
int len, s[Maxn];
//无参构造函数
BigInt()
{
memset(s, 0, sizeof(s));
len = 1;
}
//参数为数字类型的构造函数
BigInt(int num)
{
*this = num;
}
//参数为字符数组类型的构造函数
BigInt(const char* num)
{
*this = num;
}
//重载运算符'='用于赋值字符数组
BigInt operator = (const char* num)
{
len = strlen(num);
for (int i = 0; i < len; i++)
s[i] = num[len - 1 - i] - '0';
return *this;
}
//重载运算符'='用于赋值数字
BigInt operator = (int num)
{
char s[Maxn];
sprintf(s, "%d", num);
*this = s;
return *this;
}
//将字符数组转化成字符串
string str() const
{
string res = "";
for (int i = 0; i < len; i++)
{
res = (char)(s[i] + '0') + res;
}
if (res == "")
{
res = "0";
}
return res;
}
//'+' 运算符重载
BigInt operator + (const BigInt& b) const
{
BigInt c;
c.len = 0;
for (int i = 0, g = 0; g || i < max(len, b.len); i++)
{
int x = g;
if (i < len)
x += s[i];
if (i < b.len)
x += b.s[i];
c.s[c.len++] = x % 10;
g = x / 10;
}
return c;
}
//'*' 运算符重载
BigInt operator * (const BigInt& b) const
{
BigInt c;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < b.len; j++)
{
c.s[i + j] = s[i] * b.s[j] + c.s[i + j];
//printf("----%d-----", c.s[i + j]);
}
}
c.len = len + b.len;
int jinwei = 0;
for (int i = 0; i < c.len; i++)
{
int t = c.s[i] + jinwei;
jinwei = t / 10;
c.s[i] = t % 10;
}
while (c.s[c.len - 1] == 0 && c.len > 1)
c.len--;
return c;
}
//'+='运算符重载
BigInt operator += (const BigInt& b)
{
*this = *this + b;
return *this;
}
//'<'运算符重载
bool operator < (const BigInt& 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 BigInt& b) const
{
return b < *this;
}
//'<='运算符重载
bool operator <= (const BigInt& b) const
{
return !(b < *this);
}
//'>='运算符重载
bool operator >= (const BigInt& b) const
{
return !(b > *this);
}
//'!='运算符重载
bool operator != (const BigInt& b) const
{
return b < *this || b > *this;
}
//'=='运算符重载
bool operator == (const BigInt& b) const
{
return !(b < *this) && !(b > *this);
}
};
istream& operator >> (istream& in, BigInt& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const BigInt& x)
{
out << x.str();
return out;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
BigInt num1 = 0;
BigInt num2 = 0;
BigInt ans = 1;
while (cin >> num1)
{
cin >> num2;
ans = num2 * num1;
cout << ans << endl;
}
return 0;
}