为什么不对!!!

P1001 A+B Problem

Kevin626 @ 2024-05-19 09:43:39

本菜鸡不会任何算法,只会打暴力,有没有大佬帮忙看看,除了负数没考虑到还有啥别的问题

#include<bits/stdc++.h>
using namespace std;

int n=10;
string a,b;
string operator+(string d,string p) {
    int sa=d.length(),sb=p.length();
    int a[505],b[505];
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(int i=0; i<sa; i++)
        if(d[sa-1-i]<='9'&&d[sa-1-i]>='0')a[i]=d[sa-1-i]-'0';
        else a[i]=d[sa-1-i]-87;
    for(int i=0; i<sb; i++)
        if(p[sb-1-i]<='9'&&p[sb-1-i]>='0')b[i]=p[sb-1-i]-'0';
        else b[i]=p[sb-1-i]-87;
    int len=max(sa,sb);
    int ans[505];
    memset(ans,0,sizeof(ans));
    for(int i=0; i<len; i++) {
        ans[i]+=a[i]+b[i];
        if(ans[i]>=n) {
            ans[i]-=n;
            ans[i+1]++;
        }
    }
    string turn="";
    if(ans[len]==0)len--;
    for(int i=len; i>=0; i--) {
        char ch=char(ans[i]+'0');
        if(ch<='9'&&ch>='0')turn+=char(ch);
        else turn+=char(ch-'0'+87);
    }
    return turn;
}
int len(string xx){
    int ret=0;
    for(int i=1;xx[i]!='\0';i++){
        ret++;
    }
    return ret;
}
bool operator>(string d,string p){
    if(len(d)!=len(p))return len(d)<len(p);
    else return d<p;
}
string s_turn(int k){
    string anss="";
    while(k>0){
        anss+=k%10+'0';
        k/=10;
    }
    return anss;
}
int ss_turn(string k){
    int anss=0;
    for(int i=1;i<=len(k);i++){
        anss=anss*10+(k[i]-'0');
    }
    return anss;
}
string suan_a(int num,int ret){        //递归a 
    if(s_turn(ret+num)>a){
        ret+=num-(num-(ss_turn(a)-ret));
        return s_turn(ret);
    }
    ret+=num;
    return suan_a(num+1,ret); 
}
string suan_b(int num,int ret){        //递归b 
    if(s_turn(ret+num)>b){
        ret+=num=(num-(ss_turn(b)-ret));
        return s_turn(ret);
    }
    ret+=num;
    return suan_b(num+1,ret); 
}

int main() {
    cin>>a;
    string ta=a;
//  getchar();
    cin>>b;
    string tb=b;
    string sum="0";
    for(int i=1; i<=2; i++) {
        switch(i) {
            case 1: {
                sum=sum+suan_a(1,ss_turn(sum));
            }
            case 2: {
                sum=sum+suan_b(1,ss_turn(sum));
            }
        }
    }
    cout<<sum;
    return 0;
}

//a+b

by Sizeof_cpp @ 2024-05-19 09:48:28


#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}

# ```直接AC

by Hacker_Cracker @ 2024-05-19 09:49:42

或许您想要:P1601 A+B Problem


by liuerer @ 2024-05-19 09:59:45

@Kevin626 负数?


by __My0217__ @ 2024-05-19 10:27:23

|a|, |b| <= 10^9


by dingxuanrui120531 @ 2024-05-26 22:45:07

其实P1001的用c++直接输出a+b就行了 P1601才要高精度


by dingxuanrui120531 @ 2024-05-26 22:45:50

不过我看到很多人都是用很高级的语言和做法


by ATION001 @ 2024-06-06 23:24:11

对这题没必要写高精度吧

code:

#include<bits/stdc++.h>
using namespace std;
int a,b;
int main(){
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

by lizeyuhello @ 2024-06-09 20:26:01

@Kevin626 随手写个树状数组就能过。

``c++

include<bits/stdc++.h>

using namespace std;

int n = 2; int tree[10];

int lowbit(int x) { return x & -x; }

void add(int x, int k) { while (x <= n) { tree[x] += k; x += lowbit(x); } return; }

int sum(int x) { int s = 0; while (x != 0) { s += tree[x]; x -= lowbit(x); } return s; }

int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int a, b; cin >> a >> b; add(1, a), add(2, b); cout << sum(2) << endl; return 0; }


by lizeyuhello @ 2024-06-09 20:26:55

艹,少打的一个点。

#include<bits/stdc++.h>
using namespace std;

int n = 2; 
int tree[10];

int lowbit(int x)
{
    return x & -x;
}

void add(int x, int k)
{
    while (x <= n)
    {
        tree[x] += k;
        x += lowbit(x);
    }
    return;
}

int sum(int x)
{
    int s = 0;
    while (x != 0)
    {
        s += tree[x];
        x -= lowbit(x);
    }
    return s;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int a, b;
    cin >> a >> b;
    add(1, a), add(2, b);
    cout << sum(2) << endl;
    return 0;
}

by iamsharkbee @ 2024-06-16 09:17:36

@Sizeof_cpp long long int???


|