外星生命
(et.pas/in/out)
Problem
地球上的科学家收到了来自外星的信号:000023*000011=002093,科学家猜想这是某个外星人的年龄。但有人指出,这些外星人好像不怎么聪明,因为23*11=253,而非2093。 但是科学家们发现,如果把000011改成00091的话算式就成立了。他们认为这是接收信号的时候出了差错的缘故。现在给你这样一个算式,问最少改动几个数字就能使得算式成立?
Input
只有一个算式以a*b=c的形式给出,其中a、b、c长度都是6位。
Output
输出最少改动几个数字才能使得算式成立。
我们忽略进位,即a*b的结果只要最后6位与c相同就认为算式成立。
Sample Input
000023*000011=002093
Sample Output
1
==========================
搜索...
1.枚举每一位判断是否和给出的相同..
2.模拟高精乘....
--------------
==============================
{
ID:jie19952
PROG:
LANG:PASCAL
}
var
st:string;
ans:longint;
a,b,c:array[1..6]of longint;
procedure init;
begin
assign(input,'et.in');
assign(output,'et.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
procedure pre;
var
i:longint;
begin
readln(st);
for i:=1 to 6 do
begin
a[i]:=ord(st[i])-ord('0');
b[i]:=ord(st[7+i])-ord('0');
c[i]:=ord(st[14+i])-ord('0');
end;
end;
procedure dfs(l,jw,m:longint);
var
x,y:longint;
x0,y0:longint;
sum:longint;
i:longint;
begin
if m>=ans then exit;
if l=0 then begin ans:=m; exit; end;
x0:=a[l]; y0:=b[l];
for x:=0 to 9 do
for y:=0 to 9 do
begin
if x0<>x then inc(m);
if y0<>y then inc(m);
a[l]:=x; b[l]:=y;
sum:=jw;
for i:=l to 6 do
inc(sum,a[i]*b[l+6-i]);
if sum mod 10<>c[l] then inc(m);
dfs(l-1,sum div 10,m);
if sum mod 10<>c[l] then dec(m);
if x0<>x then dec(m);
if y0<>y then dec(m);
end;
a[l]:=x0; b[l]:=y0;
end;
procedure main;
begin
ans:=6;
dfs(6,0,0);
writeln(ans);
end;
begin
init;
pre;
main;
terminate;
end.