讨论 / 史上各个神牛来解决A+B的方法!!不看别后悔!
desprado2 2013-08-19 13:41:00
点我顶贴 收藏 删除
最简洁的解法:

program p1000;

var a,b:longint;

begin

readln(a,b);

writeln(a+b);

end.

高精度算法:

program p1000;

var

st:string;

x,y:array[0..101]of integer;

i,j,l1,l2:integer;

begin

readln(st);

fillchar(x,sizeof(x),0);

fillchar(y,sizeof(y),0);

l1:=length(st);

for i:=l1 downto 1 do

x[l1-i]:=ord(st[i])-ord('0');

readln(st);

l2:=length(st);

for j:=l2 downto 1 do

y[l2-j]:=ord(st[j])-ord('0');

if l1<l2 then l1:=l2;

for i:=0 to l1 do

begin

x[i]:=x[i]+y[i];

if x[i]>=10 then begin

inc(x[i+1]);

dec(x[i],10);

end;

end;

while x[l1]<>0 do

inc(l1);

for i:=l1-1 downto 0 do

write(x[i]);

end.

分类别讨论解法(先算'A'再算'B'):

program p1000;

var

a,b,i:int64;

s:int64;

begin

readln(a,b);

for i:=1 to a do inc(s);

for j:=1 to b do inc(s);

writeln(s);

end.

随机数Random算法:

program p1000;

var a,b,n,m,s:longint;

begin

readln(a,b);

randomize;

repeat

n:=random(a+1);

m:=random(b+1);

s:=random(n+m+1);

until s=a+b;

writeln(s);

end.

数形结合的算法(利用 直角三角形公式A^2+B^2=C^2):

program p1000;

var

a,b,c:longint;

begin

readln(a,b);

c:=sqr(sqr(sqrt(a))+sqr(sqrt(b)));

writeln(trunc(sqrt(c));

end.

最小网络流:

program p1000;

var

en,et,ec,eu,ep,ex:Array[0..250000] of longint;

dis:array[0..1000] of longint;

v:array[0..1000] of boolean;

i,j,k,n,m,w,cost,l:longint;

a,b,ans,left,right:longint;

function min(a,b:longint):longint;

begin

if a<b then min:=a else min:=b

end;

procedure addedge(s,t,c,u,k:longint);

begin

inc(l);

en[l]:=en[s];

en[s]:=l;

et[l]:=t;

ec[l]:=c;

eu[l]:=u;

ep[l]:=l+k;

end;

procedure build(s,t,u,c:longint);

begin

addedge(s,t,c,u,1);

addedge(t,s,-c,0,-1);

end;

function aug(no,m:longint):longint;

var

i,d:longint;

begin

if no=n then

begin

inc(cost,m*dis[1]);

exit;

end;

v[no]:=true;

i:=ex[no];

while i<>0 do

begin

if (eu[i]>0)and not v[et[i]] and(dis[et[i]]+ec[i]=dis[no]) then

begin

d:=aug(et[i],min(m,eu[i]));

if d>0 then

begin

dec(eu[i],d);

inc(eu[ep[i]],d);

ex[no]:=i;

exit(d);

end;

end;

i:=en[i];

end;

ex[no]:=i;

exit(0);

end;

function modlabel:boolean;

var

d,i,j:longint;

begin

d:=maxlongint;

for i:=1 to n do

if v[i] then

begin

j:=en[i];

while j<>0 do

begin

if (eu[j]>0)and not v[et[j]] and(ec[j]-dis[i]+dis[et[j]]<d) then

d:=ec[j]-dis[i]+dis[et[j]];

j:=en[j]

end;

end;

if d=maxlongint then exit(true);

for i:=1 to n do

if v[i] then

begin

v[i]:=false;

inc(dis[i],d);

end;

exit(false);

end;

function work:longint;

var i:longint;

begin

cost:=0;

repeat

for i:=1 to n do ex[i]:=en[i];

while aug(1,maxlongint)>0 do

fillchar(v,sizeof(v),0);

until modlabel;

work:=cost;

end;

function solve(x,d:longint):longint;

var i,k,t,p,last,cost,lk:longint;

begin

fillchar(en,sizeof(en),0);

fillchar(dis,sizeof(dis),0);

k:=0;

n:=2;

t:=x;

p:=0;

while x<>0 do

begin

k:=k+x mod 10;

x:=x div 10;

inc(p);

end;

n:=1;

x:=t;

l:=k+p+1;

last:=1;

cost:=1;

lk:=0;

while x<>0 do

begin

k:=x mod 10;

for i:=1 to k do

begin

inc(n);

build(last,n,1,-cost);

build(n,last+k+1,1,0);

end;

cost:=cost*10;

inc(n);

if last<>1 then

begin

if lk<k then

build(1,last,k-lk,0);

if k<lk then

build(last,n,lk-k,0);

end;

last:=n;

x:=x div 10;

if lk<k then lk:=k;

end;

build(1,n,1,d);

solve:=-work;

end;

begin

readln(a,b);

left:=1;

right:=1000000000;

while right-left>15000 do

begin

ans:=(left+right)shr 1;

if solve(ans,b)>a then

right:=ans

else left:=ans;

end;

for i:=left to right do

if solve(i,b)=a then

begin

writeln(i);

halt;

end;

end.

面向对象的解法:

program p1000;

var a,b,c:qword;

function max(a,b:qword):qword;

begin

if a<b then exit(b)

else exit(a);

end;

operator :=(a:qword):b:qword;

begin

b:=0;

if max(a,b)=a then b:=max(a,b)

else b:=min(a,b);

end;

operator +(a,b:qword)c:qword;

begin

c:=0;

c:=max(a,b)+min(a,b);

end;

begin

readln(a,b);

c:=a+b;

writeln(c);

end.

神速的汇编解法:

program p1000;

var

a,b:longint;

begin

readln(a,b);

asm

  mov a,%eax

  add b,%eax

  mov %eax,b

end.

递推解法:

program p1000;

var i,j,a,b:longint;

f:array[0..100000] of longint;

begin

readln(a,b);

for i:=1 to a+b do f[i]:=f[i-1]+1;

writeln(f[i]);

end.

-。-

大家顶一下吧!好吗?

#1 nxyz117@2012-02-26 06:12:00
回复 删除
太牛了!!!!!
#2 ~\(≧▽≦)/~@2012-02-26 08:33:00
回复 删除
jajajajaja!!!!
#3 jst123@2012-02-26 15:13:00
回复 删除
牛到家啊!!!!!!
#4 折翼天使@2012-02-26 15:48:00
回复 删除
啊vv

#5 hzy1998@2012-02-26 17:31:00
回复 删除
.......
#6 desprado2@2012-02-26 21:13:00
回复 删除
自顶

#7 desprado2@2012-02-26 21:26:00
回复 删除
各位强势顶起!!
#8 初学者123@2012-02-27 19:59:00
回复 删除
看到了我的程序哎~~~dp都不熟的弱菜表示网络流和什么面向对象完全不懂,不过应该还能用深搜,i从0开始深搜,边界是i=a+b,我是用移动设备上的,写个没经调试的:

Var a,b:longint;

Procedure print(I:longint);

Begin

Writeln(I);

Readln;

Halt;

End;

Procedure Dfs(I:longint);

Begin

If I=a+b then print(I);

Dfs(I+1);

End;

Begin

Readln(a,b);

Dfs(0);

End.

Ps:未调试,可能有错,

Pss:这个是自动大写,不是我的编程风格,见谅。

#9 ahfy_zyt@2012-02-28 21:57:00
回复 删除
太疯狂了
#10 追风猎人SZ@2012-02-29 13:16:00
回复 删除
厉害!

强大啊啊啊啊啊啊!!!!!

查看更多回复
提交回复