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

循环结构练习题目1

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

1. 将100圆兑换为20, 10, 5, 2, 1,五种不同面额的纸币,输出每种换法,最后输出换法总数。

program exExhaustive1_1;
const All = 100;
var
     twenty, ten, five, two, num : integer;
begin
        for twenty := 1 to All div 20 do
            for ten := 1 to (All - twenty * 20) div 10 do
                for five := 1 to (All - twenty * 20 - ten * 10) div 5 do
                    for two := 1 to (All - twenty * 20 - ten * 10 - five * 5) div 2 do
                        if (All - twenty * 20 - ten * 10 - five * 5 - two * 2) <> 0 then
                        begin
                           writeln(All, ' = ', twenty, 'x20+', ten, 'x10+', five, 'x5+', two, 'x2+', (All - twenty*20 - ten*10 - five*5 - two*2), 'x1');
                           num := num+1;
                        end;
        writeln('There are ', num, ' kinds.');
        readln;
end.
 

2. 韩信点兵又称为中国剩余定理:相传汉高祖刘邦问大将军韩信统御兵士多少,韩信答说,每3人一列余1人、5人一列余2人、7人一列余4人、13人一列余6人……。刘邦茫然而不知其数。

program exExhaustive2_1;
const Maxnum = 100;
var
     remainder3, remainder5, remainder7, num : integer;
begin
     write('Please input 3 remainders (by 3, 5, 7): ');
     readln(remainder3, remainder5, remainder7);
     num := remainder7;
     while num <= Maxnum do
     begin
          if (num mod 3 = remainder3) and (num mod 5 = remainder5) then writeln('The number maybe is ', num);
          num := num+7;
     end;
     readln;
end.

3. 求 2 - 200内的所有素数,每行输出10个。

program exExhaustive3_1;
const e = 200;
var
     i, j, k, sq : integer;
begin
     write('   2    3 ');
     k := 2;
     i := 5;
     while i <=  e do
     begin
          sq := trunc(sqrt(i));
          for j := 3 to sq do
              if i mod j = 0 then
                 break;
          if i mod j <> 0 then
          begin
               write(i : 4, ' ');
               k := k+1;
               if k mod 10 = 0 then writeln;
          end;
          i := i+2;
     end;
     writeln;
     writeln('There are ', k, ' primes.');
     readln;
end.

4. 对于一个100以内的素数(也称质数),把它的十位数字与个位数字对换之后,若得到的新整数仍为素数,则称原来的那个素数为“绝对素数”。例如11、13对换后为11、31仍是素数,它们就是绝对素数。要求编程求出100以内所有“绝对素数”。

program exExhaustive4_1;
var
     gw, sw, i, num, sq : integer;
begin
     sw := 1;
     while sw <= 9 do
     begin
           gw := 1;
           while gw <= sw do
           begin
                num := sw*10+gw;
                sq := trunc(sqrt(num));
                for i := 3 to sq do
                    if (num mod i = 0) or ((gw*10+sw) mod i = 0) then break;
                if (num mod i <> 0) and ((gw*10+sw) mod i <> 0) then
                   writeln(num, ' ', gw*10+sw);
                gw := gw+2;
           end;
           sw := sw+2;
     end;
     readln;
end.