加载中…
个人资料
sacredfantasy
sacredfantasy
  • 博客等级:
  • 博客积分:0
  • 博客访问:3,313
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

usaco 2.3.3 zerosum

(2009-04-24 23:29:27)
标签:

usaco

2.3.3

zerosum

杂谈

分类: OI题解

本题不难,但是由于没有好的算术方案,所以编的时间长,又因为pascal递归不稳定,所以调试时间长,提交了三次.

Compiling...

Compile: OK

 

Executing...

   Test 1: TEST OK [0.000 secs, 204 KB]

   Test 2: TEST OK [0.000 secs, 204 KB]

   Test 3: TEST OK [0.000 secs, 208 KB]

   Test 4: TEST OK [0.000 secs, 208 KB]

   Test 5: TEST OK [0.000 secs, 204 KB]

   Test 6: TEST OK [0.011 secs, 208 KB]

   Test 7: TEST OK [0.011 secs, 208 KB]

 

All tests OK.

Your program ('zerosum') produced all correct answers!  This is your

submission #3 for this problem.  Congratulations!

program test;

type node=array[0..8]of integer;

var n:3..9;

         a:array[1..9]of integer;

         b1:array[1..9]of integer;

         total:longint;

         b:node;

procedure print(c:node);

var i:integer;

begin

         for i:=2 to n do

         begin

                   case c[i-1] of

                   1:begin

                            write(a[i-1],' ');

                            end;

                   2:begin

                            write(a[i-1],'+');

                            end;

                   3:begin

                            write(a[i-1],'-');

                            end;

                   end;

         end;

         writeln(a[n]);

         {for i:=1 to n-1 do

                   write(c[i],' ');

         writeln;}

end;

procedure pd(c:node);

var i,temp,temp2:integer;

begin

         fillchar(b1,sizeof(b1),0);

         total:=0;temp:=0;

         for i:=1 to n do

         begin

                   if c[i]=1 then

                   begin

                            if c[i-1]=1 then total:=total*10+a[i+1];

                            if c[i-1]<>1 then

                            begin

                                     total:=a[i];

                                     total:=total*10+a[i+1];

                            end;

                            if c[i+1]<>1 then

                            begin inc(temp);b1[temp]:=total;end;

                   end;

         end;

         temp:=1;temp2:=0;

         if c[1]=1 then begin total:=b1[1];inc(temp2);end

                   else total:=a[1];

         while temp<n do

         begin

                   if c[temp]<>1 then

                   begin

                   if c[temp+1]<>1 then

                            case c[temp] of

                                     2:total:=total+a[temp+1];

                                     3:total:=total-a[temp+1];

                            end

                   else

                            begin

                                     inc(temp2);

                                     case c[temp] of

                                     2:total:=total+b1[temp2];

                                     3:total:=total-b1[temp2];

                                     end;

                                     for i:=temp+1 to n do

                                               if i<>1 then

                                               begin

                                                        temp:=i;

                                                        break;

                                               end;

                            end;

                   end;

                   inc(temp);

         end;

         if total=0 then print(c)

                   else exit;

end;

procedure dfs(x:integer;b:node);

var i:integer;

begin

         if (x=n) then

         begin

                   pd(b);

                   exit;

         end;

         for i:=1 to 3 do

         begin

                   b[x]:=i;

                   if x+1<=n then dfs(x+1,b);

         end;

end;

procedure init;

var i:integer;

begin

         readln(n);

         fillchar(a,sizeof(a),0);

         for i:=1 to n do

                   a[i]:=i;

         b[0]:=0;

end;

begin

  assign(input,'zerosum.in');

  reset(input);

  assign(output,'zerosum.out');

  rewrite(output);

  init;

  dfs(1,b);

  close(input);

  close(output);

end.

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有