【习题 7-8 UVA-12107】Digit Puzzle
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
迭代加深搜索。
枚举最大层数。(也即改变的数字个数
然后枚举第一个改哪个数字,第二个改哪个数字。。
一定要注意字典序问题。
每次优先改成较小的字典序(也即顺序枚举
然后注意这个字符不改的情况。
不要算改变数。
最后改完之后。
只需要枚举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的更多相关文章
- UVA - 12107 Digit Puzzle(数字谜)(IDA*)
题意:给出一个数字谜,要求修改尽量少的数,使修改后的数字谜只有唯一解.空格和数字可以随意替换,但不能增删,数字谜中所有涉及的数必须是没有前导零的正数.输入数字谜一定形如a*b=c,其中a.b.c分别最 ...
- UVA_Digit Puzzle UVA 12107
If you hide some digits in an integer equation, you create a digit puzzle. The figure below shows tw ...
- UVa 1225 Digit Counting --- 水题
UVa 1225 题目大意:把前n(n<=10000)个整数顺次写在一起,12345678910111213...,数一数0-9各出现多少字 解题思路:用一个cnt数组记录0-9这10个数字出现 ...
- UVa 1583 Digit Generator --- 水题+打表
UVa 1583 题目大意:如果x加上x的各个数字之和得到y,那么称x是y的生成元. 给定数字n,求它的最小生成元 解题思路:可以利用打表的方法,提前计算出以i为生成元的数,设为d,并保存在a[d]中 ...
- 紫书 习题7-8 UVa 12107 (IDA*)
参考了这哥们的博客 https://blog.csdn.net/hyqsblog/article/details/46980287 (1)atoi可以char数组转int, 头文件 cstdlib ...
- UVa 1225 Digit Counting
题意:给出n,将前n个整数顺次写在一起,统计各个数字出现的次数. 用的最笨的办法--直接统计-- 后来发现网上的题解有先打表来做的 #include<iostream> #include& ...
- UVa 1583 Digit Generator(数学)
题意 假设a加上a全部数位上的数等于b时 a称为b的generator 求给定数的最小generator 给的数n是小于100,000的 考虑到全部数位和最大的数99,999的数位和也才45 ...
- UVa 1583 - Digit Generator
A+A的每一位的数字的和=B 问你每一个B对应 的最小的A 是多少 不然输出0: #include <cstdio> #include <iostream> #include ...
- UVa 10533 - Digit Primes
题目:输出给定区间中,本身是素数,而且这个数的各位之和也是素数的数(称为位素数)的个数. 分析:数论.首先利用筛法,求出1000000内的全部的素数:然后在利用生成的素数表, 推断每一个数是不是各位之 ...
随机推荐
- FPGA design flow
FPGA engineering process usually involves the following stages: Architecture design. This stage invo ...
- Github-flavored Markdown 导出为 PDF
前提条件: 你可以访问Google和它相关的服务 第一步 到Chrome Store安装插件 Markdown Preview Plus 安装以后记得勾选 "允许访问文件网址" 设 ...
- Java反射异常处理之InvocationTargetException
java.lang.reflect.InvocationTargetException处理办法可能是没有引commons-lang3-3.x.jar包
- 访问API的方式为:localhost/api/customers, 创建自定义JSON格式化器
注意的是,访问API的方式为:localhost/api/customers,在实际中将要根据情况替换合适的端口,默认所有的WEB API都是通过/api根目录的方式访问的 创建自定义JSON格式化器 ...
- 小试VS 2017 开发Python Django项目过程一
一.新建项目python ->django web 项目 (选择带bootstrap风格与twwriter)项目名称iepiececomputing (ie计件计算)跳出窗体 -> 添加虚 ...
- 【Uva 1336】Fixing the Great Wall
[Link]: [Description] 给你长城上的n个修补点,然后你的位置为x; 你需要依次去这n个点,然后把它们全部修好. 但是修的前后顺序不一样的话,花费不一样. 如果立即把第i个点修好的话 ...
- [Python] Create Unique Unordered Collections in Python with Set
A set is an unordered collection with no duplicate items in Python. In this lesson, you will learn h ...
- PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自測4. Have Fun with Numbers (20) 【二星级】
题目链接:http://www.patest.cn/contests/mooc-ds/00-%E8%87%AA%E6%B5%8B4 题面: 00-自測4. Have Fun with Numbers ...
- button-xml 中android:clickable="false" 属性
今天在做项目的时候,遇到了一个问题,就是需要把一个常按监听事件,加到一个linearlayout中,但是,这个linearlayout中有其他的button.textview等控件,这样就导致当我们常 ...
- Java 关于运算结果
今天发现一个自己的一个知识误区 原来有些自己以为会发生异常的运算,并没有异常发生,只要运算符两端存在至少一个浮点数 比如: public class Demo { /** * @param args ...