高精度比较
题目描述
现在给你一个简单的问题,给你两个正整数A和B,需要你帮忙判断它们的大小。
输入格式
输入两个正整数A和B
数据范围
A、B的位数不超过10000。
输出格式
若A>B 则输出 > ;
若A若A=B 则输出 = 。
样例输入
123456789 123456789
样例输出
=
样例输入
1234567 123456789
样例输出
<
样例输入
123456789 123456779
样例输出
如果要在 C++ 中实现高精度数的比较,你可以考虑以下几种方法:
使用标准库中的高精度数类型,例如 中的 std::string 类型。这样你就可以使用标准库中的运算符来比较两个高精度数的大小。例如:
#include <iostream>
#include <string>
int main() {
std::string a = "123456789";
std::string b = "123456789";
if (a == b) {
std::cout << "=" << std::endl;
} else if (a < b) {
std::cout << "<" << std::endl;
} else {
std::cout << ">" << std::endl;
}
return 0;
}
自己实现一个高精度数类型,并定义运算符重载。这样你就可以像使用标准库中的类型一样使用运算符来比较两个高精度数的大小。
例如,你可以定义一个 BigInt 类型,并实现运算符重载:
#include <iostream>
#include <string>
class BigInt {
public:
BigInt(const std::string& str) : value_(str) {}
bool operator==(const BigInt& other) const {
return value_ == other.value_;
}
bool operator<(const BigInt& other) const {
return value_ < other.value_;
}
private:
std::string value_;
};
int main() {
BigInt a("123456789");
BigInt b("123456789");
if (a == b) {
std::cout << "=" << std::endl;
} else if (a < b) {
std::cout << "<" << std::endl;
} else {
std::cout << ">" << std::endl;
}
return 0;
}
这种方法适用于你希望自己实现高精度数的运算,而不想使用标准库中的类型或自定义类型的情况。
例如,你可以定义一个比较两个高精度数的大小的函数:
#include <iostream>
#include <string>
int compare(const std::string& a, const std::string& b) {
if (a.size() < b.size()) {
return -1;
}
if (a.size() > b.size()) {
return 1;
}
return a.compare(b);
}
int main() {
std::string a = "123456789";
std::string b = "123456789";
int result = compare(a, b);
if (result == 0) {
std::cout << "=" << std::endl;
} else if (result < 0) {
std::cout << "<" << std::endl;
} else {
std::cout << ">" << std::endl;
}
return 0;
}
如果只是比较大小的话完全可以利用字符串进行比较,a b字符串长度相同时可直接比较大小
下面提供了c++和c两个
c++
#include<iostream>
#include<string>
using namespace std;
int main(){
string a, b;
cin >> a >> b;
if (a.size() == b.size()) {
if (a > b) {
cout << ">";
}
else if (a < b) {
cout << "<";
}
else {
cout << "=";
}
}
else if(a.size()<b.size()) {
cout << "<";
}
else {
cout << ">";
}
}
c
#include<stdio.h>
#include<string.h>
int main() {
char a[1002], b[1002];
scanf("%s%s", &a ,&b);
int la = strlen(a);
int lb = strlen(b);
if (la==lb) {
if (strcmp(a,b)>0) {
printf(">");
}
else if (strcmp(a,b)<0) {
printf("<");
}
else {
printf("=");
}
}
else if (la<lb) {
printf("<");
}
else {
printf(">");
}
}
前几位的方法是有一些错误的。
比如:
#include <iostream>
#include <string>
int main() {
std::string a = "12345678";
std::string b = "123456779";
if (a == b) {
std::cout << "=" << std::endl;
} else if (a < b) {
std::cout << "<" << std::endl;
} else {
std::cout << ">" << std::endl;
}
return 0;
}
应该输出<,但实际输出>。
正确做法:应该先比较位数,再从高位到低位依次比较。
下面的代码如果不出意外应该可以直接A掉这一题
#include<bits/stdc++.h>
using namespace std;
char A[100005]={},B[100005]={};
int main()
{
cin>>A>>B;
int n=strlen(A)-1,m=strlen(B)-1;
if(n>m)return printf(">")*0;
else if(n<m)return printf("<")*0;//比较位数
else
{
for(int i=0;i<=n;i++)
{
if(A[i]<B[i])return printf("<")*0;
else if(A[i]>B[i])return printf(">")*0;//从高到低位依次比较
}
return printf("=")*0;//相等
}
}