回 帖 发 新 帖 刷新版面

主题:组合改拍了(结果是60种)

program P5_2(input,output);
type color=(red,orange,yellow,green,blue);
var m,m1,m2,m3:color;
    s,p:integer;
begin
  s:=0;
  for m1:=red to blue do
    for m2:=succ(m1) to blue do
      for m3:=succ(m2) to blue do
          if (m3<>m1) and (m3<>m2) then
          begin
              s:=s+1;
              write(s,':');
              for p:=1 to 3 do
                begin
                  case p of 1:m:=m1;
                  2:m:=m2;3:m:=m3;
                end;
                case m of
                  red:write('red':8);
                  orange:write('orange':8);
                  yellow:write('yellow':8);
                  green:write('green':8);
                  blue:write('blue':8);
                end;
            end;
            writeln;
          end;
        writeln('total:',s);
  readln;
end.

现在运行是10种,改成排列应该是60种!谢谢大家!

回复列表 (共1个回复)

沙发

如果题意为任从五种颜色里选三种的话,你出得最大问题就是进行了多余的判断,应该改成这样

rogram P5_2(input,output);
type color=(red,orange,yellow,green,blue);
var m,m1,m2,m3:color;
    s,p:integer;
begin
  s:=0;
  for m1:=red to blue do
    for m2:=red[color=FF00FF]{后面有判断了用succ没有意义}[/color] to blue do
      for m3:=red to blue do
          if (m3<>m1) and (m3<>m2) and [color=00FF00]{(m1<>m2)这里你少了一个判断}[/color] then
          begin
              s:=s+1;
              write(s,':');
              for p:=1 to 3 do
                begin
                  case p of 1:m:=m1;
                  2:m:=m2;3:m:=m3;
                end;
                case m of
                  red:write('red':8);
                  orange:write('orange':8);
                  yellow:write('yellow':8);
                  green:write('green':8);
                  blue:write('blue':8);
                end;
            end;
            writeln;
          end;
        writeln('total:',s);
  readln;
end.

我来回复

您尚未登录,请登录后再回复。点此登录或注册