博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

循环结构练习题目3

Posted on 2010-10-07 13:42  桃子在路上  阅读(165)  评论(0编辑  收藏  举报

6. 一个两位数 x,个位上的数字与十位上的数字对调后得到第二个两位数 y;此时, y - x = 36。求所有这样的两位数。

program exExhaustive6_1;
const c = 36;
var
     f, s : integer;
begin
     for s := 1 to 9 do
         for f := 1 to s-1 do
             if (s*10+f-f*10-s = c) then writeln(s*10+f, '-', f*10+s, '=', c);
     readln;
end.

7. (30+35)*(30+25) = 3025

program exExhaustive7_1;
var
     f, s : integer;
begin
     for f := 10 to 99 do
         for s := 0 to 99 do
             if (s+f)*(s+f) = f*100+s then writeln(f*100+s);
     readln;
end.

8. 法国数学家梅齐亚克的砝码题目:

有位商人有40磅重的砝码,不小心摔成4块碎片.每块都是整磅数,能秤1-40之间任意重量的物品(整磅).请问四块碎片的重量各是多少?
program exExhaustive8_1;
const All = 40;
var
     p1, p2, p3, p4, p1s, p2s, p3s, p4s, w : integer;
         // p1 ~ p4 为4块的重量,它们可能放在被称物体一边或另一边,即既可以加这个重量也可以减这个重量,当然某块也可以不用
         // p1s ~ p4s 为正负符号或零,用来分别表示某块放在被称物体一边或另一边,零表示某块未用
     flag : boolean;
begin
     for p1 := 1 to All do
         for p2 := p1+1 to All-p1 do
             for p3 := p2+1 to All-p1-p2 do
             begin
                  p4 := All-p1-p2-p3;
                  if p4 > p3 then
                  begin
                       flag := true;
                       for w := 1 to All do
                           if flag then
                           begin
                                flag := false;
                                for p1s := -1 to 1 do
                                    if not flag then
                                    for p2s := -1 to 1 do
                                        if not flag then
                                        for p3s := -1 to 1 do
                                            if not flag then
                                            for p4s := -1 to 1 do
                                                if not flag then
                                                if w = abs(p1s*p1 + p2s*p2 + p3s*p3 + p4s*p4) then flag := true;
                           end;
                       if flag then writeln('4 pieces: ', p1, ' ', p2, ' ', p3, ' ', p4);
                  end;
             end;
     readln;
end.