1
已解决
薛乘志
初级启示者
初级启示者
众所不知,C++(64位编译器)中还有一个数据类型叫__int128,可以存下128二进制位的超长整数,但是cout/cin/scanf/printf均不支持输入输出
现在noilinux2.0使用64位编译器,也没有禁止使用__int128,所以我封装了一个类方便使用(现在大部分stl标准库都可以使用int128了)
namespace std {
class int128 {
private:
__int128 data;
void read(__int128 &n) {
__int128 x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
n = x * f;
}
void print(__int128 n) {
if (n < 0) {
putchar('-');
n *= -1;
}
if (n > 9) print(n / 10);
putchar(n % 10 + '0');
}
public:
int128() {
data = 0;
}
int128(__int128 n) {
data = n;
}
int128(const int128 &n) {
data = n.data;
}
int128(string n) {
int l = n.size();
data = 0;
for (int i = 0; i < l; i++) {
if (n[i] != '-') {
data *= 10;
data += n[i] - '0';
}
}
if (n[0] == '-') {
data *= -1;
}
}
int128(const char n[]) {
int l = strlen(n);
data = 0;
for (int i = 0; i < l; i++) {
if (n[i] != '-') {
data *= 10;
data += n[i] - '0';
}
}
if (n[0] == '-') {
data *= -1;
}
}
template<typename T> int128(T n) {
data = n;
}
const int128 operator=(__int128 n) {
data = n;
return *this;
}
const int128 operator=(int128 n) {
data = n.data;
return *this;
}
const int128 operator=(string n) {
int l = n.size();
data = 0;
for (int i = 0; i < l; i++) {
if (n[i] != '-') {
data *= 10;
data += n[i] - '0';
}
}
if (n[0] == '-') {
data *= -1;
}
return *this;
}
const int128 operator=(const char n[]) {
int l = strlen(n);
data = 0;
for (int i = 0; i < l; i++) {
if (n[i] != '-') {
data *= 10;
data += n[i] - '0';
}
}
if (n[0] == '-') {
data *= -1;
}
return *this;
}
template<typename T> const int128 operator=(T n) {
data = n;
return *this;
}
const int128 operator+(int128 n) {
int128 tmp;
tmp.data = data + n.data;
return tmp;
}
const int128 operator-(int128 n) {
int128 tmp;
tmp.data = data - n.data;
return tmp;
}
const int128 operator*(int128 n) {
int128 tmp;
tmp.data = data * n.data;
return tmp;
}
const int128 operator/(int128 n) {
int128 tmp;
tmp.data = data / n.data;
return tmp;
}
template<typename T> const int128 operator+(T n) {
int128 tmp;
tmp.data = data + n;
return tmp;
}
template<typename T> const int128 operator-(T n) {
int128 tmp;
tmp.data = data - n;
return tmp;
}
template<typename T> const int128 operator*(T n) {
int128 tmp;
tmp.data = data * n;
return tmp;
}
template<typename T> const int128 operator/(T n) {
int128 tmp;
tmp.data = data / n;
return tmp;
}
const bool operator==(int128 n) {
return data == n;
}
const bool operator!=(int128 n) {
return data != n;
}
const bool operator>(int128 n) {
return data > n;
}
const bool operator<(int128 n) {
return data < n;
}
const bool operator>=(int128 n) {
return data >= n;
}
const bool operator<=(int128 n) {
return data <= n;
}
operator __int128() const {
return data;
}
friend istream &operator>>(istream &in, int128 &n) {
n.read(n.data);
return in;
}
friend ostream &operator<<(ostream &out, int128 n) {
__int128 tmp = n.data;
n.print(tmp);
return out;
}
};
}
使用方式:
using namespace std;
int main() {
int128 a[5] = {5, 4, 3, 2, 1};
cin >> a[0];
sort(a, a + 5);
cout << a[0];
return 0;
}
完整代码:
#include <bits/stdc++.h>
namespace std {
class int128 {
private:
__int128 data;
void read(__int128 &n) {
__int128 x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
n = x * f;
}
void print(__int128 n) {
if (n < 0) {
putchar('-');
n *= -1;
}
if (n > 9) print(n / 10);
putchar(n % 10 + '0');
}
public:
int128() {
data = 0;
}
int128(__int128 n) {
data = n;
}
int128(const int128 &n) {
data = n.data;
}
int128(string n) {
int l = n.size();
data = 0;
for (int i = 0; i < l; i++) {
if (n[i] != '-') {
data *= 10;
data += n[i] - '0';
}
}
if (n[0] == '-') {
data *= -1;
}
}
int128(const char n[]) {
int l = strlen(n);
data = 0;
for (int i = 0; i < l; i++) {
if (n[i] != '-') {
data *= 10;
data += n[i] - '0';
}
}
if (n[0] == '-') {
data *= -1;
}
}
template<typename T> int128(T n) {
data = n;
}
const int128 operator=(__int128 n) {
data = n;
return *this;
}
const int128 operator=(int128 n) {
data = n.data;
return *this;
}
const int128 operator=(string n) {
int l = n.size();
data = 0;
for (int i = 0; i < l; i++) {
if (n[i] != '-') {
data *= 10;
data += n[i] - '0';
}
}
if (n[0] == '-') {
data *= -1;
}
return *this;
}
const int128 operator=(const char n[]) {
int l = strlen(n);
data = 0;
for (int i = 0; i < l; i++) {
if (n[i] != '-') {
data *= 10;
data += n[i] - '0';
}
}
if (n[0] == '-') {
data *= -1;
}
return *this;
}
template<typename T> const int128 operator=(T n) {
data = n;
return *this;
}
const int128 operator+(int128 n) {
int128 tmp;
tmp.data = data + n.data;
return tmp;
}
const int128 operator-(int128 n) {
int128 tmp;
tmp.data = data - n.data;
return tmp;
}
const int128 operator*(int128 n) {
int128 tmp;
tmp.data = data * n.data;
return tmp;
}
const int128 operator/(int128 n) {
int128 tmp;
tmp.data = data / n.data;
return tmp;
}
template<typename T> const int128 operator+(T n) {
int128 tmp;
tmp.data = data + n;
return tmp;
}
template<typename T> const int128 operator-(T n) {
int128 tmp;
tmp.data = data - n;
return tmp;
}
template<typename T> const int128 operator*(T n) {
int128 tmp;
tmp.data = data * n;
return tmp;
}
template<typename T> const int128 operator/(T n) {
int128 tmp;
tmp.data = data / n;
return tmp;
}
const bool operator==(int128 n) {
return data == n;
}
const bool operator!=(int128 n) {
return data != n;
}
const bool operator>(int128 n) {
return data > n;
}
const bool operator<(int128 n) {
return data < n;
}
const bool operator>=(int128 n) {
return data >= n;
}
const bool operator<=(int128 n) {
return data <= n;
}
operator __int128() const {
return data;
}
friend istream &operator>>(istream &in, int128 &n) {
n.read(n.data);
return in;
}
friend ostream &operator<<(ostream &out, int128 n) {
__int128 tmp = n.data;
n.print(tmp);
return out;
}
};
}
using namespace std;
int main() {
int128 a[5] = {5, 4, 3, 2, 1};
cin >> a[0];
sort(a, a + 5);
cout << a[0];
return 0;
}
薛乘志在2022-02-27 15:41:02追加了内容
当然使用方式不仅仅是这一种,int/long long可以的操作它大部分都可以
薛乘志在2022-02-28 19:51:56追加了内容
由于这个代码有一些问题,现在结帖,修复后重新发帖