pascal编程只需解释,兔子小白是位数学爱好者,有一次它参加了兔界里的数学比赛或者另外告诉个简便的方法

成绩公布后,小白很想知道自己的成绩到底排第几,现在请你帮它编一个程序,要求输入一个成绩,就能知道相应的名次。注意:同分的按相同名次算,且只算一次。
程序如下:
var
a:array[0..1000] of integer;
i,n ,x,y,s,max:integer;
begin
readln(n); max:=0;
for i:=1 to n do
read(x);
if x>max then max:=x;
a[x]:=1; 为什么这里要这样?
read(y);
i:=max;
while (a[i]<>y)and(i>y) do 这里的意思是什么?
begin
dec(i);
if a[i]=1 then inc(s); 这里呢?
end;
writeln(s+1);
end.
另外,我自己编了个程序但效率不够高,怎样提高啊?
begin
readln(n);
for i:=1 to n do
read(a[i]);
readln(power);
for i:=1 to n do
for j:=i+1 to n do
begin
if a[i]=a[j] then a[i]:=0;
end;
for i:=1 to n do
if a[i]<>0 then
begin
if a[i]>power then inc(s);
end;
writeln(s+1);
end.

第1个回答  推荐于2016-07-15
看别人的程序,一个字--累 !!!

自己编一个:
var
n:integer;
a:array[1..1001] of integer;{存放成绩}
no:array[1..1000] of integer;{名次数组}
i,j,t:integer;
x:integer;
begin
readln(n);
for i:=1 to n do read(a[i]);
{排序}
for i:=1 to n-1 do for j:=i+1 to n do
if a[i]<a[j] then begin
t:=a[i]; a[i]:=a[j]; a[j]:=t;
end;
{名次数组赋值}
for i:=1 to n do no[i]:=i;
{同名次处理}
for i:=2 to n do if a[i]=a[i-1] then no[i]:=no[i-1];
{请输入分数}
write('请输入分数=':11);readln(x);
{确定名次并输出}
for i:=1 to n do if x=a[i] then writeln('名次=',no[i]);
end.
==================================================
最优化:
var
n:integer;
a:array[1..1001] of integer;{存放成绩}
i,no:integer;
x:integer;
find:boolean;
begin
readln(n);
for i:=1 to n do read(a[i]);
write('请输入分数=');readln(x);
fand:=false;
no:=0;
for i:=1 to n do if x=a[i] then begin find:=true; break; end;
if find then begin
for i:=1 to n do if a[i]>x then inc(no);
writeln('名次=',no+1);
end;
end.追问

你的最优化错了啊、、、

追答

只是求名次最简单:
1、判断输入的分数在不在成绩数组?
2、若在,计算大于这个分数的次数,再加一就是它的名次。若不在,则直接结束程序!

你看懂了吗!

追问

但是它说相同分数的按同名次计算啊。
例如输出
7
30 50 60 80 20 50 60
50
应该输出3啊。但是你这个输出了4.
并且如果你这个程序如果那个第三行假如输入了(例如上面),但假如第二行里面没有50的话,他就不会输出啊。。。。怎么办???

追答

1、80
2~3、60
4~5、50
当然50对应的名次为4。

没有50就表示分数列表中没有这个人,当然就没有名次了。
===========================================
var
n:integer;
a:array[1..1001] of integer;{存放成绩}
i,no:integer;
x:integer;
find:boolean;
begin
readln(n);
for i:=1 to n do read(a[i]);
write('请输入分数=');readln(x);
find:=false;
no:=0;
for i:=1 to n do if x=a[i] then begin find:=true; break; end;
if find then begin
for i:=1 to n do if a[i]>x then inc(no);
writeln('名次=',no+1);
end;
end.

追问

但是那里真的是3啊,同分数的是按一个名次计算,且只算一次。

追答

排名次是从高到低 !!! 不是从低到高 !

追问

哎呀,我的意思是说80分第一,两个60第二 两个50分的第三,OK?同分的是按一个名次计算啊!

追答

var
n:integer;
a:array[1..1001] of integer;{存放成绩}
aa:array[1..1000] of integer;{从大到小存放非重复分数}
same:array[1..1000] of boolean; {记录相同分数的位置状态数组}
i,j,t:integer;
k,x:integer;
begin
readln(n);
for i:=1 to n do read(a[i]);
{排序}
for i:=1 to n-1 do for j:=i+1 to n do
if a[i]<a[j] then begin
t:=a[i]; a[i]:=a[j]; a[j]:=t;
end;
{记录相同分数的位置状态数组赋初值}
for i:=1 to n do begin same[i]:=false; end;
{同名次处理}
for i:=2 to n do if a[i]=a[i-1] then same[i]:=true;
k:=0;
for i:=1 to n do if not same[i] then begin inc(k); aa[k]:=a[i]; end;
{请输入分数}
write('请输入分数=');readln(x);
{确定名次并输出}
for i:=1 to k do if x=aa[i] then writeln('名次=',i);
end.

本回答被提问者采纳
第2个回答  2015-01-13
额(⊙o⊙)…完全看不懂追问

看不懂就别说好吗?

相似回答