USACO 1.1 我的题解和程序
http://hi.baidu.com/mfs666/blog/item/68347ef42000f7dcf3d38577.html
USACO这个东西,有很多大牛已经“通关”了,还有的大牛有三天做了两大章的事迹,像我这样的小菜,就慢慢从1.1往后来吧
1.1.1 ride
这个是第一题,纯水,但是因为不知道usaco对输出的回车符敏感,必须用writeln,所以交了好多次才AC,唯一注意的是关于乘的变量别忘了初始化成1
{
ID: mfs.dev2
PROG: ride
LANG: PASCAL
}
Program ride;
var
fin, fout: text;
a,b,i,t:longint;
s:string;
begin
Assign(fin, 'ride.in'); Reset(fin);
Assign(fout, 'ride.out'); Rewrite(fout);
a:=1;b:=1;
readln(fin,s);
for i:=1 to length(s) do
a:=(ord(s[i])-64)*a;
read(fin,s);
for i:=1 to length(s) do
b:=(ord(s[i])-64)*b;
if (a mod 47)=(b mod 47) then
writeln(fout,'GO')
else
writeln(fout,'STAY');
Close(fout);
end.
1.1.2 gift1
纯水,看清题义,读入以后取模算一下就完了,一次AC{
ID: mfs.dev2
PROG: gift1
LANG: PASCAL
}
Program gift1;
var
mo,ng,np,i,j,k,f,b:integer;
t:string;
name:array[0..10] of string;
r,g:array[0..10] of integer;
begin
assign(input,'gift1.in');
assign(output,'gift1.out');
reset(input);
rewrite(output);
readln(np);
for i:=1 to np do
readln(name[i]);
for i:=1 to np do begin
readln(t);
for j:=1 to np do
if t=name[j] then
f:=j;
readln(mo,ng);
if ng<>0 then begin
g[f]:=mo-(mo mod ng);
b:=mo div ng;
end;
for j:=1 to ng do begin
readln(t);
for k:=1 to np do
if t=name[k] then
f:=k;
inc(r[f],b);
end;
end;
for i:=1 to np do
writeln(name[i],' ',r[i]-g[i]);
close(input);
close(output);
end.
1.1.3 friday
以时间线模拟就行了,说是不要提前做好数据,所以我尽量不用任何已知的变量,写了个函数计算一天加上n天是星期几。。。一次AC
{
ID: mfs.dev2
PROG: friday
LANG: PASCAL
}
program friday;
var
a,i,j,k,n,f,tt:integer;
r:array[1..7] of integer;
function p(x,b:integer):integer;
var t:integer;
begin
t:=b mod 7;
p:=x+t;
if p>7 then
p:=p mod 7
end;
begin
assign(input,'friday.in');
assign(output,'friday.out');
reset(input);rewrite(output);
readln(n);
a:=1;
for i:=1900 to 1900+n-1 do begin
f:=0;
if (i mod 400)=0 then
f:=1
else
if ((i mod 100)<>0) and ((i mod 4)=0) then
f:=1;
for j:=1 to 12 do begin
tt:=p(a,12);
inc(r[tt]);
if (j=4) or (j=6) or (j=9) or (j=11) then
a:=p(a,30);
if (j=1) or (j=3) or (j=5) or (j=7) or (j=8) or (j=10) or (j=12) then
a:=p(a,31);
if j=2 then begin
if f=1 then
a:=p(a,29);
if f=0 then
a:=p(a,28);
end;
end;
end;
for i:=6 to 7 do
write(r[i],' ');
for i:=1 to 4 do
write(r[i],' ');
writeln(r[5]);
close(output);
end.
1.1.4 beads
这个题的题义翻译的有点问题,但是基本上按他说的枚举就行了,没有用题解说的复制串的方法,而是写了一个函数来求位置(既到端自动跳到另一端),注意数据范围,不能用string存。特殊情况是颜色全同(记录是否收集过),第一个是万能珠(题义其实是白色不算一种单独的颜色,把它变成第一个遇到的颜色),一次AC
{
ID: mfs.dev2
PROG: beads
LANG: PASCAL
}
program beads;
var
s:array[0..350] of char;
t:array[0..350] of integer;
f,max,n,i,l,r,st,fl,c:integer;
fr:char;
function get(x,a:integer):integer;
begin
if a=1 then begin
get:=x+1;
if get>n then
get:=1;
end;
if a=0 then begin
get:=x-1;
if get<1 then
get:=n;
end;
end;
begin
assign(input,'beads.in');
assign(output,'beads.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do
read(s[i]);
for i:=1 to n do begin
r:=i;l:=i-1;f:=0;st:=0;c:=0;
fillchar(t,sizeof(t),0);
if i=1 then l:=n;
fr:=s[r];
while (t[r]=0) and (st=0) do begin
if s[r]='w' then begin
inc(c);t[r]:=1;r:=get(r,1);continue;
end;
if s[r]<>fr then begin
if fr='w' then begin
fr:=s[r];
inc(c);t[r]:=1;r:=get(r,1);
continue;
end;
st:=1;
continue;
end;
inc(c);t[r]:=1;r:=get(r,1);
end;
fr:=s[l];st:=0;
while (t[l]=0) and (st=0) do begin
if s[l]='w' then begin
inc(c);t[l]:=1;l:=get(l,0);continue;
end;
if s[l]<>fr then begin
if fr='w' then begin
fr:=s[l];
inc(c);t[l]:=1;l:=get(l,0);
continue;
end;
st:=1;
continue;
end;
inc(c);t[l]:=1;l:=get(l,0);
end;
if c>max then max:=c;
end;
writeln(max);
close(output);
end.

浙公网安备 33010602011771号