石宝杀了多少猛将:pascal有关问题
例:输入:aaaabbbccc
输出:a:4
b:3
c:3
program letter_count;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
s: string;
count: array['a'..'z'] of integer;
i: integer;
c: char;
begin
fillchar(count, sizeof(count), 0);
readln(s);
for i := 1 to length(s)
do
if (s[i] = '.')
then
break
else if (s[i] in ['a'..'z'])
then
inc(count[s[i]]);
for c := 'a' to 'z'
do
if (count[c] <> 0)
then
writeln(c,':',count[c]);
end.
program xjo;
var
s:string;
a:array ['a'..'z'] of integer;
l,i:integer;
j:char;
begin
readln(s);
l:=length(s);
fillchar(a,sizeof(a),0);
for i:=1 to l-1 do
a[s[i]]:=a[s[i]]+1;
for j:='a' to 'z' do
if a[j]<>0 then
writeln(j,':',a[j]);
end.
拿回去编译以下,如果数据正常,那么结果应该没有错。相信一个编程竞赛生的话吧。
核心代码:
begin
read(str);
for i:=1 to len(str) do
begin
if str[i]='a' then inc(numa);
if str[i]='b' then inc(numb);
if str[i]='c' then inc(numc);
end;
writeln('a:',numa);
writeln('b:',numb);
writeln('c:',numc);
end.
楼上的兄弟你编的不错 不过着个问题可以用集合优化
相信一个编程竞赛生的话吧。