PASCAL语言的一点很基础的问题 请大牛们帮我看下

[复制链接]
查看11 | 回复2 | 2008-5-15 14:52:38 | 显示全部楼层 |阅读模式
看了题目,会发现此题和noip2004普及组的《火星人》很像,但是又有一些差别。 其实《火星人》考的是生成排列,而此题考的是生成组合。 生成组合大致算法流程如下: 1. 从后向前找第一个未达到最大值的数a 2. a:=a+1 3. 后面的数都等于前面数加一 4. 判断a[0],若a[0]=1则结束,否则转1 再结合题目,读入时只要转字符位数即可,即A:=ord(ch)-96 具体代码如下: program noip2006_3_count; var a:array[1..26]of longint; s,t,w,i,p,q:longint; str:string; procedure first; begin assign(input,'count.in'); assign(output,'count.out'); reset(input);rewrite(output); readln(s,t,w); readln(str); end; Begin first; for i:=1 to w do a:=ord(str)-96; if t-s>w then begin p:=1; while (p=1)and(a[q]=t-w+q) do dec(q); if q0 then begin inc(a[q]); for i:=q+1 to w do a:=a[i-1]+1; for i:=1 to w do write(chr(a+96)); writeln; inc(p); end; end; end; close(input);close(output); end.
回复

使用道具 举报

千问 | 2008-5-15 14:52:38 | 显示全部楼层
找规律。参考了下以前的一个题目,Mars(或者是Martian,反正是火星人数数的那个)。列成一个表(长度为w的,boolean,相对应的字母有为true,无为false),然后从后向前找到一个空的。再从此向前找第一个一个非空的,把那个向后移1(肯定能保证后面那个不是true),然后让后面的true全部顶到移了的字母后。一个函数isend判断是否结束,一个过程next求下一个,一个过程print打印。program count(input,output);vars,t,w:integer;jam:array[1..26] of boolean;i,j:integer;a:char;procedure next;varii,jj,p:integer;beginp:=0;for ii:= t downto s do if not(jam[ii]) then break;for jj:= ii downto s do if jam[jj] then break;jam[jj]:=false;jam[jj+1]:=true;for ii:=(jj+2) to t do if jam[ii] then inc(p);for ii:=(jj+2) to t do jam[ii]:=false;for ii:=(jj+2) to (jj+1+p) do jam[ii]:=true;end;function isend:boolean;varii:integer;beginisend:=false;for ii:= t downto s do if not(jam[ii]) then break;if (s-ii)>=w then isend:=true;end;procedure print;varii:integer;beginfor ii:=s to t do if jam[ii] then write(chr(ord('a')+ii-1));writeln;end;beginassign(input,'count.in');assign(output,'count.out');reset(input);rewrite(output);readln(s,t,w);fillchar(jam,sizeof(jam),0);for i:=1 to w do beginread(a);jam[(ord(a)-ord('a')+1)]:=true;end;for i:=1 to 5 do if not(isend) then begin next; print; end;close(input);close(output);end.
回复

使用道具 举报

千问 | 2008-5-15 14:52:38 | 显示全部楼层
简单呀。只要用调用ASII码—函数chr一个字母来做,再减49。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行