zoukankan      html  css  js  c++  java
  • Codeforces 58E Expression (搜索)

    题意:给你一个可能不正确的算式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);
    } 
    

      

  • 相关阅读:
    jstl核心标签库
    乱码的解决
    eclipse 中 Servlet 模板代码(其实是代码提示模板)
    因为最近一直在和数据库打交道,所以做了几个小封装
    意外发现的大批量导入数据SqlBulkCopy类
    Http相关
    Tomcat相关
    对于反射中的invoke()方法的理解
    SqlCommandBuilder类是如何构建T-Sql语句
    模拟在内存中的数据库DataSet相关的类
  • 原文地址:https://www.cnblogs.com/pkgunboat/p/10438018.html
Copyright © 2011-2022 走看看