var i,j,k,n:longint;
begin
write(’N=’);
readln(n);
k:=1;
while n>k do
begin
n:=n-k;
k:=k+1;
end;
if k mod 2=0 then writeln(n,’’,k-n+1)
else writeln(k-n+1,’’,n);
end.
2、回文數
本題也很簡單,只是考查了壹些基本編程能力,沒有什麽難度可言。只要細心,本題的分是可以輕松拿到手的。這裏數采用字符串表示(其他方法當然也可以),因為處理方便。N進制的加法是本題的重頭戲,處理如下:1)字符->數字,可以用數組來簡化程序,即digit和chars數組2)做加法,保留各位數字和進位,就想做高精度加法壹樣。g是進位。
const
step:integer=0;
chars:array[0..15] of char=('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
var
digit:array[char] of integer;
i,n,g:integer; m,s:string;
ok:boolean;
begin
for i:=0 to 9 do digit[char(ord('0')+i)]:=i;
for i:=0 to 5 do digit[char(ord('A')+i)]:=i+10;
write('n=');
readln(n);
write('m=');
readln(s);
for i:=1 to length(s) do
s[i]:=upcase(s[i]);
repeat
ok:=true;
for i:=1 to length(s) div 2 do
if s[i]<>s[length(s)+1-i] then ok:=false;
if ok then break;
inc(step);
m:=s; g:=0;
for i:=length(m) downto 1 do
begin
s[i]:=chars[(digit[m[i]]+digit[m[length(m)+1-i]]+g) mod n];
g:=(digit[m[i]]+digit[m[length(m)+1-i]]+g) div n;
end;
if g>0 then s:=chars[g]+s;
until step>=30;
if ok then w
riteln('STEP=',step) else
writeln('Impossible');
end.
3、旅行家的預算
有兩種方法:
1:
program trip(input,output);
const max=1000;
var i,n:longint;
c,d2:real;
p,d,consume:array[0..max] of real;
function minp(b,e:longint):longint;{在b站到e站之間從後往前找油價最低的站}
var i,k:longint;
tempminp:real;
begin
tempminp:=p[e]; k:=e;
for i:=e-1 downto b do
if p[i]<tempminp then
begin tempminp:=p[i]; k:=i end;
minp:=k
end;
function money(start,stop:longint;rest:real):real;
var k:longint;
begin
if stop-start=1
then money:=((d[stop]-d[start])/d2-rest)*p[start]
else begin
k:=minp(start,stop-1);
if k<>start{油價最低的加油站不是起點站}
then money:=money(start,k,rest)+money(k,stop,0)
else if d[stop]-d[start]<=d2*c{在起點加滿油能直接到達該段終點}
then money:=((d[stop]-d[start])/d2-rest)*p[start]
else begin
k:=minp(start+1,stop-1);
if d[k]-d[start]<=d2*c
then{在起點加滿油能到達加油站k}
money:=(c-rest)*p[start]+money(k,stop,c-(d[k]-d[start])/d2)
{在起點處加滿油箱***加油c-rest升,到達加油站k用去(d[k]-d[start])/d2}
else money:=money(start,k,rest)+money(k,stop,0)
end
end
end;
begin
reset(input);
rewrite(output);}
readln(d[0],c,d2,p[0],n);
d[n+1]:=d[0];
for i:=1 to n do readln(d[i],p[i]);
d[0]:=0;
for i:=n downto 0 do consume[i]:=(d[i+1]-d[i])/d2;
for i:=0 to n do
if consume[i]>c then
begin writeln('No Solution'); close(output);halt end;
writeln(money(0,n+1,0):0:2); {起點站編號為0,終點站編號為n+1}
end.
2:
program trip2(input,output);
const max=1000;
type recordtype=record price,content:real end;
var i,j,n,point,tail:longint;
content,change,distance2,money,use:real;
price,distance,consume:array[0..max] of real;
oil:array [0..max] of recordtype;
begin
reset(input);
rewrite(output);}
readln(distance[0],content,distance2,price[0],n);
distance[n+1]:=distance[0];
for i:=1 to n do readln(distance[i],price[i]);
distance[0]:=0;
for i:=n downto 0 do consume[i]:=(distance[i+1]-distance[i])/distance2;
{計算相鄰兩站間的油耗}
for i:=0 to n do
if consume[i]>content then
begin writeln('No Solution'); halt end;
money:=0; tail:=1; change:=0;
oil[tail].price:=price[0]*2;
oil[tail].content:=content;
for i:=0 to n do
begin
point:=tail;
while (point>=1) and (oil[point].price>=price[i]) do
begin
change:=change+oil[point].content;
point:=point-1
end;
tail:=point+1;
oil[tail].price:=price[i];
oil[tail].content:=change;
use:=consume[i]; point:=1;
while (use>1e-6) and (point<=tail) do
if use>=oil[point].content
then begin
use:=use-oil[point].content;
money:=money+oil[point].content*oil[point].price;
point:=point+1
end
else begin
oil[point].content:=oil[point].content-use;
money:=money+use*oil[point].price;
use:=0
end;
for j:=point to tail do oil[j-point+1]:=oil[j];
tail:=tail-point+1;
change:=consume[i]
end;
writeln(money:0:2);
end.
有問題追問