模拟加法

You are given three numbers. Is there a way to replace variables A, B and C with these numbers so the equality A + B = C is correct?

Input
There are three numbers X1, X2 and X3 (1 ≤ Xi ≤ 10100), each on a separate line of input.

Output
Output either “YES”, if there is a way to substitute variables A, B and C with given numbers so the equality is correct, or “NO” otherwise.

cf上一道加法的模拟题

用双端队列模拟了一下过程,然后就做出来了

用完容器一定要记得 清空啊 !!!!!!

来吧 直接上代码 注释完备不多比比

#include<algorithm>
#include<iostream>
#include<sstream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<ctime>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int N = 1005;

///用 双端队列 模拟加法 然后三层循环 遍历所有的可能性

deque<int> s1, s2, s3, res;///res保存结果
///和输入的上s3比较
string ss[3];

bool love ()
{
///模拟加法
    int len1 = s1.size();
    int len2 = s2.size();
    int n = (int)fabs(len1 - len2);

    while (n--)
    {
        if (len1 < len2)
        {
            s1.push_front(0);
        }
        else if (len1 > len2)
        {
            s2.push_front(0);
        }
    }///把两个数字的位数统一

    n = len1 > len2 ? len1 : len2;

    while (n--)
    {
        res.push_back(0);
    }

    res.push_back(0);
    ///res队列比上面的队列长度大一
    ///方便第一位数的进位
    deque<int>::iterator p = res.end() - 1;
    ///迭代器 QAQ
    ///p指向res队列的最后一个元素

    while (!s1.empty())
    {
        int k = s1.back() + s2.back();
        s1.pop_back();
        s2.pop_back();
        ///计算两个数字的而最后一位数字

        if (k <= 9)
        {
            *p += k;

            if (*p > 9)
            {
                int a = *p;
                *p %= a;
                p--;
                (*p)++;
                p++;
            }

            p--;
        }///结果小于九就直接把该值放在队列中 然后移动指针到前一位++
        else
        {
            *p += k % 10;

            if (*p > 9)
            {
                int a = *p;
                *p %= a;
                p--;
                *p ++;
                p ++;
            }

            p--;
            (*p) ++;
        }///结果大于九 就让当前的位置的值加上mod 10

        ///移动指针 在++
    }

    ///这时p指向的是 第一个元素的前一位;
    p++;///移动到当前的位置

    if (res.front() == 0)
    {
        res.pop_front();
    }

    ///如果第一位上的元素是元素是0就弹出这个元素

    if (res.front() > 9)
    {
        int a1 = *p;
        *p %= 10;
        p--;
        (*p) ++;
    }

    /**
    *在实际的测试中,发现可能出现的第一位元素大于9的情况
    *在加上一组判断 ,确保正确
    **/

    if (res.size() != s3.size())
    {
        return false;
    }

    ///如果两个答案的长度都不相等 直接返回false;

    while (!res.empty())
    {
        if (res.back() != s3.back())
        {
            return false;
            return 0;
        }

        ///逐个比较元素 不相同就直接返回
        res.pop_back();
        s3.pop_back();
    }

    ///以上的都不成立就返回true
    return true;

}

void hhhhhh (string a, string b, string c)
{
    /**
    *把a,b,c对应的字符串放入 对应的容器中
    *字符串的单个元素 减去'0' 转化为int
    **/
    for (int i = 0; i < a.size(); i++)
    {
        s1.push_back((int)(a[i] - '0'));
    }

    for (int i = 0; i < b.size(); i++)
    {
        s2.push_back((int)(b[i] - '0'));
    }

    for (int i = 0; i < c.size(); i++)
    {
        s3.push_back((int)(c[i] - '0'));
    }
}

int main ()
{
    cin >> ss[0] >> ss[1] >> ss[2];
    int i, j, k;
    int s = 0;

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            for (k = 0; k < 3; k++)
            {
                hhhhhh(ss[i], ss[j], ss[k]);

                if (love())
                {
                    cout << "YES";
                    return 0;
                }

                s1.clear();
                s3.clear();
                s2.clear();
                res.clear();
            }
        }
    }

    cout << "NO";
    return 0;
}

在程序中,申明了一个迭代器

 deque<int>::iterator p = res.end() - 1;

其实 这样写 更方便(不用背单词) (偷笑)

 auto p = res.end() - 1;
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值