标签:
题意:给两个大整数,判断哪个更大。大整数以”AB”形式给出,”A”是一个不含前导0的整数(大于0,不超过1e9),”B”是若干个(可能为空)阶乘符号(“!”)。比如:3!!=6!=720
解法:设两个大整数形式A部分分别为a,b;B部分分别有n1,n2个符号。假设n1 > n2,那么我们只需判断aA 和 b的大小即可,其中A为(n1 - n2)个阶乘符号。n1 = n2 或者 n1 < n2时候类似。但要注意有坑,当a为0或b为0时,如果可以将a或b转化为1,就转换。否则可能会有坑,比如1!!!!! = 0!!
My Code
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
class ExtraordinarilyLarge{
public:
string compare(string x, string y){
string ans1 = "x<y", ans2 = "x=y", ans3 = "x>y";
int n1 = 0;
while(x.back() == ‘!‘) n1++, x.pop_back();
int n2 = 0;
while(y.back() == ‘!‘) n2++, y.pop_back();
ll a = stol(x), b = stol(y);
if(a == 0 && n1 > 0) a = 1, n1--;
if(b == 0 && n2 > 0) b = 1, n2--;
if(n1 == n2)
{
if(a < b) return ans1;
else if(a == b) return ans2;
else return ans3;
}
else if(n1 > n2)
{
ll c = 1;
for(int i = 0; i < n1 - n2; i++)
{
c = 1;
for(int j = 1; j <= a; j++)
{
c = c * j;
if(c > b) return ans3;
}
a = c;
}
if(a < b) return ans1;
else if(a == b) return ans2;
else return ans3;
}
else//n1 < n2
{
ll c = 1;
for(int i = 0; i < n2 - n1; i++)
{
c = 1;
for(int j = 1; j <= b; j++)
{
c = c * j;
if(c > a) return ans1;
}
b = c;
}
if(a < b) return ans1;
else if(a == b) return ans2;
else return ans3;
}
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/uestc_peterpan/article/details/47711565