宁波市第25届中小学生程序设计竞赛(初中试题及答案)(2)

2020-04-14 06:29

第3题:

Program cz2010_3;

var fu,a,num,b,tot,k,n:longint; shu:boolean;c:char; begin

num:=0;a:=0;fu:=1;shu:=false;b:=0;

while not(eoln) do begin //当一行输入未结束时 read(c);

if c='-' then fu:=-1

else if (c<='9') and (c>='0') then begin val(c,k);num:=10*num+k;shu:=true; end

else if shu then begin shu:=false;

if (fu=1) and (num>0) then inc(a); if fu=-1 then inc(b); inc(tot,num*fu); inc(n);num:=0;fu:=1; end; end;

if shu then begin shu:=false;

if (fu=1) and (num>0) then inc(a); if fu=-1 then inc(b); inc(n); inc(tot,num*fu); num:=0;fu:=1; end;

writeln(tot/n:0:3,' ',a,' ',b); close(input);close(output); end.

【输入】 54hiy-24 51 【输出】 ____________

第4题:

Program cz2010_4;

var f:array[1..46]of longint; a:array[1..100]of longint;

x,y,z,i,j,n,count:longint;

function find(i,h,x:longint):longint; var m:longint; begin

if i<3 then exit(0); m:=h+f[i-1]-1; count:=count+1;

if x=a[m] then exit(m);

if x

f[1]:=1;f[2]:=1;

for i:=3 to 46 do f[i]:=f[i-1]+f[i-2]; readln(n,x,y,z);

for i:=1 to n do a[i]:=3*i-1; i:=1;

while f[i]-1

for j:=n+1 to f[i]-1 do a[j]:=maxlongint; count:=0; j:=find(i,1,x); writeln(j,' ',count); count:=0; j:=find(i,1,y); writeln(j,' ',count); count:=0; j:=find(i,1,z); writeln(j,' ',count); end.

【输入】 8 14 7 17 【输出】 _________ _________ _________

四.程序填空(第1题每空2分,第2题每空3分,共28分) 1、多项式乘法

二个只含有一个未知数x的多项式相乘,得到的结果也是含有未知数x的多项式。比如(1+2x2+4x4)*(6+x-2x3)=6+x+12x2+24x4-8x7。计算过程中,可以让第1个多项式的每一项去乘上第2个多项式,将得到的每个乘积结果相加。比如上述二式相乘的例子中:

1乘上第2式得: 6+x-2x3,

2x2乘上第2式得: 12x2+2x3-4x5, 4x4乘上第2式得: 24x4+4x5-8x7,

三个乘积相加得最终结果:6+x+12x2+24x4-8x7。 样例输出: 6.000 0 1.000 1 12.000 2 24.000 4 -8.000 7

样例输入: 3 3 1 0 2 2 4 4 6 0 1 1 -2 3

以下程序中将第一个多项式的系数和指数分别存储在a1 和b1数组中,将第二个多项式的系数和指数分别存储在a2 和b2数组中,结果存放在数组a和b中。其中变量n1和 n2分别表示二个多项式的项数。

假设二个相乘的多项式均不超过1000项,且x的次数 最高项均不超过1000。请将该程序补充完整。

Program cz2010_5; const maxn=1000; eps=1e-10;

var a1,a2:array[1..maxn]of extended; b1,b2:array[1..maxn]of longint;

a:array[ ① ]of extended; b:array[ ① ]of longint; n1,n2,n,i,j,k:longint; s:extended; begin

readln(n1,n2);

for i:=1 to n1 do readln(a1[i],b1[i]);// 输入第1个多项式系数和指数 for i:=1 to n2 do readln(a2[i],b2[i]); // 输入第2个多项式系数和指数 fillchar(a,sizeof(a),0); for i:=1 to n1 do

for j:=1 to n2 do begin

k:= ② ; a[k]:= ③ ; end; n:=-1; i:=0;

while i<=b1[n1]+b2[n2] do begin if abs(a[i])>=eps then begin n:=n+1;

④ ; b[n]:= ⑤ ; end; i:=i+1; end;

for i:=0 to n do writeln(a[i]:0:3,' ',b[i]); end.

2、表达式求值

以下程序计算包含“+”、“-”、“*”、“(”、“)”和正整数的一个表达式的值。以数组num和数组code作为二个栈。其中栈num用来存储表达式中的数值以及计算的中间结果,栈code用来存储表达式中的运算符号。最终结果存储在num[1]中,程序输出最终求得的一个整数值num[1]。

程序逐字符扫描表达式:

1、如果是运算数,则直接进运算数栈num。 2、如果是运算符:

2.1如果当前运算符级别低于或相同于位于运算符栈顶的前一个运算符的级别,则: 2.1.1 在运算数栈中出栈两次,得到a,b; 2.1.2运算符栈出栈,得运算符p;

2.1.3 将a和b在运算p下的计算结果入运算数栈;

2.1.4当前运算符继续与位于运算符栈顶的前一个运算符比较;

2.2如果当前运算符级别高于位于运算符栈顶的前一个运算符级别,则当前运算符进栈: 3、左括号最高级。右括号最低级

3.1因此,遇左括号时,左括号入栈;但左括号在栈内时,级别低于任何其它符号! 3.2遇右括号时,一直作运算,直至遇上左括号,则简单地作左括号出栈即可,且此时右括号不进栈;

为方便起见,程序会在输入的表达式前后加上一对括号。另外,输入数据保证是正确的。请将程序补充完整。 【样例输入】

12+2*34+(45-5)*(6+7)

【样例输出】 600

Program cz2010_6; var s:ansistring;

n,i,tc,tn:longint; x,y:extended;

num:array[1..1001]of extended; code:array[1..1001]of char;

function cal(x,y:extended;c:char):extended;//计算x和y在运算c下的值 begin

if c='-' then cal:=x-y else if c='+' then cal:=x+y else cal:=x*y; end;

function prio(x,y:char):boolean;//运算符x比运算y级别高吗? begin

if x='(' then prio:=false else if x='*' then prio:=true

else if (x='+')and((y='+')or(y='-'))then prio:=true else if (x='-')and((y='+')or(y='-'))then prio:=true else prio:=false; end; begin

readln(s);s:='('+s+')'; n:=length(s); x:=0;tc:=0;tn:=0; for i:=1 to n do begin

if (s[i]>='0')and(s[i]<='9') then x:=x*10+ord(s[i])-ord('0') else begin

if x<>0 then begin

inc(tn);num[tn]:=x; ⑥ ;end; if s[i]='(' then begin

⑦ ;code[tc]:=s[i];end else if s[i]=')' then begin

while ⑧ do begin dec(tn);

num[tn]:=⑨ ; dec(tc); end;

⑩ ;


宁波市第25届中小学生程序设计竞赛(初中试题及答案)(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:会计报表编制与分析 - 于久洪 - 现金流量表分析(1)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: