A+B Problem _ C++(嘿嘿)

题目背景

 

作为所有编程语言的最“难”(一点也不难是假的)的一道题,曾近难倒了许多“大佬”,本次提供几种“简单”算法(嘿嘿);

题目描述

 

a+b problem;

输入输出格式

输入格式:

 

分两行输入a,b<=10^500;

 

输出格式:

 

输出只有一行,代表A+B的值

 

First.

         最“难”的方法,输入a,b,再相加输出(虽计算的范围不大,但"思维难度大"

#include 
#include 

using namespace std;

int main() {
    int a,b;
    cin >> a >> b;
    cout << a + b;
    return 0;
}

Second.

          来个Link-Cut Tree的a+b,这个相比上一个要简单的多(嘿嘿)

    作者Treeloveswater的方法:

#include
#include
#include
#include
using namespace std;
struct node 
{
    int data,rev,sum;
    node *son[2],*pre;
    bool judge();
    bool isroot();
    void pushdown();
    void update();
    void setson(node *child,int lr);
}lct[233];
int top,a,b;
node *getnew(int x)
{
    node *now=lct+ ++top;
    now->data=x;
    now->pre=now->son[1]=now->son[0]=lct;
    now->sum=0;
    now->rev=0;
    return now;
}
bool node::judge(){return pre->son[1]==this;}
bool node::isroot()
{
    if(pre==lct)return true;
    return !(pre->son[1]==this||pre->son[0]==this);
}
void node::pushdown()
{
    if(this==lct||!rev)return;
    swap(son[0],son[1]);
    son[0]->rev^=1;
    son[1]->rev^=1;
    rev=0;
}
void node::update(){sum=son[1]->sum+son[0]->sum+data;}
void node::setson(node *child,int lr)
{
    this->pushdown();
    child->pre=this;
    son[lr]=child;
    this->update();
}
void rotate(node *now)
{
    node *father=now->pre,*grandfa=father->pre;
    if(!father->isroot()) grandfa->pushdown();
    father->pushdown();now->pushdown();
    int lr=now->judge();
    father->setson(now->son[lr^1],lr);
    if(father->isroot()) now->pre=grandfa;
    else grandfa->setson(now,father->judge());
    now->setson(father,lr^1);
    father->update();now->update();
    if(grandfa!=lct) grandfa->update();
}
void splay(node *now)
{
    if(now->isroot())return;
    for(;!now->isroot();rotate(now))
    if(!now->pre->isroot())
    now->judge()==now->pre->judge()?rotate(now->pre):rotate(now);
}
node *access(node *now)
{
    node *last=lct;
    for(;now!=lct;last=now,now=now->pre)
    {
        splay(now);
        now->setson(last,1);
    }
    return last;
}
void changeroot(node *now)
{
    access(now)->rev^=1;
    splay(now);
}
void connect(node *x,node *y)
{
    changeroot(x);
    x->pre=y;
    access(x);
}
void cut(node *x,node *y)
{
    changeroot(x);
    access(y);
    splay(x);
    x->pushdown();
    x->son[1]=y->pre=lct;
    x->update();
}
int query(node *x,node *y)
{
    changeroot(x);
    node *now=access(y);
    return now->sum;
}
int main()
{
    scanf("%d%d",&a,&b);
    node *A=getnew(a);
    node *B=getnew(b);
    //连边 Link
        connect(A,B);
    //断边 Cut
        cut(A,B);
    //再连边orz Link again
        connect(A,B);
    printf("%d\n",query(A,B)); 
    return 0;
}
//作者-洛谷-Treeloveswater

Third.

        自己编了个高精的,这个没有那些大佬的"简单",,,

#include 
#include 
#define _MAX 100001
using namespace std;
typedef long long ll;
ll l1, l2;
string s1, s2;
ll a1[_MAX], a2[_MAX], h[_MAX];
int main() {
	cin >> s1 >> s2;
	if(s1.size() < s2.size())
		swap(s1, s2);
	l1 = s1.size();
	l2 = s2.size();
	for(int i = 0; i < l1; i++) {
		a1[i + 1] = s1[i] - '0';
		//cout << a1[i + 1];
	}
	//cout << endl;
	for(int i = l1 - l2; i < l1; i++) {
		a2[i + 1] = s2[i - l1 + l2] - '0';
	}
	/*
	for(int i = 1; i <= l1; i++) {
		cout << a2[i];
	}*/
	for(int i = l1; i >= 0; i--) {
		if(a1[i] + a2[i] >= 10) {
			h[i] = a1[i] + a2[i] - 10;
			a1[i - 1] += 1;
			if(i - 1 == 0)
				a1[0] = 1;
		} else {
			h[i] = a1[i] + a2[i];
		}
	}
	if(h[0] != 0)
		for(int i = 0; i <= l1; i++) {
			cout << h[i];
		}
	else
		for(int i = 1; i <= l1; i++) {
			cout << h[i];
		}
	return 0;
}

Next.

      看到doby编的SPFA,Floyd了,这个还不错,有点东西(嘿嘿)

//SPFA by doby
#include
using namespace std;
int n,m,a,b,op,head[200009],next[200009],dis[200009],len[200009],v[200009],l,r,team[200009],pd[100009],u,v1,e;
int lt(int x,int y,int z)
{
    op++,v[op]=y;
    next[op]=head[x],head[x]=op,len[op]=z;
}
int SPFA(int s,int f)//SPFA……
{
    for(int i=1;i<=200009;i++){dis[i]=999999999;}
    l=0,r=1,team[1]=s,pd[s]=1,dis[s]=0;
    while(l!=r)
    {
        l=(l+1)%90000,u=team[l],pd[u]=0,e=head[u];
        while(e!=0)
        {
            v1=v[e];
            if(dis[v1]>dis[u]+len[e])
            {
                dis[v1]=dis[u]+len[e];
                if(!pd[v1])
                {
                    r=(r+1)%90000,
                    team[r]=v1,
                    pd[v1]=1;
                }
            }
            e=next[e];
        } 
    }
    return dis[f];
}
int main()
{
    scanf("%d%d",&a,&b);
    lt(1,2,a);lt(2,3,b);//1到2为a,2到3为b,1到3即为a+b……
    printf("%d",SPFA(1,3));
    return 0;
}

 

// Floyd, by doby
#include
#include
using namespace std;
long long n=3,a,b,dis[4][4];
int main()
{
    cin>>a>>b;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dis[i][j]=2147483647;
        }
    }
    dis[1][2]=a,dis[2][3]=b;
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);//Floyd……
            }
        }
    }
    cout<

哎,学不来这些“简单”的(╮(╯▽╰)╭)

如果想了解更多算法去A+B Problem 题解,点击;

好的,OK

你可能感兴趣的:(日常)