VB我不是不会用 是不想给你现成的答案,尤其是不指望你这种能为论坛做出贡献的人,保准拿着答案就跑了,顶多说声谢谢
Pascal(Delphi)语言的 自己看懂之后移植。
第一题:
(*
Project: Amber Standard Sources Library [ASSL]
Author: Amber
Title: Extended GCD
Category: Math - Numberic - Modular - Extended GCD
Version: 1.0
Date: 2006-10-24
Remark: N/A
Tested Problems: N/A
*)
program ASSL_ExtendedGCD(Input, Output);
type
TData = Longint;
function ExtendedGCD(A, B: TData; var X, Y: TData): TData; //A*X+B*Y=GCD(A,B)
begin
if B = 0 then
begin
Result := A;
X := 1;
Y := 0;
end
else
begin
Result := ExtendedGCD(B, A mod B, Y, X);
Dec(Y, A div B * X);
end;
end;
procedure Main;
var
X, Y: TData;
A, B, C: TData;
begin
Readln(A, B); //A, B>0
C := ExtendedGCD(A, B, X, Y);
Writeln(X, '*', A, '+', Y, '*', B, '=', C);
end;
begin
Main;
end.
第二题:
(*
Project: Amber Standard Sources Library [ASSL]
Author: Amber
Title: Naive Prime Test
Category: Math - Numberic - Prime - Naive Prime Test
Version: 1.0
Date: 2006-10-24
Remark: N/A
Tested Problems: Ural 1343 Fairy tale
*)
program ASSL_PrimeTest(Input, Output);
type
TIndex = Longint;
TData = Int64;
function IsPrime(N: TData): Boolean; //Here N > 0
var
A: TIndex;
begin
Result := false;
if N = 1 then Exit;
if not Odd(N) then
begin
if N = 2 then Result := true;
Exit;
end;
A := 3;
while A * A <= N do
begin
if N mod A = 0 then Exit;
Inc(A, 2);
end;
Result := true;
end;
begin
end.
(求素数的顺便给你)
(*
Project: Amber Standard Sources Library [ASSL]
Author: Amber
Title: Prime Filter
Category: Math - Numberic - Prime - Prime Filter
Version: 1.0
Date: 2006-10-24
Remark: N/A
Tested Problems: N/A
*)
program ASSL_PrimeFilter(Input, Output);
const
MaxRange = 10000;
MaxPrimeNum = 2000;
type
TIndex = Longint;
TFilter = array [1..MaxRange] of Boolean;
TPrime = array [1..MaxPrimeNum] of TIndex;
var
Range: TIndex;
Filter: TFilter;
PrimeNum: TIndex;
Prime: TPrime;
procedure PrimeFilter;
var
i, j: TIndex;
begin
FillChar(Filter, SizeOf(Filter), true);
Filter[1] := false;
PrimeNum := 0;
for i := 2 to Range do
if Filter[i] then
begin
Inc(PrimeNum);
Prime[PrimeNum] := i;
for j := i to Range div i do
Filter[i * j] := false;
end;
end;
procedure Main;
var
i: TIndex;
begin
Range := MaxRange;
PrimeFilter;
for i := 1 to PrimeNum do
Write(Prime[i], ' ');
end;
begin
Main;
end.
数列那题参考一下这个吧
http://zhidao.baidu.com/question/25768000.html
不过注意 时间上有很大优化余地。