bzoj3767 A+B Problem加强版

Description

Input

输入A,B

Output

输出A+B。

Sample Input

1 1

Sample Output

2

HINT

对于100%的数据,保证 |A| , |B| 不会超过10^(10^7)

呃……高精a+b和a-b的模板题

没啥技术含量的……我还以为会有什么--0之类的出现

#include<cstdio>
#include<cstring>
#define N 10000010
int a[N],b[N],c[N],l1,l2,l3;
char ch1[N],ch2[N];
bool m1,m2;
inline int max(int a,int b){return a>b?a:b;}
int main()
{
    scanf("%s",ch1+1);l1=strlen(ch1+1);
    scanf("%s",ch2+1);l2=strlen(ch2+1);
    if (ch1[1]==‘-‘){l1--;m1=1;}
    if (ch2[1]==‘-‘){l2--;m2=1;}
    for (int i=1;i<=l1;i++)a[i]=ch1[l1-i+1+m1]-‘0‘;
    while (!a[l1]&&l1>1)l1--;
    if (l1==1&&!a[1])m1=0;
    for (int i=1;i<=l2;i++)b[i]=ch2[l2-i+1+m2]-‘0‘;
    while (!b[l2]&&l2>1)l2--;
    if (l2==1&&!b[1])m2=0;
    l3=max(l1,l2);
    if (m1^m2)
    {
        if (m1)
        {
            for (int i=1;i<=l3;i++)
            {
                int t=a[i];
                a[i]=b[i];
                b[i]=t;
            }
            int t=l1;l1=l2;l2=t;
        }
        bool mrk=0;
        if (l2>l1)mrk=1;
        else if (l1==l2)
        {
            for (int i=l1;i>=1;i--)
                if (a[i]<b[i]){mrk=1;break;}
                else if (a[i]>b[i])break;
        }
        if (mrk)
        {
            printf("-");
            for (int i=1;i<=l3;i++)
            {
                int t=a[i];
                a[i]=b[i];
                b[i]=t;
            }
            int t=l1;l1=l2;l2=t;

        }
        for (int i=1;i<=l3;i++)
        {
            a[i]-=b[i];
            if (a[i]<0)
            {
                a[i]+=10;
                int p=i+1;
                while (a[p]==0)
                {
                    a[p]=9;
                    p++;
                }
                a[p]--;
            }
        }
        while (l3>1&&!a[l3])l3--;
        for (int i=l3;i>=1;i--)
            printf("%d",a[i]);
    }else
    {
        if (m1&&m2)printf("-");
        for (int i=1;i<=l3;i++)
        {
            c[i]+=a[i]+b[i];
            if (c[i]>9)
            {
                c[i]-=10;
                c[i+1]++;
            }
        }
        if (c[l3+1])l3++;
        for (int i=l3;i>=1;i--)
            printf("%d",c[i]);
    }
}
时间: 2024-08-03 23:37:32

bzoj3767 A+B Problem加强版的相关文章

【BZOJ3767】A+B Problem加强版

3767: A+B Problem加强版 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 760  Solved: 339[Submit][Status][Discuss] Description Input 输入A,B Output 输出A+B. Sample Input 1 1 Sample Output 2 HINT 对于100%的数据,保证 |A| , |B| 不会超过10^(10^7) sol:虽然说不支持提交了但是还是能交 压位板子题1

bzoj3767A+B Problem加强版

bzoj3767A+B Problem加强版 题意: 求两个数的和,每个数绝对值≤10^(10^7). 题解: 又用Python水过了…… 代码: 1 a=raw_input() 2 b=a.split() 3 print int(b[0])+int(b[1]) 20160828

算法笔记_093:蓝桥杯练习 Problem S4: Interesting Numbers 加强版(Java)

目录 1 问题描述 2 解决方案   1 问题描述 Problem Description We call a number interesting, if and only if: 1. Its digits consists of only 0, 1, 2 and 3, and all these digits occurred at least once. 2. Inside this number, all 0s occur before any 1s, and all 2s occur

可持久化并查集加强版 BZOJ 3674

http://www.lydsy.com/JudgeOnline/problem.php?id=3674 3674: 可持久化并查集加强版 Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 3225  Solved: 1192[Submit][Status][Discuss] Description Description:自从zkysb出了可持久化并查集后--hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可

贞鱼传教&amp;&amp;贞鱼传教(数据加强版)

http://acm.buaa.edu.cn/problem/1381/ 贞鱼传教[问题描述] 新的一年到来了,贞鱼哥决定到世界各地传授“贞教”,他想让“贞教”在2016年成为世界第四大宗教.说干就干......贞鱼哥把即将接受传教的人排成一行,每个人从左到右的编号为1-n.每个人有一个信仰值,一开始所有人的信仰值为0.接着贞鱼哥会做以下两件事之一: 1.贞鱼哥向连续的一段人群传教.具体来说,贞鱼哥会使第l个人到第r个人之间的所有人的信仰值增加k. 2.贞鱼哥想知道某一段人的信仰值之和,于是他想

Light OJ 1004 - Monkey Banana Problem dp题解

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dim

poj 3783 DP 2个鸡蛋扔100层楼的加强版

http://poj.org/problem?id=3783 估计23号之后的排位赛之后我就要退役了,这之前最后再做5天ACM 今天的排位很惨,上次排位也很惨......这道题原来算法课老师讲过,模模糊糊记得方程,但是边界处理有问题, dp[i][j]=min(1+max(dp[k-1][j-1],dp[i-k][j]))   k=1 to 楼数 dp[i][j]:i层楼扔,手里有j个ball 的次数 边界两个:1.dp[1][i]=1,第一层无论手里有几个鸡蛋都是1次,2.dp[i][1]=i

hdoj-1505-City Game【动态规划】1506的加强版

1506的加强版 City Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5587 Accepted Submission(s): 2412 Problem Description Bob is a strategy game programming specialist. In his new city building gam

leecode 回文字符串加强版

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th