问题描述
我是一个初学者,目前我正在享受竞争性编程作为学习的一种方式。 我的任务是输入单词列表并将其加密输出。 我的算法运行良好,但是我的问题出在输入名称列表的方式上。 我不知道是否可以放置问题的链接,但是我必须输入一次:
4
word
localization
internationalization
pneumonoultramicroscopicsilicovolcanoconiosis
并获得此结果
word
l10n
i18n
p43s
但是我唯一管理的是通过一个一个地给出名称,是否有一种方法可以一次输入所有内容而无需考虑一个实体字符串呢?
这是我的脚本:
program A71;
uses wincrt;
var
word: String;
function correctname(word : String): Boolean;
var
i : Integer;
begin
correctname:= true;
for i:=1 to length(word) do
if not (upcase(word[i]) in ['A'..'Z']) then
correctname:= false;
end;
function Cryptname(var word : String): String;
var
wordcrypt: String;
begin
wordcrypt:= '';
str(length(word) - 2,wordcrypt);
Cryptname:= word[1] + wordcrypt + word[length(word)];
end;
begin
repeat
repeat
readln(word);
until correctname(word);
if correctname(word) and (length(word) > 10) then
writeln(Cryptname(word))
//writeln(word);
else
writeln(word);
//readkey();
until false;
end.
**更新:**
program A71;
uses wincrt;
type
tab = array[1..100] of String;
var
word: tab;
correctwords,n,i : Integer;
function correctname(word : tab; correctwords : Integer): Boolean;
var
i : Integer;
begin
correctname:= true;
i:= 0;
//for i:=1 to length(word[correctwords]) do
repeat
i:=i+1;
if not (upcase(word[correctwords][i]) in ['A'..'Z']) then
correctname:= false;
until not correctname or (i = length(word[correctwords]));
end;
procedure Cryptname(var word : tab; correctwords : Integer);
var
wordcrypt: String;
begin
//wordcrypt:= '';
str(length(word[correctwords]) - 2,wordcrypt);
word[correctwords]:= word[correctwords][1] + wordcrypt + word[correctwords][length(word)];
end;
begin
correctwords:=0;
readln(n);
repeat
readln(word[correctwords]);
if correctname(word,correctwords) then
correctwords:= correctwords+1;
until correctwords = n;
for i:= 1 to correctwords do
begin
if correctname(word,correctwords) and (length(word[correctwords]) > 10) then
begin
Cryptname(word,correctwords);
writeln(word[correctwords]);
end
else
writeln(word[correctwords]);
end;
readkey();
end.
现在我的代码直接在第二个输入处崩溃。
更新,我以这种方式解决它:
program A71;
uses wincrt;
type
tab = array[1..100] of String;
var
word: tab;
correctwords,i : Integer;
tester : String;
function correctname(tester : String): Boolean;
var
i : Integer;
begin
correctname:= true;
i:= 0;
//for i:=1 to length(word[correctwords]) do
repeat
i:=i+1;
if not (upcase(tester[i]) in ['A'..'Z']) then
correctname:= false;
until not correctname or (i = length(tester));
end;
procedure Cryptname(var wordcrypt : String);
var
wordcryptaux: String;
begin
wordcryptaux:= wordcrypt;
str(length(wordcrypt) - 2,wordcrypt);
wordcrypt:= wordcryptaux[1] + wordcrypt + wordcryptaux[length(wordcryptaux)];
end;
begin
correctwords:=0;
readln(n);
repeat
readln(tester);
if correctname(tester) then
correctwords:= correctwords+1;
word[correctwords]:= tester;
until correctwords = n;
for i:= 1 to correctwords do
begin
if correctname(word[i]) and (length(word[i]) > 10) then
begin
Cryptname(word[i]);
writeln(word[i]);
end
else
writeln(word[i]);
end;
readkey();
end.
非常感谢您!
解决方法
我修复并改进了您的代码。注意主程序中的if
只有一行,但是缩进使您认为它只有两行。我也解决了这个问题。
program A71;
uses wincrt;
type
tab = array[1..100] of String;
var
word: tab;
correctwords,n,i: Integer;
tester: String;
function correctname(inputword : String): Boolean;
var
i: Integer;
begin
for i:=1 to length(inputword[correctwords]) do
begin
if not (upcase(inputword[i]) in ['A'..'Z']) then
Exit(false);
end;
Exit(true);
end;
procedure Cryptname(var wordcrypt : String);
var
wordcryptaux: String;
begin
wordcryptaux:= wordcrypt;
str(length(wordcrypt) - 2,wordcrypt);
wordcrypt := wordcryptaux[1] + wordcrypt + wordcryptaux[length(wordcryptaux)];
end;
begin
correctwords:=0;
readln(n);
repeat
readln(tester);
if correctname(tester) then begin
Inc(correctwords);
word[correctwords]:= tester;
end;
until correctwords = n;
for i:=1 to correctwords do
begin
if correctname(word[i]) and (length(word[i]) > 10) then
Cryptname(word[i]);
writeln(word[i]);
end;
readkey();
end.