helgt @ 2024-04-14 15:00:57
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int mx=4e3+10;
struct bign{
int l,s[mx];//
bign(){ l=1, memset(s,0,sizeof(s)); }//自动初始化
bign(const int x){ *this=x; }
bign operator =(const int x){//高精度=整数除
char str[20];
sprintf(str,"%d",x);
return *this=str;
}
bign operator =(const char *num){//高精度=字符串
memset(s,0,sizeof(s));
l=strlen(num);
for(int i=1; i<=l; i++) s[i]=num[l-i]-'0';
return *this;
}
bign operator +(bign b){//高精度+高精度
bign c;
int len=l>b.l?l:b.l;
for(int i=1; i<=len; i++){
c.s[i]+=s[i]+b.s[i];
c.s[i+1]+=c.s[i]/10;
c.s[i]%=10;
}
c.l=len+1;///?????
c.clean();
return c;
}
bign operator - (bign b){
bign c;
for(int i=1; i<=l; i++){
if(s[i]<b.s[i]) s[i]+=10, s[i+1]--;
c.s[i]=s[i]-b.s[i];
}
c.l=l;
c.clean();
return c;
}
bign operator *(bign b){
bign c;
for(int i=1; i<=l; i++){
for(int j=1; j<=b.l; j++)
c.s[i+j-1]+=s[i]*b.s[j],c.s[i+j]+=c.s[i+j-1]/10,c.s[i+j-1]%=10;
}
c.l=l+b.l+1;
c.clean();
return c;
}
bool operator >(bign b){
if(l!=b.l) return l>b.l;
for(int i=l; i>=1; i--){
if(s[i]!=b.s[i]) return s[i]>b.s[i];
}
return false;
}
bool operator <(bign b){
if(l!=b.l) return l<b.l;
for(int i=l; i>=1; i--){
if(s[i]!=b.s[i]) return s[i]<b.s[i];
}
return false;
}
bool operator ==(bign b){
return !(*this>b) && !(*this<b);
}
bool operator >=(bign b){
return (*this>b) || (*this==b);
}
void clean(){//清除最高位0
while(l>1&&s[l]==0) l--;
}
};
istream& operator >>(istream&in, bign &x){
string str;
in>>str;
x=str.c_str();
return in;
}
ostream& operator <<(ostream&out,const bign x){
for(int i=x.l; i>=1; i--) out<<x.s[i];
return out;
}
int main(){
bign a,b;
cin>>a>>b;
cout<<(a==b);
return 0;
}
by M3te0rDream @ 2024-04-14 15:18:40
belike:
#include <iostream>
#include <string>
#include <algorithm>
// 辅助函数,将字符串表示的大整数转换为数组表示
void stringToNum(const std::string& str, int num[]) {
int len = str.length();
for (int i = 0; i < len; ++i) {
num[i] = str[len - i - 1] - '0';
}
}
// 辅助函数,将数组表示的大整数转换为字符串表示
std::string numToString(int num[], int len) {
std::string str;
for (int i = len - 1; i >= 0; --i) {
str += (char)(num[i] + '0');
}
return str;
}
// 大整数加法函数
std::string add(const std::string& num1, const std::string& num2) {
// 计算两个数字的长度
int len1 = num1.length();
int len2 = num2.length();
// 确保第一个数字比第二个数字长,如果不是,则交换它们
if (len1 < len2) {
return add(num2, num1);
}
// 将字符串表示的大整数转换为数组表示,并且翻转数组,使得低位在前,高位在后
int arr1[len1], arr2[len1];
stringToNum(num1, arr1);
stringToNum(num2, arr2);
// 对齐两个数组,使得它们的长度相同
for (int i = len2; i < len1; ++i) {
arr2[i] = 0;
}
// 执行手工加法,从低位到高位逐位相加,进位则向高位传递
int carry = 0;
int sum[len1 + 1]; // 结果数组的长度可能比原数组多一位,用于存储最高位的进位
for (int i = 0; i < len1; ++i) {
sum[i] = arr1[i] + arr2[i] + carry;
carry = sum[i] / 10;
sum[i] %= 10;
}
sum[len1] = carry; // 存储最高位的进位
// 将结果数组转换为字符串表示
std::string result = numToString(sum, len1 + 1);
// 去除结果字符串前面的0(如果有的话)
result.erase(0, std::min(result.find_first_not_of('0'), result.size() - 1));
return result;
}
int main() {
std::string num1, num2;
std::cin >> num1 >> num2;
std::cout<<add(num1, num2);
return 0;
}
错在哪
by alexdai @ 2024-04-14 19:00:22
@helgt
忘记处理负数的情况了
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int mx=4e3+10;
struct bign{
int op,l,s[mx];//
//记得加符号
bign(){ l=1, memset(s,0,sizeof(s)); }//自动初始化
bign(const int x){ *this=x; }
bign operator =(const int x){//高精度=整数除
char str[20];
sprintf(str,"%d",x);
return *this=str;
}
bign operator =(const char *num){//高精度=字符串
memset(s,0,sizeof(s));
l=strlen(num);
for(int i=1; i<=l; i++) s[i]=num[l-i]-'0';
return *this;
}
bign operator +(bign b){//高精度+高精度
bign c;
int len=l>b.l?l:b.l;
for(int i=1; i<=len; i++){
c.s[i]+=s[i]+b.s[i];
c.s[i+1]+=c.s[i]/10;
c.s[i]%=10;
}
c.l=len+1;///?????
c.clean();
return c;
}
bign operator - (bign b){
bign c;
for(int i=1; i<=l; i++){
if(s[i]<b.s[i]) s[i]+=10, s[i+1]--;
c.s[i]=s[i]-b.s[i];
}
c.l=l;
c.clean();
return c;
}
bign operator *(bign b){
bign c;
for(int i=1; i<=l; i++){
for(int j=1; j<=b.l; j++)
c.s[i+j-1]+=s[i]*b.s[j],c.s[i+j]+=c.s[i+j-1]/10,c.s[i+j-1]%=10;
}
c.l=l+b.l+1;
c.clean();
return c;
}
bool operator >(bign b){
if(l!=b.l) return l>b.l;
for(int i=l; i>=1; i--){
if(s[i]!=b.s[i]) return s[i]>b.s[i];
}
return false;
}
bool operator <(bign b){
if(l!=b.l) return l<b.l;
for(int i=l; i>=1; i--){
if(s[i]!=b.s[i]) return s[i]<b.s[i];
}
return false;
}
bool operator ==(bign b){
return !(*this>b) && !(*this<b);
}
bool operator >=(bign b){
return (*this>b) || (*this==b);
}
void clean(){//清除最高位0
while(l>1&&s[l]==0) l--;
}
};
istream& operator >>(istream&in, bign &x){
char str[100];
in>>str;
if(str[0]=='-')x.op=-1,x=str+1;
else x.op=1,x=str;
return in;
}
ostream& operator <<(ostream&out,const bign x){
if(x.op==-1)out<<"-";
for(int i=x.l; i>=1; i--) out<<x.s[i];
return out;
}
int main(){
bign a,b;
cin>>a>>b;
bign ans;
if(a.op==b.op){
ans=a+b;
//cout<<a<<','<<b<<endl;
//cout<<ans<<endl;
ans.op=a.op;
}else{
int flag=0;
if(a.op==-1)swap(a,b);
if(a>=b)flag=1;
else flag=-1,swap(a,b);
ans=a-b;
ans.op=flag;
}
cout<<ans;
return 0;
}
by ZHB666include @ 2024-04-18 21:01:32
@helgt 不是这道题不是高精度的啊,直接:
long long a,b;
cin>>a>>b;
cout<<a+b;
它不香吗?
by Allen1234zzz @ 2024-04-20 17:32:56
man! what can i say!
直接搞个:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}
不行吗?当然也不是不行
by GeorgeCHN @ 2024-04-25 15:16:53
等下。为什么你们都把这么简单的题做成高精了?
(求关注!我先给你关注)
by lukanqi @ 2024-04-25 19:27:47
这道题有负数,高精麻烦 建议直接```
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}
by Lucashuang @ 2024-11-03 13:21:46
#include<bits/stdc++.h>
using namespace std;
int m , n , a[200001] , l1 , s , r1 , maxx = 0 , ans;
priority_queue<int> que;
int fen(int l , int r){
if(l == r) return a[l];
m = (l + r) / 2;
s = a[m] , l1 = a[m] , r1 = a[m + 1];
for(int i = m - 1; i >= l; i--){
s += a[i];
l1 = max(l1 , s);
}
int s1 = a[m + 1];
for(int i = m + 2; i <= r; i++){
s1 += a[i];
r1 = max(r1 , s1);
}
return max(max(fen(l , m) , fen(m + 1 , r)) , l1 + r1);
}
bool check(int k){
for(int i = 1; i <= n; i++){
que.push(a[i]);
}
while(!que.empty()){
k -= que.top();
que.pop();
}
if(k){
return true;
} else {
return false;
}
}
int f(int n){
if(n == 1) return a[1];
return a[n] + f(n - 1);
}
int main(){
int n = 2 , ans;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
ans = fen(1 , n);
if(check(ans)){
if(f(n) == ans){
cout << ans;
} else {
cout << "Wrong!";
}
} else {
cout << "Wrong!";
}
return 0;
}