- 在线时间
- 5024 小时
- 最后登录
- 2022-11-28
- 注册时间
- 2009-4-8
- 听众数
- 738
- 收听数
- 1
- 能力
- 23 分
- 体力
- 77273 点
- 威望
- 96 点
- 阅读权限
- 255
- 积分
- 27108
- 相册
- 1
- 日志
- 14
- 记录
- 36
- 帖子
- 4293
- 主题
- 1341
- 精华
- 15
- 分享
- 16
- 好友
- 1975

数学中国总编辑
TA的每日心情 | 衰 2016-11-18 10:46 |
|---|
签到天数: 206 天 [LV.7]常住居民III 超级版主
 群组: 2011年第一期数学建模 群组: 第一期sas基础实训课堂 群组: 第二届数模基础实训 群组: 2012第二期MCM/ICM优秀 群组: MCM优秀论文解析专题 |
#include
0 U" D" F1 ]) G1 g7 W#include
2 a6 d4 |% Q3 j( M, ~% o1 X/ X* w! `& W#define N 11
8 |: z' N) O- Y/*用**哨查找*/
3 x8 \, M* c% d/ Mint search(int array[],int n,int k)
- x7 h$ Q$ n, r( J/ w) n0 R. C, \{int i;
" T# J( Y: s$ Fi=n-1;
% t8 i7 D" J. }2 r, i0 D" garray[0]=k;
G1 T3 b) t% b1 \while(array[i]!=k) i--;
; ^0 P7 x. e+ G7 i+ f3 r" Z& Freturn(i);
7 f3 q! ]3 L! p w& h! Y) {}
- \6 {) V; [2 q! c+ q/*折半查找法*/
2 N9 M, W/ |0 Q% \int halfsearch(int array[],int n,int k)7 k: [- s4 ~/ P" ~8 S
{int i,j,mid;
. H8 ~ |8 i( |7 \0 y, @: o7 Li=1;j=n;; H1 c; M4 \! B. b' D
while(i<=j)
/ h# N0 e9 I) B7 X{mid=(i+j)/2;1 M6 B4 o! \; j( D! H6 N
if(k==array[mid]) return(mid);% |8 r$ ^9 _1 E: l* [3 Q* {$ A
else if(k - k+ y' _- U1 ]2 A, n$ X
else i=mid+1;2 F: P0 [6 M" L2 X. @
}) O/ P" ~ K. {1 G. @
return(0);% _" P7 a. O5 v$ B& l9 f) F H6 P9 B
}) x0 I. ~5 ~$ h+ k( N- m; n
/*冒泡排序法*/; |7 O6 q% @! Q/ U1 h
void mpsort(int array[]); O/ R3 C: n7 N; k0 r Q
{int i,j,a;
2 C7 [ n1 e; O5 ia=0;" L* d6 j! p# [% Z1 Z b! w$ p5 v
for(i=1;i
0 U7 A4 ~& W' O3 p8 u0 Vfor(j=i+1;j
) p5 c: G& d) Pif(array[i]>array[j])
' O7 z* r" C. @5 u$ n{a=array[i];$ s( Y) {: s! [. L& i) c. X
array[i]=array[j];5 c3 U0 q2 ^8 P* ^! a! k# Q) R, y0 R7 f
array[j]=a;}( `* A$ ^8 d5 p% f [
}4 o* \- [) m2 p4 N3 ]# |
/*直接插入排序*/- D$ k& o. m; L$ N. @0 U
void insertsort(int array[])
1 I$ ^0 A+ L+ A2 F{int i,j;
* r* `1 y# h) Y Ffor(i=2;i / q! X/ r; U( c% o+ q# y& A" o# F
{array[0]=array[i];
5 F) H0 C& h$ o; Jj=i-1;
) \3 P6 R( g M4 ], N- b, zwhile(array[0] & l0 t" z+ @( n( O- s) Z0 r2 h
{array[j+1]=array[j--];( G: |! a& I A6 e
array[j+1]=array[0];
0 W) s- _9 X% _: R* d}/ \$ D$ N4 k' J) w$ D6 j, y- K
}
+ B5 A: z) }2 E. w- P}; V1 f. F$ r( K( L8 |
/*建立*/
5 S' N. |7 z" t3 r6 J2 I: F. Lvoid creat(int array[])/ T- V2 y2 x: [6 }5 J* j/ F" i
{int i;$ k7 ~0 H5 a) X. E( [, _& j1 s
printf("enter the array:\n");2 s7 b" B, b8 k, b# b0 z
for(i=1;i
B; e& o) C. ]scanf("%d",&array[i]);) A* K% @5 `. T' Y: h
}! b. v; l$ A# ]% w0 N: `3 S
/*显示*/$ S! s3 L4 v' z- G- b; q' g( ~
void print(int array[])
8 l ?& |% p3 Z/ n{int i;( k. N9 w, Y; t/ D
printf("The numbers after sort is:\n");1 H; V: G% l9 C) \ V! m
for(i=1;i
$ K: T' a/ U! W1 B+ |7 aprintf("%d ",array[i]);
5 m c3 J* j! s0 \. f8 Dprintf("\n");& x' G$ M: m( }4 _! B9 K# R
}- ]7 z$ q0 _; E& N. C( `
main()9 Y9 p0 r* s, Y( q
{int a[11],i,x,chang;3 R1 _8 r! ~4 I- S: [5 B
/*printf("enter the array\n");
7 ]6 @2 T* G) I6 cfor(i=1;i<11;i++)
z) D: J; `3 L: ^0 W" i5 Yscanf("%d",&a[i]);*/* T+ ^' ]9 I2 f3 {$ p1 `
aga:/ `* J3 l" I ]0 y. X, w% d
printf("\nchang:1: use watching method finding\n 2:use half method finding\n 3: use directness intsert method sort\n 4:use bubble up method sort\n 5:exit\n");* a8 Y$ t& J, {' l* u$ X7 h1 W0 S
scanf("%d",&chang);! L) X9 w8 R- x" }
switch (chang)5 t" [$ a* J2 j- L: M' G8 u. K* M& ^
{case 1:
$ K' x1 l" I8 F& C" V{creat(a);
6 S" ^" A) M I3 o/ N) lprintf("Please int the search number:\n");1 }3 Y% W+ z1 s. Q( D5 J0 M
scanf("%d",&x);
" m6 W+ u& ?: e' u0 G) Wprintf("The number station is:%d\n",search(a,N,x));* s$ R; l. L3 {' s. ?' L2 O
goto aga; I! p4 D% B6 B1 s0 y
}' f V G9 h8 A8 @# |3 Z
case 2:
" j. \& P: C1 P& m{ creat(a);; J* i$ `/ O: I
insertsort(a);$ |7 K5 z' ]1 C! K3 m: Y
print(a);: C8 \% @7 G- R
printf("Please int the search number:\n");$ c2 p4 q! a8 V2 k' X3 {
scanf("%d",&x);
5 V' @) y+ C6 j0 ]/ l" Oprintf("The number station is:%d\n",halfsearch(a,N,x));
; V+ Y- K" E+ N7 x- Q% ygoto aga;
7 l6 U. ]$ D& m+ z- Q}
. h0 a$ W. G- H$ z" Scase 3:
9 p3 A3 Q d5 d+ g7 m" @' h{creat(a);9 m% d! z/ G8 y: J' u3 O5 ]
insertsort(a);
, J; w; W$ n: B0 R) E: \6 i( n! rprint(a);
7 ]6 ^' s3 o, c7 egoto aga;
% d2 t- H. S% c2 ^$ n9 N& S. L1 B}
& J9 _0 B" Q0 ]$ g# K4 Bcase 4:
6 ~/ [& O# K- f8 Q% S1 W{creat(a);
; _, T% ~7 t& r/ u5 @mpsort(a);5 j$ ~. n1 o5 G4 @2 j0 C0 y% @, i
print(a);. S" C: ~) e& Y# M1 d4 a- b5 }
goto aga;
3 E+ p! l8 Y/ \# T}
, ^+ z; p) u6 G5 {( N' o* r8 Y. A5 f7 Hcase 5:{ printf("exit!\n");break;}
/ o: E; f7 \) V4 x Y% l6 e3 pdefault:{printf("Error!\n"); goto aga;}2 T9 `$ l; a; t# k9 e
}( F$ B5 j* r3 {/ r7 T
}
4 u- o5 h" A+ O% ?" U$ q/ G$ x O3 x$ ?& Q& j; z
|
zan
|