【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

迭代加深搜索。
枚举最大层数。(也即改变的数字个数
然后枚举第一个改哪个数字,第二个改哪个数字。。
一定要注意字典序问题。
每次优先改成较小的字典序(也即顺序枚举
然后注意这个字符不改的情况。
不要算改变数。
最后改完之后。
只需要枚举a和b的情况。
看看a*b是不是等于c就好
->查看这样的a*b数量是不是1
如果是1的话.就说明是正确的。直接输出那个ans就好(我们已经是从小到大枚举了,找到的一定是答案

【代码】

/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
/*
一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std; string x,y,z,s,ans;
int belong[10];
int maxdep,lenx,leny,lenz,tot; string inttostring(int x){
string s = "";
while (x > 0){
s = (char)(x%10+'0')+ s;
x/=10;
}
return s;
} int stringtoint(string s){
int x = 0;
int len = s.size();
for (int i = 0;i < len;i++){
x = x *10 + s[i]-'0';
}
return x;
} void ok(int dep){
if (tot>1) return;
if (dep==lenx+leny){
string v[3];
for (int i = 0;i < 3;i++) v[i]="";
for (int i = 0;i < lenx+leny+lenz;i++) v[belong[i]]+=s[i];
int tx = stringtoint(v[0]),ty = stringtoint(v[1]);
tx = tx*ty;
string tz = inttostring(tx);
if ((int)tz.size()!=(int)v[2].size()) return;
for (int i = 0;i < (int) v[2].size();i++)
if (tz[i]!=v[2][i] && v[2][i]!='*') return;
tot++;
return;
}
if (s[dep]=='*'){
int qidian = 0;
if (dep==0 || dep == lenx || dep==lenx+leny) qidian = 1;
for (int i = qidian;i <= 9;i++){
s[dep] = i+'0';
ok(dep+1);
s[dep] = '*';
}
}else ok(dep+1);
} bool dfs1(int dep,int nex){
if (dep==maxdep){
tot = 0;
string tans = s;
ok(0);
if(tot==1) {
ans = s;
return true;
}
return false;
}
if (nex>=lenx+leny+lenz) return false;
char temp; temp = s[nex];
s[nex] = '*';
int cnt = 1;
if (s[nex]==temp) cnt = 0;
if (dfs1(dep+cnt,nex+1)) return true;
s[nex] = temp; int qidian = 0;
if (nex==0 || nex == lenx || nex == lenx+leny) qidian = 1;
for (int i = qidian;i <= 9;i++){
temp = s[nex];
s[nex] = i+'0';
int cnt = 1;
if (s[nex]==temp) cnt = 0;
if (dfs1(dep+cnt,nex+1)) return true;
s[nex] = temp;
} return false;
} int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int kase = 0;
while (cin >> x && x[0]!='0'){
ans.clear();
cin >> y >> z;
lenx = x.size(),leny = y.size(),lenz = z.size();
s = x + y + z;
for (int i = 0;i < lenx;i++) belong[i] = 0;
for (int i = lenx;i < lenx+leny;i++) belong[i] = 1;
for (int i = lenx+leny;i < lenx+leny+lenz;i++) belong[i] = 2; for (maxdep = 0;;maxdep++)
if (dfs1(0,0)) break; cout <<"Case "<<++kase<<": ";
for (int i = 0;i < lenx;i++) cout << ans[i];cout <<' ';
for (int i = lenx;i < lenx+leny;i++) cout << ans[i];cout <<' ';
for (int i = lenx+leny;i < lenx+leny+lenz;i++) cout << ans[i];
cout << endl;
}
return 0;
}

【习题 7-8 UVA-12107】Digit Puzzle的更多相关文章

  1. UVA - 12107 Digit Puzzle(数字谜)(IDA*)

    题意:给出一个数字谜,要求修改尽量少的数,使修改后的数字谜只有唯一解.空格和数字可以随意替换,但不能增删,数字谜中所有涉及的数必须是没有前导零的正数.输入数字谜一定形如a*b=c,其中a.b.c分别最 ...

  2. UVA_Digit Puzzle UVA 12107

    If you hide some digits in an integer equation, you create a digit puzzle. The figure below shows tw ...

  3. UVa 1225 Digit Counting --- 水题

    UVa 1225 题目大意:把前n(n<=10000)个整数顺次写在一起,12345678910111213...,数一数0-9各出现多少字 解题思路:用一个cnt数组记录0-9这10个数字出现 ...

  4. UVa 1583 Digit Generator --- 水题+打表

    UVa 1583 题目大意:如果x加上x的各个数字之和得到y,那么称x是y的生成元. 给定数字n,求它的最小生成元 解题思路:可以利用打表的方法,提前计算出以i为生成元的数,设为d,并保存在a[d]中 ...

  5. 紫书 习题7-8 UVa 12107 (IDA*)

    参考了这哥们的博客 https://blog.csdn.net/hyqsblog/article/details/46980287  (1)atoi可以char数组转int, 头文件 cstdlib ...

  6. UVa 1225 Digit Counting

    题意:给出n,将前n个整数顺次写在一起,统计各个数字出现的次数. 用的最笨的办法--直接统计-- 后来发现网上的题解有先打表来做的 #include<iostream> #include& ...

  7. UVa 1583 Digit Generator(数学)

     题意 假设a加上a全部数位上的数等于b时 a称为b的generator  求给定数的最小generator 给的数n是小于100,000的  考虑到全部数位和最大的数99,999的数位和也才45 ...

  8. UVa 1583 - Digit Generator

    A+A的每一位的数字的和=B 问你每一个B对应 的最小的A 是多少 不然输出0: #include <cstdio> #include <iostream> #include ...

  9. UVa 10533 - Digit Primes

    题目:输出给定区间中,本身是素数,而且这个数的各位之和也是素数的数(称为位素数)的个数. 分析:数论.首先利用筛法,求出1000000内的全部的素数:然后在利用生成的素数表, 推断每一个数是不是各位之 ...

随机推荐

  1. 同一台服务器部署多个WEB应用,SESSION冲突的解决方法

    由于一台服务器上使用Tomcat部署多个WEB项目,而项目因为用到框架都是一样的,导致同时运行,session相互冲突,这个登录后,那个就得重新登录,造成了使用不方便,解决办法如下: 在server. ...

  2. 动态代理(jdk--cglib)

    JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类 ...

  3. BZOJ1901 ZOJ2112 线段树+treap (线段树套线段树)

    BZOJ1901: 线段树套线段树做法: (外层线段树 里层动态开节点的权值线段树) 有一个小小的trick 可以省掉二分变成nlog^2n的 就是把查询的区间都取出来- logn个一起走- 2016 ...

  4. Scrapy请求传参

    scrapy.Request(url=url, callback=self.parse_item, meta={'item': item}, headers=headers) url: 要请求的地址 ...

  5. Linux Cgroups

    目录 Linux Cgroups Cgroups中的三个组件 三个组件的关系 Kernel接口 Docker是如何使用Cgroups的 Go语言实现Cgroups限制容器资源 Linux Cgroup ...

  6. pip-window安装

    windows 安装: 保证计算机联网直接使用cmd 执行 python -m pip install -U pip 自动安装 找到 python安装的路径 C:\Users\Administrato ...

  7. MySQL Field排序法

    检索 id = 2 or id = 5 or id = 9 or id = 56 or id = 38.然后按照 2 , 5, 9, 56, 38 这个顺序排列,这是题目要求   以下为解决方案: 1 ...

  8. 单向链表 golang

    package main import "fmt" type Object interface {} //节点 type Node struct { data Object nex ...

  9. Fastboot线刷“复活”之刷机心得(三)——错误处理

        在刷机的过程中可能不会是一帆风顺的.至少我是这种,总是会遇到这样或者那样的问题,下面是我为大家总结一些问题和解决办法,希望能对大家有所帮助. 一.电量问题     刷机和系统更新有一个共同的前 ...

  10. php输出杨辉三角

    php输出杨辉三角 一.截图 二.代码 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...