题意:给你一个可能不正确的算式a + b = c, 你可以在a,b,c中随意添加数字。输出一个添加数字最少的新等式x + y  = z;

题目链接

思路:来源于这片博客:https://www.cnblogs.com/ljh2000-jump/p/5886279.html

我们可以从个位开始搜索。如果现在c % 10 == (a % 10 + b % 10 + pre) % 10 (pre是之前的进位),那么直接把a, b, c的个位消去,加上进位继续深搜。如果不满足这个条件,我们可以考虑从a, b, c3个数中选一个数添加满足关系的一位,然后继续深搜。

代码:

#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int ans = INF;
LL ans_a, ans_b, ans_c;
LL p[20];
void dfs(LL a, LL b, LL c, LL nowa, LL nowb, LL nowc, LL pre, int now, int deep) {
if(now >= ans) return;
if(a == 0 && b == 0 && c == 0 && pre == 0) {
ans = now;
ans_a = nowa;
ans_b = nowb;
ans_c = nowc;
return;
}
if(c == 0) {
LL tmp = a + b + pre;
int cnt = 0;
while(tmp) {
cnt++;
tmp /= 10;
}
dfs(0, 0, 0, nowa + p[deep] * a, nowb + p[deep] * b, nowc + p[deep] * (a + b + pre), 0, now + cnt, deep + 1);
return;
}
if((a + b + pre) % 10 == c % 10) {
dfs(a / 10, b / 10, c / 10, nowa + (a % 10) * p[deep], nowb + (b % 10) * p[deep], nowc + (c % 10) * p[deep], (a %10 + b % 10 + pre) / 10, now, deep + 1);
return;
}
dfs(a * 10 + (c % 10 - b % 10 - pre + 10) % 10, b, c, nowa, nowb, nowc, pre, now + 1, deep);
dfs(a, b * 10 + (c % 10 - a % 10 - pre + 10) % 10, c, nowa, nowb, nowc, pre, now + 1, deep);
dfs(a, b, c * 10 + (a % 10 + b % 10 + pre) % 10, nowa, nowb, nowc, pre, now + 1, deep);
}
int main() {
LL a, b, c;
scanf("%lld+%lld=%lld", &a, &b, &c);
p[0] = 1;
for (int i = 1; i <= 18; i++)
p[i] = p[i - 1] * 10;
dfs(a, b, c, 0, 0, 0, 0, 0, 0);
printf("%lld+%lld=%lld",ans_a, ans_b, ans_c);
}

  

Codeforces 58E Expression (搜索)的更多相关文章

  1. codeforces 58E:Expression

    Description One day Vasya was solving arithmetical problems. He wrote down an expression a + b = c i ...

  2. CF58E Expression 搜索

    题目传送门:http://codeforces.com/problemset/problem/58/E 题意:给出一个形如$x+y=z$(不一定正确)的式子,试输出一个$a+b=c$的式子,满足:$1 ...

  3. codeforces 数字区分 搜索

    Jokewithpermutation Input file: joke.inOutput file: joke.outJoey had saved a permutation of integers f ...

  4. Codeforces 161E(搜索)

    要点 标签是dp但搜索一发就能过了. 因为是对称矩阵所以试填一下就是一个外层都填满了,因此搜索的深度其实不超过5. 显然要预处理有哪些素数.在这个过程中可以顺便再处理出一个\(vector:re[le ...

  5. Weakness and Poorness CodeForces - 578C 三分搜索 (精度!)

    You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weak ...

  6. Codeforces 730L - Expression Queries(大模拟)

    Codeforces 题面传送门 & 洛谷题面传送门 大模拟(?)+阿巴细节题,模拟赛时刚了 3h 最后因为某个细节写挂 100->40/ll/ll(下次一定不能再挂分了啊 awa) 首 ...

  7. Cleaner Robot - CodeForces 589J(搜索)

    有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断.求机器人最多能打扫的面 ...

  8. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

  9. [GodLove]Wine93 Tarining Round #7

    比赛链接: http://vjudge.net/contest/view.action?cid=47643#overview 比赛来源: 2012 ACM/ICPC Asia Regional Han ...

随机推荐

  1. cocos2d-js 创建帧动画

    封装创建方法 var RunActionHelper = function(){ };RunActionHelper.prototype.createAnimationByPlist = functi ...

  2. hdu 5239 Doom(线段树)

    Doom Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Sub ...

  3. JProfiler远程监控 -转

    1. 服务端安装JProfiler(与客户端版本一致) 2. 客户端配置连接: A).session——integration wizards——New remote integration B).选 ...

  4. 打印控件Lodop

    官网:http://www.lodop.net/demo.html Lodop.C-Lodop使用说明及样例   Lodop(标音:劳道谱,俗称:露肚皮)是专业WEB控件,用它既可裁剪输出页面内容,又 ...

  5. Gitblit的使用

    什么是 Gitblit Gitblit是一个开源的用于管理,查看和提供Git仓库. 它主要设计为希望托管集中存储库的小工作组的工具. Gitblit有什么特点 ... Gitblit部署示例1 日常维 ...

  6. LeetCode 305. Number of Islands II

    原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...

  7. LeetCode 480. Sliding Window Median

    原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description 题目: Median is the middl ...

  8. $_validate 自动验证 字段是否重复

    1.在控制器里面实例化 数据表的时候用D方法,不要用M方法 2.在当前Admin下的Model里面建立一个自定义的Model AdminsController.class.php ========== ...

  9. 页面报错Uncaught SyntaxError: Unexpected identifier

    错误描述:未捕获的语法错误:意想不到的标识符. 如图所示:检查之后发现是页面js内缺少“,”引起的.添加之后就OK了.

  10. 14.Selenium+Python使用火狐浏览器问题解决

    一开始使用的是IE浏览器作为自动化浏览器,但是由于想学习无头模式,故选择FireFox作为浏览器,以下是遇到的相关问题: 1.简单代码 from selenium import webdriver d ...