Fraction Comparision
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
Bobo has two fractions
xaxa and ybyb. He wants to compare them. Find the result.
输入描述:
The input consists of several test cases and is terminated by end-of-file.
Each test case contains four integers x, a, y, b.
* 0≤x,y≤10180≤x,y≤1018
* 1≤a,b≤1091≤a,b≤109
* There are at most 105105 test cases.
输出描述:
For each test case, print `=` if xa=ybxa=yb. Print `<` if xa<ybxa<yb. Print `>` otherwise.
示例1
输出
复制< > =
AC代码:
#include <iostream>
using namespace std;
int main()
{
long long x, a, y, b;
while(cin >> x >> a >> y >> b)
{
__int128 l = (__int128)x*b;
__int128 r = (__int128)y*a;
if(l>r) cout << ">" << endl;
else if(l<r) cout << "<" << endl;
else cout << "=" << endl;
}
return 0;
}
来源大佬博客:https://www.cnblogs.com/ECJTUACM-873284962/p/9198885.html
官方上GCC提供了两种128位整数类型,分别是__int128_t和__uint128_t,分别用于声明有符号整数变量和无符号整数变量。
这种大整数无法使用函数cout输出其值,需要自定义输出函数。
void myitoa(__int128_t v, char* s) { char temp; int i=0, j; while(v >0) { s[i++] = v % 10 + '0'; v /= 10; } s[i] = '\0'; j=0; i--; while(j < i) { temp = s[j]; s[j] = s[i]; s[i] = temp; j++; i--; } }
a+b大数读入模板
1 #include <bits/stdc++.h> 2 using namespace std; 3 inline __int128 read() 4 { 5 __int128 x=0,f=1; 6 char ch=getchar(); 7 while(ch<'0'||ch>'9') 8 { 9 if(ch=='-') 10 f=-1; 11 ch=getchar(); 12 } 13 while(ch>='0'&&ch<='9') 14 { 15 x=x*10+ch-'0'; 16 ch=getchar(); 17 } 18 return x*f; 19 } 20 21 inline void write(__int128 x) 22 { 23 if(x<0) 24 { 25 putchar('-'); 26 x=-x; 27 } 28 if(x>9) 29 write(x/10); 30 putchar(x%10+'0'