Volume 1. Big Number(uva)

时间:2023-03-08 22:19:52

如用到bign类参见大整数加减乘除模板

424 - Integer Inquiry

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define N 10050
using namespace std;
string s;
int ans[N];
int main()
{
int i, j;
while (cin>>s, s[] != '')
{
for (i = s.length() - , j = ; i >= ; i--, j++)
ans[j] += (s[i] - '');
}
for (i = ; i < N - ; i++)
{
ans[i + ] += ans[i] / ;
ans[i] %= ;
}
i = N - ;
while (!ans[i] && i > )
i--;
while (i >= )
cout<<ans[i--];
cout<<endl;
return ;
}

10106 - Product

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#define N 550
using namespace std;
int ans[N];
string s1, s2; int main()
{
int i, j, ls1, ls2, len;//tmp表示进位
while (cin>>s1>>s2)
{
ls1 = s1.length() - ;
ls2 = s2.length() - ;
len = ls1 + ls2;
memset(ans, , sizeof(ans));
for (i = ls1; i >= ; i--)
for (j = ls2; j >= ; j--)
{
ans[len - (i + j)] += (s1[i] - '') * (s2[j] - '');
}
len++;
for (i = ; i < len; i++)
{
ans[i + ] += ans[i] / ;
ans[i] = ans[i] % ;
}
while (!ans[len] && len > )//去除前导0
len--;
for (; len >= ; len--)
cout<<ans[len];
cout<<endl;
}
return ;
}

465 - Overflow

int main()
{
//ifstream cin("test.in");
bign a, b, c;
char ch;
bign d = INT_MAX;
string sa, sb;
while (cin>>sa>>ch>>sb)
{ cout<<sa<<' '<<ch<<' '<<sb<<endl;
a = sa;
b = sb;
if (d < a)
cout<<"first number too big"<<endl;
if (d < b)
cout<<"second number too big"<<endl;
if (ch == '+')
c = a + b;
else
c = a * b;
cout<<c<<endl;
if (d < c)
cout<<"result too big"<<endl;
}
return ;
}

748 - Exponentiation

int main()
{
string sa, sb;
bign a, b;
int p, n, i;
while (cin>>sa>>n)
{
p = ;
while (sa[p] != '.')
p++;
sa.erase(sa.begin() + p);
a = sa;
b = a ^ n;
sb = b.to_str();
p = ( - p) * n;
for (i = b.length(); i < p; i++)
sb += '';
sb.insert(sb.begin() + p, '.');
p = ;
while (sb[p] == '')
p++;
for (i = sb.length() - ; i >= p; i--)
cout<<sb[i];
cout<<endl;
}
return ;
}

10494 - If We Were a Child Again

int main()
{
//string sa, sb;
bign a, b, c;
char ch;
while (cin>>a>>ch>>b)
{
if (ch == '/')
c = a / b;
else
c = a % b;
cout<<c<<endl;
}
return ;
}