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

【题意】

在这里输入题意

【题解】

迭代加深搜索。
枚举最大层数。(也即改变的数字个数
然后枚举第一个改哪个数字,第二个改哪个数字。。
一定要注意字典序问题。
每次优先改成较小的字典序(也即顺序枚举
然后注意这个字符不改的情况。
不要算改变数。
最后改完之后。
只需要枚举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. 基于matlab的音频波形实时採集显示 v0.1

    robj = audiorecorder(44100,16,1); %设置採样频率.採样位数.通道数 recordblocking(robj,1); %採集初步数据(1s长度) rdata = get ...

  2. [Java][log4j]支持同一时候按日期和文件大小切割日志

    依据DailyRollingFileAppender和RollingFileAppender改编,支持按日期和文件大小切割日志.  源文件: package com.bao.logging; impo ...

  3. [BZOJ5305][HAOI2018]苹果树 组合数学

    链接 小 C 在自己家的花园里种了一棵苹果树, 树上每个结点都有恰好两个分支. 经过细心的观察, 小 C 发现每一天这棵树都会生长出一个新的结点. 第一天的时候, 果树会长出一个根结点, 以后每一天, ...

  4. MySQL外键的介绍

    在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束. 1.MySQL中“键”和“索引”的定义相同,所以外键和主键一样也是索引的一种.不同的是MySQL会自动为所有表的主键进行索 ...

  5. POJ 1966 求无向图点连通度

    思路: n^2枚举(必须要n^2枚举啊)+拆点 特此嘲讽网上诸多垃圾题解,你们许多都是错的 -yyh //By SiriusRen #include <queue> #include &l ...

  6. windows 手动添加服务

    windows 手动添加服务方法一:修改注册表 在注册表编辑器,展开分支"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" ...

  7. 关于Shiro的退出请求是如何关联到登录请求的思考

    一.结论 先给出结论,是因为本身是很简单的道理.假设我们没有使用任何认证授权的框架,就简单的使用Cookie和HttpSession,那么用户登录后的每一个请求是如何关联上这个用户的呢?答案很简单,由 ...

  8. amlogic M8操作gpio bank

    參照规格书: r代表:读 a代表GPIOAO bank 0x28代表read bit echo r a 0x28 > /sys/class/amlogic/debug 操作GPIO口读取 w代表 ...

  9. 轻松使用 Redis slowlog

    之前中秋项目搞活动,用户比较活跃 SE.Redis 频繁报 Timeout 异常,狂翻了一波 issues 发现提这个问题还蛮多的,作者非常频繁的提到使用 slowlog 这个命令进行排查,那么问题就 ...

  10. js -- img 随着鼠标滚轮的变化变化

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...