一个快速入门的秘诀就是“背”,这个“背”字可以大做文章,想当年我就靠这种方法迅速建立起初步信息学知识体系的。
这是《信息学初学者之家》站长、曾多次参加AcM竞赛并获优异成绩的tenshi所发的切身体会。因此,我们要求,下面的算法不仅要读得懂,而且要熟记于心,这样,你才能在程序设计中灵活地运用……
1、高精度加法
高精度加法程序如下:
Program HighPrecision1_Plus
const
fn_inp='hp1.inp'
fn_out='hp1.out'
maxlen=100 { 数的最大长度 }
type
hp=record
len:integer{ 数的长度 }
s:array[1..maxlen] of integer
{ s[1]为最低位,s[len]为最高位 }
end
var
x:array[1..2] of hp
y:hp{ x:输入 y:输出 }
procedure PrintHP(const p:hp)
var i:integer
begin
for i:=p.len downto 1 do write(p.s[i])
end
procedure init {初始化}
var
st:string
j,i:integer
begin
assign(input,fn_inp)
reset(input)
for j:=1 to 2 do
begin
readln(st)
x[j].len:=length(st)
for i:=1 to x[j].len do { 将字符串转换到HP }
x[j].s[i]:=ord(st[x[j].len+1-i])-ord('0')
end
close(input)
end
procedure Plus(a,b:hpvar c:hp){ c:=a+b }
var i,len:integer
begin
fillchar(c,sizeof(c),0)
if a.len>b.len then len:=a.len { 从a,b中取较大长度 }
else len:=b.len
for i:=1 to len do { 从低到高相加 }
begin
inc(c.s[i],a.s[i]+b.s[i])
if c.s[i]>=10 then
begin
dec(c.s[i],10)
inc(c.s[i+1]){ 加1到高位 }
end
end
if c.s[len+1]>0 then inc(len)
c.len:=len
end
procedure out {打印输出}
begin
assign(output,fn_out)
rewrite(output)
PrintHP(y)
writeln
close(output)
end
begin
init
Plus(x[1],x[2],y)
out
end.
2、 高精度减法
高精度减法程序如下:
Program HighPrecision2_Subtract
const
fn_inp='hp2.inp'
fn_out='hp2.out'
maxlen=100 { 数的最大长度 }
type
hp=record
len:integer{ 数的长度 }
s:array[1..maxlen] of integer
{ s[1]为最低位,s[len]为最高位 }
end
var
x:array[1..2] of hp
y:hp{ x:输入 y:输出 }
positive:boolean
procedure PrintHP(const p:hp)
var i:integer
begin
for i:=p.len downto 1 do write(p.s[i])
end
procedure init
var
st:string
j,i:integer
begin
assign(input,fn_inp)
reset(input)
for j:=1 to 2 do
begin
readln(st)
x[j].len:=length(st)
for i:=1 to x[j].len do { change string to HP }
x[j].s[i]:=ord(st[x[j].len+1-i])-ord('0')
end
close(input)
end
procedure Subtract(a,b:hpvar c:hp){ c:=a-b, suppose a>=b }
var i,len:integer
begin
fillchar(c,sizeof(c),0)
if a.len>b.len then len:=a.len { get the bigger length of a,b }
else len:=b.len
for i:=1 to len do { subtract from low to high }
begin
inc(c.s[i],a.s[i]-b.s[i])
if c.s[i]<0 then
begin
inc(c.s[i],10)
dec(c.s[i+1]){ add 1 to a higher position }
end
end
while(len>1) and (c.s[len]=0) do dec(len)
c.len:=len
end
function Compare(const a,b:hp):integer
{
1 if a>b
0 if a=b
-1 if a <b
}
var len:integer
begin
if a.len>b.len then len:=a.len { get the bigger length of a,b }
else len:=b.len
while(len>0) and (a.s[len]=b.s[len]) do dec(len)
{ find a position which have a different digit }
if len=0 then compare:=0 { no difference }
else compare:=a.s[len]-b.s[len]
end
procedure main
begin
if compare(x[1],x[2])<0 then positive:=false
else positive:=true
if positive then Subtract(x[1],x[2],y)
else Subtract(x[2],x[1],y)
end
procedure out
begin
assign(output,fn_out)
rewrite(output)
if not positive then write('-')
PrintHP(y)
writeln
close
(output)
end
begin
init
main
out
end.
3、高精度乘法
1).高精度乘单精度(1位数)
程序如下:
Program HighPrecision3_Multiply1
const
fn_inp='hp3.inp'
fn_out='hp3.out'
maxlen=100 { 数的最大长度 }
type
hp=record
len:integer{ 数的长度 }
s:array[1..maxlen] of integer
{ s[1]为最低位,s[len]为最高位 }
end
var
x:array[1..2] of hp
y:hp{ x:输入 y:输出 }
procedure PrintHP(const p:hp)
var i:integer
begin
for i:=p.len downto 1 do write(p.s[i])
end
procedure init
var
st:string
i:integer
begin
assign(input,fn_inp)
reset(input)
readln(st)
x.len:=length(st)
for i:=1 to x.len do { change string to HP }
x.s[i]:=ord(st[x.len+1-i])-ord('0')
readln(z)
close(input)
end
procedure Multiply(a:hpb:integervar c:hp){ c:=a*b }
var i,len:integer
begin
fillchar(c,sizeof(c),0)
len:=a.len
for i:=1 to len do
begin
inc(c.s[i],a.s[i]*b)
inc(c.s[i+1],c.s[i] div 10)
c.s[i]:=c.s[i] mod 10
end
inc(len)
while(c.s[len]>=10) do
begin
inc(c.s[len+1],c.s[len] div 10)
c.s[len]=c.s[len] mod 10
inc(len)
end
while(len>1) and (c.s[len]=0) do dec(len)
c.len:=len
end
procedure main
begin
Multiply(x,z,y)
end
procedure out
begin
assign(output,fn_out)
rewrite(output)
PrintHP(y)
writeln
close(output)
end
begin
init
main
out
end.
2).高精度乘一个整型数据(integer)
只需要将上述程序的hp类型定义如下即可:
type
hp=record
len:integer { length of the number }
s:array[1..maxlen] of longint
{ s[1] is the lowest position
s[len] is the highest position }
end
3).高精度乘高精度
程序如下:
Program HighPrecision4_Multiply2
const
fn_inp='hp4.inp'
fn_out='hp4.out'
maxlen=100 { max length of the number }
type
hp=record
len:integer{ length of the number }
s:array[1..maxlen] of integer
{ s[1] is the lowest position
s[len] is the highest position }
end
var
x:array[1..2] of hp
y:hp{ x:input y:output }
procedure PrintHP(const p:hp)
var i:integer
begin
for i:=p.len downto 1 do write(p.s[i])
end
procedure init
var
st:string
j,i:integer
begin
assign(input,fn_inp)
reset(input)
for j:=1 to 2 do
begin
readln(st)
x[j].len:=length(st)
for i:=1 to x[j].len do { change string to HP }
x[j].s[i]:=ord(st[x[j].len+1-i])-ord('0')
end
close(input)
end
procedure Multiply(a,b:hpvar c:hp){ c:=a+b }
var i,j,len:integer
begin
fillchar(c,sizeof(c),0)
for i:=1 to a.len do
for j:=1 to b.len do
begin
inc(c.s[i+j-1],a.s[i]*b.s[j])
inc(c.s[i+j],c.s[i+j-1] div 10)
c.s[i+j-1]:=c.s[i+j-1] mod 10
end
len:=a.len+b.len+1
{
the product of a number with i digits and a number with j digits
can only have at most i+j+1 digits
}
while(len>1)and(c.s[len]=0) do dec(len)
c.len:=len
end
procedure main
begin
Multiply(x[1],x[2],y)
end
procedure out
begin
assign(output,fn_out)
rewrite(output)
PrintHP(y)
writeln
close(output)
end
begin
init
main
out
end.
4、 高精度除法
1).高精度除以整型数据(integer)
程序如下:
Program HighPrecision3_Multiply1
const
fn_inp='hp5.inp'
fn_out='hp5.out'
maxlen=100 { max length of the number }
type
hp=record
len:integer{ length of the number }
s:array[1..maxlen] of integer
{ s[1] is the lowest position
s[len] is the highest position }
end
var
x,y:hp
z,w:integer
procedure PrintHP(const p:hp)
var i:integer
begin
for i:=p.len downto 1 do write(p.s[i])
end
procedure init
var
st:string
i:integer
begin
assign(input,fn_inp)
reset(input)
readln(st)
x.len:=length(st)
for i:=1 to x.len do { change string to HP }
x.s[i]:=ord(st[x.len+1-i])-ord('0')
readln(z)
close(input)
end
procedure Divide(a:hpb:integervar c:hpvar d:integer)
{ c:=a div b d:=a mod b }
var i,len:integer
begin
fillchar(c,sizeof(c),0)
len:=a.len
d:=0
for i:=len downto 1 do { from high to low }
begin
d:=d*10+a.s[i]
c.s[i]:=d div b
d:=d mod b
end
while(len>1) and (c.s[len]=0) do dec(len)
c.len:=len
end
procedure main
begin
Divide(x,z,y,w)
end
procedure out
begin
assign(output,fn_out)
rewrite(output)
PrintHP(y)
writeln
writeln(w)
close(output)
end
begin
init
main
out
end.
2).高精度除以高精度
程序如下:
Program HighPrecision4_Multiply2
const
fn_inp='hp6.inp'
fn_out='hp6.out'
maxlen=100 { max length of the number }
type
hp=record
len:integer{ length of the number }
s:array[1..maxlen] of integer
{ s[1] is the lowest position
s[len] is the highest position }
end
var
x:array[1..2] of hp
y,w:hp{ x:input y:output }
procedure PrintHP(const p:hp)
var i:integer
begin
for i:=p.len downto 1 do write(p.s[i])
end
procedure init
var
st:string
j,i:integer
begin
assign(input,fn_inp)
reset(input)
for j:=1 to 2 do
begin
readln(st)
x[j].len:=length(st)
for i:=1 to x[j].len do { change string to HP }
x[j].s[i]:=ord(st[x[j].len+1-i])-ord('0')
end
close(input)
end
procedure Subtract(a,b:hpvar c:hp){ c:=a-b, suppose a>=b }
var i,len:integer
begin
fillchar(c,sizeof(c),0)
if a.len>b.len then len:=a.len { get the bigger length of a,b }
else len:=b.len
for i:=1 to len do { subtract from low to high }
begin
inc(c.s[i],a.s[i]-b.s[i])
if c.s[i]<0 then
begin
inc(c.s[i],10)
dec(c.s[i+1]){ add 1 to a higher position }
end
end
while(len>1) and (c.s[len]=0) do dec(len)
c.len:=len
end
function Compare(const a,b:hp):integer
{
1 if a>b
0 if a=b
-1 if a <b
}
var len:integer
begin
if a.len>b.len then len:=a.len { get the bigger length of a,b }
else len:=b.len
while(len>0) and (a.s[len]=b.s[len]) do dec(len)
{ find a position which have a different digit }
if len=0 then compare:=0 { no difference }
else compare:=a.s[len]-b.s[len]
end
procedure Multiply10(var a:hp){ a:=a*10 }
var i:Integer
begin
for i:=a.len downto 1 do
a.s[i+1]:=a.s[i]
a.s[1]:=0
inc(a.len)
while(a.len>1) and (a.s[a.len]=0) do dec(a.len)
end
procedure Divide(a,b:hpvar c,d:hp){ c:=a div b d:=a mod b }
var i,j,len:integer
begin
fillchar(c,sizeof(c),0)
len:=a.len
fillchar(d,sizeof(d),0)
d.len:=1
for i:=len downto 1 do
begin
Multiply10(d)
d.s[1]:=a.s[i]{ d:=d*10+a.s[i] }
{ c.s[i]:=d div b d:=d mod b}
{ while(d>=b) do begin d:=d-binc(c.s[i]) end }
while(compare(d,b)>=0) do
begin
Subtract(d,b,d)
inc(c.s[i])
end
end
while(len>1)and(c.s[len]=0) do dec(len)
c.len:=len
end
procedure main
begin
Divide(x[1],x[2],y,w)
end
procedure out
begin
assign(output,fn_out)
rewrite(output)
PrintHP(y)
writeln
PrintHP(w)
writeln
close(output)
end
begin
init
main
out
end.
//程序如下,加法 减法各25道#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void main()
{
int i,a,b,c,score=0
srand((unsigned)time(NULL))
for(i=1i<=50i++)
{ a=rand()%89+10
b=rand()%89+10
c=rand()%198
if(i<=25)
{
printf("%d+%d=%d\n",a,b,c)
if(c==a+b)
{printf("正确!\n")
score+=2
}
else
printf("错误!\n")
}
else
{
printf("%d-%d=%d\n",a,b,c)
if(c==a-b)
{printf("正确!\n")
score+=2
}
else
printf("错误!\n")
}
}
printf("总分为:%d\n",score)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)