❶ 求汕尾海豐麻將演算法大全 越全越好
好像是這樣,雞糊2支1,自摸4支4,小一色對對糊,糊就6支3,自摸8支8,大一色糊10支5,自摸12支12,十三幺 天湖 地胡 大三元,大四喜。糊2底1,自摸2底2。小三元、小四喜糊就是12支6,自摸就是14支14。如果碰.東南西北中發白或者自己摸三隻,一個風位算一方,如果剛剛好是自己的風位算兩方,碼字太難教了
❷ C語言演算法有哪些 並舉例和分析
演算法大全(C,C++)
一、 數論演算法
1.求兩數的最大公約數
function gcd(a,b:integer):integer;
begin
if b=0 then gcd:=a
else gcd:=gcd (b,a mod b);
end ;
2.求兩數的最小公倍數
function lcm(a,b:integer):integer;
begin
if a<b then swap(a,b);
lcm:=a;
while lcm mod b>0 do inc(lcm,a);
end;
3.素數的求法
A.小范圍內判斷一個數是否為質數:
function prime (n: integer): Boolean;
var I: integer;
begin
for I:=2 to trunc(sqrt(n)) do
if n mod I=0 then begin
prime:=false; exit;
end;
prime:=true;
end;
B.判斷longint范圍內的數是否為素數(包含求50000以內的素數表):
procere getprime;
var
i,j:longint;
p:array[1..50000] of boolean;
begin
fillchar(p,sizeof(p),true);
p[1]:=false;
i:=2;
while i<50000 do begin
if p[i] then begin
j:=i*2;
while j<50000 do begin
p[j]:=false;
inc(j,i);
end;
end;
inc(i);
end;
l:=0;
for i:=1 to 50000 do
if p[i] then begin
inc(l);pr[l]:=i;
end;
end;{getprime}
function prime(x:longint):integer;
var i:integer;
begin
prime:=false;
for i:=1 to l do
if pr[i]>=x then break
else if x mod pr[i]=0 then exit;
prime:=true;
end;{prime}
二、圖論演算法
1.最小生成樹
A.Prim演算法:
procere prim(v0:integer);
var
lowcost,closest:array[1..maxn] of integer;
i,j,k,min:integer;
begin
for i:=1 to n do begin
lowcost[i]:=cost[v0,i];
closest[i]:=v0;
end;
for i:=1 to n-1 do begin
{尋找離生成樹最近的未加入頂點k}
min:=maxlongint;
for j:=1 to n do
if (lowcost[j]<min) and (lowcost[j]<>0) then begin
min:=lowcost[j];
k:=j;
end;
lowcost[k]:=0; {將頂點k加入生成樹}
{生成樹中增加一條新的邊k到closest[k]}
{修正各點的lowcost和closest值}
for j:=1 to n do
if cost[k,j]<lwocost[j] then begin
lowcost[j]:=cost[k,j];
closest[j]:=k;
end;
end;
end;{prim}
B.Kruskal演算法:(貪心)
按權值遞增順序刪去圖中的邊,若不形成迴路則將此邊加入最小生成樹。
function find(v:integer):integer; {返回頂點v所在的集合}
var i:integer;
begin
i:=1;
while (i<=n) and (not v in vset[i]) do inc(i);
if i<=n then find:=i else find:=0;
end;
procere kruskal;
var
tot,i,j:integer;
begin
for i:=1 to n do vset[i]:=[i];{初始化定義n個集合,第I個集合包含一個元素I}
p:=n-1; q:=1; tot:=0; {p為尚待加入的邊數,q為邊集指針}
sort;
{對所有邊按權值遞增排序,存於e[I]中,e[I].v1與e[I].v2為邊I所連接的兩個頂點的序號,e[I].len為第I條邊的長度}
while p>0 do begin
i:=find(e[q].v1);j:=find(e[q].v2);
if i<>j then begin
inc(tot,e[q].len);
vset[i]:=vset[i]+vset[j];vset[j]:=[];
dec(p);
end;
inc(q);
end;
writeln(tot);
end;
2.最短路徑
A.標號法求解單源點最短路徑:
var
a:array[1..maxn,1..maxn] of integer;
b:array[1..maxn] of integer; {b[i]指頂點i到源點的最短路徑}
mark:array[1..maxn] of boolean;
procere bhf;
var
best,best_j:integer;
begin
fillchar(mark,sizeof(mark),false);
mark[1]:=true; b[1]:=0;{1為源點}
repeat
best:=0;
for i:=1 to n do
If mark[i] then {對每一個已計算出最短路徑的點}
for j:=1 to n do
if (not mark[j]) and (a[i,j]>0) then
if (best=0) or (b[i]+a[i,j]<best) then begin
best:=b[i]+a[i,j]; best_j:=j;
end;
if best>0 then begin
b[best_j]:=best;mark[best_j]:=true;
end;
until best=0;
end;{bhf}
B.Floyed演算法求解所有頂點對之間的最短路徑:
procere floyed;
begin
for I:=1 to n do
for j:=1 to n do
if a[I,j]>0 then p[I,j]:=I else p[I,j]:=0; {p[I,j]表示I到j的最短路徑上j的前驅結點}
for k:=1 to n do {枚舉中間結點}
for i:=1 to n do
for j:=1 to n do
if a[i,k]+a[j,k]<a[i,j] then begin
a[i,j]:=a[i,k]+a[k,j];
p[I,j]:=p[k,j];
end;
end;
C. Dijkstra 演算法:
var
a:array[1..maxn,1..maxn] of integer;
b,pre:array[1..maxn] of integer; {pre[i]指最短路徑上I的前驅結點}
mark:array[1..maxn] of boolean;
procere dijkstra(v0:integer);
begin
fillchar(mark,sizeof(mark),false);
for i:=1 to n do begin
d[i]:=a[v0,i];
if d[i]<>0 then pre[i]:=v0 else pre[i]:=0;
end;
mark[v0]:=true;
repeat {每循環一次加入一個離1集合最近的結點並調整其他結點的參數}
min:=maxint; u:=0; {u記錄離1集合最近的結點}
for i:=1 to n do
if (not mark[i]) and (d[i]<min) then begin
u:=i; min:=d[i];
end;
if u<>0 then begin
mark[u]:=true;
for i:=1 to n do
if (not mark[i]) and (a[u,i]+d[u]<d[i]) then begin
d[i]:=a[u,i]+d[u];
pre[i]:=u;
end;
end;
until u=0;
end;
3.計算圖的傳遞閉包
Procere Longlink;
Var
T:array[1..maxn,1..maxn] of boolean;
Begin
Fillchar(t,sizeof(t),false);
For k:=1 to n do
For I:=1 to n do
For j:=1 to n do T[I,j]:=t[I,j] or (t[I,k] and t[k,j]);
End;
4.無向圖的連通分量
A.深度優先
procere dfs ( now,color: integer);
begin
for i:=1 to n do
if a[now,i] and c[i]=0 then begin {對結點I染色}
c[i]:=color;
dfs(I,color);
end;
end;
B 寬度優先(種子染色法)
5.關鍵路徑
幾個定義: 頂點1為源點,n為匯點。
a. 頂點事件最早發生時間Ve[j], Ve [j] = max{ Ve [j] + w[I,j] },其中Ve (1) = 0;
b. 頂點事件最晚發生時間 Vl[j], Vl [j] = min{ Vl[j] – w[I,j] },其中 Vl(n) = Ve(n);
c. 邊活動最早開始時間 Ee[I], 若邊I由<j,k>表示,則Ee[I] = Ve[j];
d. 邊活動最晚開始時間 El[I], 若邊I由<j,k>表示,則El[I] = Vl[k] – w[j,k];
若 Ee[j] = El[j] ,則活動j為關鍵活動,由關鍵活動組成的路徑為關鍵路徑。
求解方法:
a. 從源點起topsort,判斷是否有迴路並計算Ve;
b. 從匯點起topsort,求Vl;
c. 算Ee 和 El;
6.拓撲排序
找入度為0的點,刪去與其相連的所有邊,不斷重復這一過程。
例 尋找一數列,其中任意連續p項之和為正,任意q 項之和為負,若不存在則輸出NO.
7.迴路問題
Euler迴路(DFS)
定義:經過圖的每條邊僅一次的迴路。(充要條件:圖連同且無奇點)
Hamilton迴路
定義:經過圖的每個頂點僅一次的迴路。
一筆畫
充要條件:圖連通且奇點個數為0個或2個。
9.判斷圖中是否有負權迴路 Bellman-ford 演算法
x[I],y[I],t[I]分別表示第I條邊的起點,終點和權。共n個結點和m條邊。
procere bellman-ford
begin
for I:=0 to n-1 do d[I]:=+infinitive;
d[0]:=0;
for I:=1 to n-1 do
for j:=1 to m do {枚舉每一條邊}
if d[x[j]]+t[j]<d[y[j]] then d[y[j]]:=d[x[j]]+t[j];
for I:=1 to m do
if d[x[j]]+t[j]<d[y[j]] then return false else return true;
end;
10.第n最短路徑問題
*第二最短路徑:每舉最短路徑上的每條邊,每次刪除一條,然後求新圖的最短路徑,取這些路徑中最短的一條即為第二最短路徑。
*同理,第n最短路徑可在求解第n-1最短路徑的基礎上求解。
三、背包問題
*部分背包問題可有貪心法求解:計算Pi/Wi
數據結構:
w[i]:第i個背包的重量;
p[i]:第i個背包的價值;
1.0-1背包: 每個背包只能使用一次或有限次(可轉化為一次):
A.求最多可放入的重量。
NOIP2001 裝箱問題
有一個箱子容量為v(正整數,o≤v≤20000),同時有n個物品(o≤n≤30),每個物品有一個體積 (正整數)。要求從 n 個物品中,任取若千個裝入箱內,使箱子的剩餘空間為最小。
l 搜索方法
procere search(k,v:integer); {搜索第k個物品,剩餘空間為v}
var i,j:integer;
begin
if v<best then best:=v;
if v-(s[n]-s[k-1])>=best then exit; {s[n]為前n個物品的重量和}
if k<=n then begin
if v>w[k] then search(k+1,v-w[k]);
search(k+1,v);
end;
end;
l DP
F[I,j]為前i個物品中選擇若干個放入使其體積正好為j的標志,為布爾型。
實現:將最優化問題轉化為判定性問題
f [I, j] = f [ i-1, j-w[i] ] (w[I]<=j<=v) 邊界:f[0,0]:=true.
For I:=1 to n do
For j:=w[I] to v do F[I,j]:=f[I-1,j-w[I]];
優化:當前狀態只與前一階段狀態有關,可降至一維。
F[0]:=true;
For I:=1 to n do begin
F1:=f;
For j:=w[I] to v do
If f[j-w[I]] then f1[j]:=true;
F:=f1;
End;
B.求可以放入的最大價值。
F[I,j] 為容量為I時取前j個背包所能獲得的最大價值。
F [i,j] = max { f [ i – w [ j ], j-1] + p [ j ], f[ i,j-1] }
C.求恰好裝滿的情況數。
DP:
Procere update;
var j,k:integer;
begin
c:=a;
for j:=0 to n do
if a[j]>0 then
if j+now<=n then inc(c[j+now],a[j]);
a:=c;
end;
2.可重復背包
A求最多可放入的重量。
F[I,j]為前i個物品中選擇若干個放入使其體積正好為j的標志,為布爾型。
狀態轉移方程為
f[I,j] = f [ I-1, j – w[I]*k ] (k=1.. j div w[I])
B.求可以放入的最大價值。
USACO 1.2 Score Inflation
進行一次競賽,總時間T固定,有若干種可選擇的題目,每種題目可選入的數量不限,每種題目有一個ti(解答此題所需的時間)和一個si(解答此題所得的分數),現要選擇若干題目,使解這些題的總時間在T以內的前提下,所得的總分最大,求最大的得分。
*易想到:
f[i,j] = max { f [i- k*w[j], j-1] + k*p[j] } (0<=k<= i div w[j])
其中f[i,j]表示容量為i時取前j種背包所能達到的最大值。
*實現:
Begin
FillChar(f,SizeOf(f),0);
For i:=1 To M Do
For j:=1 To N Do
If i-problem[j].time>=0 Then
Begin
t:=problem[j].point+f[i-problem[j].time];
If t>f[i] Then f[i]:=t;
End;
Writeln(f[M]);
End.
C.求恰好裝滿的情況數。
Ahoi2001 Problem2
求自然數n本質不同的質數和的表達式的數目。
思路一,生成每個質數的系數的排列,在一一測試,這是通法。
procere try(dep:integer);
var i,j:integer;
begin
cal; {此過程計算當前系數的計算結果,now為結果}
if now>n then exit; {剪枝}
if dep=l+1 then begin {生成所有系數}
cal;
if now=n then inc(tot);
exit;
end;
for i:=0 to n div pr[dep] do begin
xs[dep]:=i;
try(dep+1);
xs[dep]:=0;
end;
end;
思路二,遞歸搜索效率較高
procere try(dep,rest:integer);
var i,j,x:integer;
begin
if (rest<=0) or (dep=l+1) then begin
if rest=0 then inc(tot);
exit;
end;
for i:=0 to rest div pr[dep] do
try(dep+1,rest-pr[dep]*i);
end;
{main: try(1,n); }
思路三:可使用動態規劃求解
USACO1.2 money system
V個物品,背包容量為n,求放法總數。
轉移方程:
Procere update;
var j,k:integer;
begin
c:=a;
for j:=0 to n do
if a[j]>0 then
for k:=1 to n div now do
if j+now*k<=n then inc(c[j+now*k],a[j]);
a:=c;
end;
{main}
begin
read(now); {讀入第一個物品的重量}
i:=0; {a[i]為背包容量為i時的放法總數}
while i<=n do begin
a[i]:=1; inc(i,now); end; {定義第一個物品重的整數倍的重量a值為1,作為初值}
for i:=2 to v do
begin
read(now);
update; {動態更新}
end;
writeln(a[n]);
四、排序演算法
A.快速排序:
procere qsort(l,r:integer);
var i,j,mid:integer;
begin
i:=l;j:=r; mid:=a[(l+r) div 2]; {將當前序列在中間位置的數定義為中間數}
repeat
while a[i]<mid do inc(i); {在左半部分尋找比中間數大的數}
while a[j]>mid do dec(j);{在右半部分尋找比中間數小的數}
if i<=j then begin {若找到一組與排序目標不一致的數對則交換它們}
swap(a[i],a[j]);
inc(i);dec(j); {繼續找}
end;
until i>j;
if l<j then qsort(l,j); {若未到兩個數的邊界,則遞歸搜索左右區間}
if i<r then qsort(i,r);
end;{sort}
B.插入排序:
思路:當前a[1]..a[i-1]已排好序了,現要插入a[i]使a[1]..a[i]有序。
procere insert_sort;
var i,j:integer;
begin
for i:=2 to n do begin
a[0]:=a[i];
j:=i-1;
while a[0]<a[j] do begin
a[j+1]:=a[j];
j:=j-1;
end;
a[j+1]:=a[0];
end;
end;{inset_sort}
C.選擇排序:
procere sort;
var i,j,k:integer;
begin
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then swap(a[i],a[j]);
end;
D. 冒泡排序
procere bubble_sort;
var i,j,k:integer;
begin
for i:=1 to n-1 do
for j:=n downto i+1 do
if a[j]<a[j-1] then swap( a[j],a[j-1]); {每次比較相鄰元素的關系}
end;
E.堆排序:
procere sift(i,m:integer);{調整以i為根的子樹成為堆,m為結點總數}
var k:integer;
begin
a[0]:=a[i]; k:=2*i;{在完全二叉樹中結點i的左孩子為2*i,右孩子為2*i+1}
while k<=m do begin
if (k<m) and (a[k]<a[k+1]) then inc(k);{找出a[k]與a[k+1]中較大值}
if a[0]<a[k] then begin a[i]:=a[k];i:=k;k:=2*i; end
else k:=m+1;
end;
a[i]:=a[0]; {將根放在合適的位置}
end;
procere heapsort;
var
j:integer;
begin
for j:=n div 2 downto 1 do sift(j,n);
for j:=n downto 2 do begin
swap(a[1],a[j]);
sift(1,j-1);
end;
❸ 四年級簡便計算大全
姓名:_________ 班級:_________ 學號:_________
527+199
735-198
105×99
865-198
75×98
68×99+68
63×88+88×37
58×99+58
25×49+75×49
575-78-22
48×89+48
367-199
56×102
75×48+75×52
(20+4)×25
99×11
32×(200+3)
68×39+68
239×101
38×25×4
42×125×8
(25×125)×8×4
78×125×8×3
(125×25)×4
(125+25)×4
127+352+73+4
89+276+135+33
5+204+335+96
25+71+75+29 +88
243+89+111+57
399+(154+201)
480+325+75
78+53+47+2291+89+11
36+18+64
168+250+32
85+41+15+59
78+46+154
130-46-34
263-96-104
970-132-68
400-185-15
472-126-124
168-28-72
437-137-63
244+182+56
951-395
19+199+1999
34+304+3004
798+321
325-156+675-144
8+98+998+9998
99999+9999+999+99+9+4
44+37+56
163+49+261
74+(137+326)
249+402
189+35+211+165
483-236-64
582-157-182
65×5×2
15×23×4
36×25
25×125×32
35×22
5×(63×2)
540÷45÷2
540÷36
216+305
25×32
47+236+64
6×(15×9)
402+359
43+78+122+257
25×(26×4)
25×44
354+(229+46)
25×(4×12)
25×(4+12)
64×64+36×64
99×99+99 49×99+49
49×99+49
⑴ a+b =b+a
88+56+12
178+350+22
56+208+144
⑵ (a+b)+c=a+(b+c)
(23+56)+47
19×75×8
62×8×25
43×15×6
41×35×2
⑸ a×(b+c) =a×b+a×c
136×406+406×64
702×123+877×702
246×32+34×492
⑹ a×(b-c) =a×b-a×c
102×59-59×2
456×25-25×56
43×126-86×13
101×897-897
⑺ a-b-c=a-(b+c)
458-45—155
2354-456-544
68547-457-123-420
⑻ a-b+c=a+c-b
4235-4067+765
3569+526-1569
45682-7538+14318
⑼ a÷b÷c=a÷(b×c)
4500÷4÷75
16800÷8÷25
248000÷8÷125
5200÷4÷65
⑽ a÷b×c=a×c÷b
4500×102÷90
3600÷80×2
125÷20×8
250÷75×30
⑾ a-b=a-(b+c)+c
429-293
1587-689
8904-1297
87905-388
⑿ a-b=a-(b-c)-c
2564-302
25478-9006
5024-502
1251-409
⒀ a+b=a+(b+c)-c
254+489
⒁ a+b=a+(b-c)+c
124+4005
1235+607
248+803
2005+45687
⒂ 254+246+744+1054
5897+568-897+432
45627-258-742-1627
⒃ 321×46-92×27-67×46
75×32×125
25×12
25×16
25×24
28×25
32×250
25×44
25×84
125×16
125×24
125×32
56×125
125×88
720×125
48×125
13×99+13
91×99+91
43×99+43
38×99+38
184×99+184
26×99+26
55×101-55
16×101-16
57×101-57
39×101-39
123×101-123
852×101-852
17×21+83×21
124×63+124×37
62×74+38×74
27×25+13×25
33×33+67×33
18×35+2×35
116×57+43×116
17×23-7×23
124×63-24×63
132×28-32×28
34×54-24×54
53×25-13×25
381×32-32×81
93×125-13×125
24×99
99×26
98×34
99×45
99×32
18×99
12×101
33×103
101×54
103×23
62×101
24×102
527+199
735-198
105×99
865-198
75×98
68×99+68
63×88+88×37
58×99+58
25×49+75×49
575-78-22
48×89+48
367-199
56×102
75×48+75×52
2×125×8
(25×125)×8×4
78×125×8×3
(125×25)×4
(125+25)×4
127+352+73+4
89+276+135+33
5+204+335+96
25+71+75+29 +88
243+89+111+57
399+(154+201)
480+325+75
78+53+47+2291+89+11
36+18+64
168+250+32
85+41+15+59
78+46+154
130-46-34
263-96-104
970-132-68
400-185-15
472-126-124
168-28-72
437-137-63
244+182+56
200-173-27
124+68+76
263-96-104
970-132-68
400-185-15
472-126-124
603+421 745-305
951-395
19+199+1999
34+304+3004
798+321
325-156+675-144
8+98+998+9998
99999+9999+999+99+9+4
582-157-182
65×5×2
15×23×4
36×25
25×125×32
35×22
5×(63×2)
540÷45÷2
540÷36
216+305
25×32
47+236+64
6×(15×9)
402+359
43+78+122+257
25×(26×4)
25×44
354+(229+46)
25×(4×12)
25×(4+12)
64×64+36×64
99×99+99
49×99+49
49×99+49
88+56+12
178+350+22
56+208+144
⑵ (a+b)+c
(23+56)+47
286+54+46+4
582+456+544
25×37×4
75×39×4 65×11×4
125×39×16
19×75×8
62×8×25
43×15×6
41×35×2
136×406+406×64
702×123+877×702
246×32+34×492
102×59-59×2
456×25-25×56
43×126-86×13
101×897-897
582-157-182
65×5×2
15×23×4
36×25
25×125×32
35×22
5×(63×2)
540÷45÷2
540÷36
216+305
25×32
47+236+64
6×(15×9)
402+359
43+78+122+257
25×(26×4)
25×44
354+(229+46)
25×(4×12)
25×(4+12)
64×64+36×64
99×99+99
49×99+49
49×99+49
88+56+12
178+350+22
56+208+144
⑵ (a+b)+c
(23+56)+47
286+54+46+4
582+456+544
25×37×4
75×39×4 65×11×4
125×39×16
19×75×8
62×8×25
43×15×6
41×35×2
136×406+406×64
702×123+877×702
246×32+34×492
102×59-59×2
456×25-25×56
43×126-86×13
101×897-897
458-45—155
582-157-182
65×5×2
15×23×4
36×25
25×125×32
35×22
5×(63×2)
540÷45÷2
540÷36
216+305
25×32
47+236+64
6×(15×9)
402+359
43+78+122+257
25×(26×4)
25×44
354+(229+46)
25×(4×12)
25×(4+12)
64×64+36×64
99×99+99
49×99+49
49×99+49
88+56+12
178+350+22
56+208+144
⑵ (a+b)+c
(23+56)+47
286+54+46+4
582+456+544
25×37×4
75×39×4 65×11×4
125×39×16
19×75×8
62×8×25
43×15×6
41×35×2
136×406+406×64
702×123+877×702
246×32+34×492
102×59-59×2
456×25-25×56
43×126-86×13
101×897-897
458-45—155
2354-456-544
68547-457-123-420
4235-4067+765
3569+526-1569
45682-7538+14318
2354-456-544
68547-457-123-420
4235-4067+765
3569+526-1569
45682-7538+14318
458-45—155
2354-456-544
68547-457-123-420
4235-4067+765
3569+526-1569
45682-7538+14318
44+37+56
163+49+261
74+(137+326)
249+402
189+35+211+165
483-236-64
(20+4)×25
99×11
32×(200+3)
68×39+68
239×101
38×25×4
65×16×125
加法交換律:a+b=b+a 加法結合律:(a+b)+c=a+(b+c)
乘法交換律:a×b=b×a 乘法結合律:(a×b)×c=a×(b×c)
乘法分配律:(a+b)×c=a×c+b×c 或a×(b+c)=a×b+a×c
減法的性質:a-b-c=a-(b+c)
帶著加減號搬家:a-b-c=a-c-b a-b+c=a+c-b a+b-c=a-c+b
怎麼簡便怎麼算:
★ (23+56)+47 25×277×4 125×(3+8)
462-83-117 8×(30×125) 3200÷25÷4
★★ 425-38+75 5246-(246+694) 25×6+25×4
360÷(18× 4) 32×105 598+735
★★★99×38+38 98×34 25+75-25+75
48×125 540÷45 103×56
5021+897
654+793
654+4999
286+54+46+4
582+456+544
⑶ a×b=b×a
25×37×4
75×39×4
65×11×4
125×39×16
200-173-27
124+68+76
263-96-104
970-132-68
400-185-15
472-126-124
603+421
745-305
❹ 24演算法大全101072
(2*(7+10))-10
(2*(7+10))-10
(2-10)*(7-10)
(2*(10+7))-10
(2-10)*(7-10)
(2*(10+7))-10
(7-10)*(2-10)
((7+10)*2)-10
(7-10)*(2-10)
((7+10)*2)-10
(10-2)*(10-7)
((10+7)*2)-10
(10-7)*(10-2)
(10-2)*(10-7)
((10+7)*2)-10
(10-7)*(10-2)