你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

C语言算法系列的第二篇

[复制链接]
gaosmile 发布时间:2020-11-11 18:51
算法是一个程序和软件的灵魂,作为一名优秀的程序员,只有对一些基础的算法有着全面的掌握,才会在设计程序和编写代码的过程中显得得心应手。本文是近百个C语言算法系列的第二篇,包括了经典的Fibonacci数列、简易计算器、回文检查、质数检查等算法。也许他们能在你的毕业设计或者面试中派上用场。
5 V  h. q6 U1 P* Q/ b! [" i6 B# i

8 U- A8 `& _  `5 K! `) B
1、计算Fibonacci数列
( n1 i$ z- X' ^8 _8 o9 h
Fibonacci数列又称斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21。

. u7 \' h6 \1 j. X" N$ c
C语言实现的代码如下:
  1. 8 @5 Q3 p2 `6 f, ?
  2. /* Displaying Fibonacci sequence up to nth term where n is entered by user. */4 @; X9 y* u! g5 l
  3. #include <stdio.h>
    ! w8 P7 u: J5 r
  4. int main()
    5 K! e: U' ?& q( ~
  5. {6 Y; b* {$ [5 t6 y0 _& V# V' N
  6.   int count, n, t1=0, t2=1, display=0;
    2 `7 G- p% g% N9 z
  7.   printf("Enter number of terms: ");% c: R( `" J2 p
  8.   scanf("%d",&n);. e0 Q4 D, b, {# T0 I# {
  9.   printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
    % j# @2 R5 b& H* }) Z
  10.   count=2;    /* count=2 because first two terms are already displayed. */, f* Q5 p; S& z
  11.   while (count<n)  ! E  H  W# I2 r$ I! L, U
  12.   {
    4 w6 h0 i5 }! K, Y# b- k3 w
  13.       display=t1+t2;
    3 c" M  ~1 t- ]$ L
  14.       t1=t2;
    8 G& v) b$ @6 u8 s! }
  15.       t2=display;
    - v- Z  {- Z7 k5 {
  16.       ++count;7 ~* {& S* U* a2 b
  17.       printf("%d+",display);! I7 C, ?' Z4 \  D# h4 G2 `7 ?1 i
  18.   }
    ! l" p$ i0 e/ i* ~' Z/ _
  19.   return 0;3 X% [' |# j% G  b7 i: d) O( h
  20. }
复制代码
' a" D3 k4 Z; s6 }& A
结果输出:
  1. ' ?1 A6 T" j* i- Q$ n
  2. Enter number of terms: 10
    / r. q( ]/ U( d6 h5 C" `
  3. Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+
复制代码
8 S8 Y1 r, p& a- U
也可以使用下面的源代码:

  1. 2 |- j- o: @. h% l
  2. /* Displaying Fibonacci series up to certain number entered by user. */
    8 E1 g  s5 \! k! r; f6 Y$ A9 A- G# L

  3. . U; a6 S. u% |* M
  4. #include <stdio.h>
    ( |; ~' |8 [$ Z# k* U
  5. int main()  {. w4 A  n- G; J5 z4 y+ Y# o. K
  6. {
    ) U* t. |6 C& D8 G. M" ~
  7.   int t1=0, t2=1, display=0, num;
    - T, t. ^! \# I5 p+ S" P+ |% P2 c' y
  8.   printf("Enter an integer: ");1 N6 p5 a4 t- I' O; D; J
  9.   scanf("%d",&num);3 ^7 x) ]9 T0 l. U( s
  10.   printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms *// r; m+ U: {$ q
  11.   display=t1+t2;- x$ p: u2 F( o/ v, W, T
  12.   while(display<num)
    1 G! z1 S# H! x- Z$ B$ Q
  13.   {
    ! Q9 k6 b: ?# q  D6 Q* V9 I" G9 r
  14.       printf("%d+",display);
    . X7 e) F6 L+ q: }
  15.       t1=t2;
    $ `2 u6 a- y* a* m7 V9 R7 I
  16.       t2=display;
    . C- t! I# E7 `' M8 h8 S9 a
  17.       display=t1+t2;
    3 N# ?  B3 G4 a: i4 Y5 b/ O
  18.   }5 r4 C6 j, E. v
  19.   return 0;4 E9 H) C( z" ~) z, c$ `
  20. }
复制代码
2 p9 t( M( V7 m% w* Q: e) z
结果输出:
  1. & M- w! ^) `2 |9 x( o( m; C
  2. Enter an integer: 200
    ( \( a5 s1 b8 u! |- @
  3. Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+55+89+144+
复制代码

3 d! J/ R" e5 `' ~5 y
2、回文检查
+ k# X& c. B7 \/ C" B
源代码:

  1. . p2 A  L# H; _! Z
  2. /* C program to check whether a number is palindrome or not */3 d( [5 f* K. r; S& }
  3. 4 F. B+ W* q7 ~% q4 s4 q# S! a) ]' l
  4. #include <stdio.h>
    # t8 d8 ~- L! z. X  D: f4 w* o
  5. int main()/ ?( [( P% t* G- ]3 u+ ~* D
  6. {- Z# S  r- |2 m2 n+ t4 S, r
  7.   int n, reverse=0, rem,temp;
    . Q$ v' U& Y/ `0 x
  8.   printf("Enter an integer: ");
    " v: F6 o( Q2 ^
  9.   scanf("%d", &n);
    - c9 [8 G3 V, H. I
  10.   temp=n;7 V; d5 O8 n# T, K! ?
  11.   while(temp!=0)! L* a# q4 Q! ], w
  12.   {
    * ?# y. u. z+ ]# d* f
  13.      rem=temp%10;5 {! W9 Y: W. i! z
  14.      reverse=reverse*10+rem;
    9 X) a3 E5 m: g1 U% t1 s8 [; [
  15.      temp/=10;
    6 V+ M, b0 a1 ]( ]5 a
  16.   }  
    1 E/ f% o# ~( d8 ?2 K
  17. /* Checking if number entered by user and it's reverse number is equal. */  5 q% m* U$ l: C. V/ H
  18.   if(reverse==n)  ; G1 r6 s! o; x, u
  19.       printf("%d is a palindrome.",n);. @" y; ]+ w. ?
  20.   else- m9 {& _- t& s
  21.       printf("%d is not a palindrome.",n);
    / ^/ s5 c, Q; ]: `( k; u
  22.   return 0;. ]7 [- u& [4 A
  23. }
复制代码

+ D$ c0 H, z6 P5 I! P
结果输出:

  1. & g1 f+ V; J- X, t' h
  2. Enter an integer: 12321
    3 a6 N! ^. b, A4 s- v( I7 I
  3. 12321 is a palindrome.
复制代码

% O" R, D' o% X+ Z5 ?$ N& k) B6 k
3、质数检查
# |- ]- X+ I$ g& I7 R& D  x$ f
注:1既不是质数也不是合数。
' J4 l* J  ~) E3 O
源代码:& W& @5 A0 N7 {+ F

  1. 6 o) F# N& E0 a' T/ [
  2. /* C program to check whether a number is prime or not. */! h9 I1 u4 w$ c
  3. - R8 _2 D+ z* j
  4. #include <stdio.h>8 N8 e8 ~9 o0 O% i0 v0 p
  5. int main()
    5 n% E- D4 c- k' p) a
  6. {
    2 k7 S# ]0 o) X. o. ^0 [2 y. b4 b
  7.   int n, i, flag=0;) N" q% g# k( u) H+ v" ?2 c$ i
  8.   printf("Enter a positive integer: ");
    8 V7 k- N. I6 s$ j- O/ j
  9.   scanf("%d",&n);
    $ q& T( Y6 t7 d! a3 X
  10.   for(i=2;i<=n/2;++i)3 l! y* q9 ]: P9 P- }! a, F4 N
  11.   {( {( X0 w: {6 a8 ^; \
  12.       if(n%i==0)$ ]6 N: C0 i, k0 M: W* x
  13.       {: V: P# Z: F7 [5 Y" H+ z8 Y
  14.           flag=1;% q) Y+ _. q, T% |! C
  15.           break;
    / l1 Y+ G; E  p
  16.       }5 k: D& a! }, U' x0 g- `
  17.   }# J# l  O3 E* s* H- Y1 D& v
  18.   if (flag==0)8 `1 \# B0 o! l# S4 ^- y
  19.       printf("%d is a prime number.",n);/ l4 @: X8 `+ d
  20.   else% Q, y) _$ F3 w2 I0 Z3 O
  21.       printf("%d is not a prime number.",n);6 h: E3 L9 b# x) Z
  22.   return 0;
    % q, X9 _* _1 g% N
  23. }
复制代码

1 @4 D, f' ^% Q6 h) g
结果输出:

  1. / F# _7 b* E( h0 w0 l: G
  2. Enter a positive integer: 29
    1 J2 W5 r! Y, e; [. W4 \
  3. 29 is a prime number.
复制代码
  {0 q9 j: U; ]. U, v+ c2 \/ o# @
4、打印金字塔和三角形
4 n+ e# z5 R0 H
使用 * 建立三角形
  1. , C$ W3 R+ z1 d
  2. *
    7 [5 D' |+ Z( `/ }: X  v
  3. * *3 x. v, s- Y6 q# a7 k6 _2 Z
  4. * * *' F) m  f: C+ [! w1 M# w
  5. * * * *8 E1 J, B* I  z/ s  w
  6. * * * * *
复制代码
9 f! t- G9 `/ V" _, w% l
源代码:

  1. 3 o$ O0 ~; q8 ~- X  Q" b( A5 U4 o
  2. #include <stdio.h>0 k9 F' n+ r5 _: r, f
  3. int main()' a/ n; w7 p' n* L# \$ t- x# R4 N) C" {$ A
  4. {* I  |3 ^8 e% y: g" |$ @! J( z) Y
  5.     int i,j,rows;
    % A* p1 j. x  W4 Z, K
  6.     printf("Enter the number of rows: ");
    & e! }2 m$ j7 Z5 q
  7.     scanf("%d",&rows);//微信搜索公众号【C语言中文社区】关注回复C语言,免费领取200G学习资料! z; q: H$ @& N# d% a
  8.     for(i=1;i<=rows;++i). Y- O( T+ y1 S1 s! ]
  9.     {9 b& y$ n! @  [4 d) d- [
  10.         for(j=1;j<=i;++j)
    , q& W- v6 ^/ f  z5 ]. t
  11.         {
    3 B4 x  P: U% j) r, B7 g/ M. y8 C
  12.            printf("* ");- B$ j; i* [% B  i& N
  13.         }* {: u5 ?3 \9 V* H. z# c4 A  W
  14.         printf("\n");
    / v- Q5 J! s- W9 D$ E, W- O- t2 O3 n
  15.     }
    & N* I/ {' f- C7 z& I
  16.     return 0;
    $ C5 e* ]7 ^+ ^/ V2 X7 e: g) T
  17. }
复制代码
$ G3 E- W6 _# j3 L
如下图所示使用数字打印半金字塔。

  1. 4 M1 A* `2 M. X, {
  2. 1
    ) m( u/ m  v) F" e
  3. 1 2
    ! i0 J7 t3 G+ K2 n( I
  4. 1 2 31 ^2 S) I9 H1 y( ]% ~# T; J
  5. 1 2 3 40 f  G+ n" G9 v7 b/ ?2 P$ Z
  6. 1 2 3 4 5
复制代码
+ A, V6 Y4 f! d8 R( r) C0 ^
源代码:

  1. 9 Y* C  l* H9 u( S/ _6 }, F; m! R
  2. #include <stdio.h>
    $ O- x- C5 n' m6 ~. C& ^( ?
  3. int main()
    3 _$ G+ [  K  @0 ~7 {
  4. {
    8 I# \. c* ^1 S( i" _; L8 k
  5.     int i,j,rows;4 ?- X% c4 T! O
  6.     printf("Enter the number of rows: ");
    2 S% F# v0 y5 W0 p
  7.     scanf("%d",&rows);
    . g, Y+ m% |" Z8 O( X6 ^
  8.     for(i=1;i<=rows;++i)  @+ G" d8 `0 O
  9.     {
    + V* d6 R+ {5 G4 Z' t) |
  10.         for(j=1;j<=i;++j)) T! U' B& i! m! b
  11.         {* y( f" {1 S! E9 B
  12.            printf("%d ",j);+ F5 k) {1 h# S: R3 h
  13.         }
    3 @8 M5 Y' [# }
  14.         printf("\n");2 T7 i: P+ M* X* m, d3 Y6 K
  15.     }, ]# t8 Y8 [# k3 x2 |0 e" f- u
  16.     return 0;
    # W# b( S6 q; H6 c9 Q9 h0 h
  17. }
复制代码
2 O# y/ z3 t# W
用 * 打印半金字塔

  1. ) k+ j4 R! M+ N& @
  2. * * * * *$ ]/ P* h, V, m0 B: A+ L
  3. * * * *: D1 K4 P5 z' d4 @
  4. * * *
    + |& M/ s: V/ l% w6 Z2 |
  5. * *
    4 o8 A- |  ?2 O6 s. P9 }9 G3 ~; a! h6 v
  6. *
复制代码

: V% D% b$ S9 Y  v) [+ n% B
源代码:

  1.   X1 _1 H/ C9 T/ |! w8 C7 v
  2. #include <stdio.h>
    % i) q" H- f8 `. a1 {9 j2 \5 O) h
  3. int main()
    5 [8 L! P) ~" D# W5 p2 M: c! z6 H
  4. {
    ) q8 B% M) e9 h* Y' z
  5.     int i,j,rows;
    + S3 o. P9 q2 z3 O- U4 @( t
  6.     printf("Enter the number of rows: ");
    9 S' D* b# N! h
  7.     scanf("%d",&rows);$ R6 ]6 j; h+ D
  8.     for(i=rows;i>=1;--i)
    2 {. c5 S4 Z" F+ N6 o
  9.     {
    * ]2 h2 r# L  C, L
  10.         for(j=1;j<=i;++j)5 G8 F5 U6 X4 R" `: L8 i- U. Y
  11.         {
    / \, a% ~$ X$ w& V7 l. t. C
  12.            printf("* ");
    + n9 i$ W; n6 [9 s% }% |5 w
  13.         }
    " ]6 Z  x+ P( V# ~
  14.     printf("\n");1 U1 c$ |& Z) y6 f# R7 u2 M; ]
  15.     }. z4 w2 j) k( D6 h0 q
  16.     return 0;: \! d+ t( E, y4 s$ r% \
  17. }
复制代码

# Y5 g0 f  j: e' o+ g5 [+ O
用 * 打印金字塔
  1. + w7 d$ x5 P0 e2 y) m
  2.         *
    + c3 X8 ]* v6 _/ ^% O$ T
  3.       * * *- u3 B7 @3 L, J5 h
  4.     * * * * *8 v( F* v- r: s, X$ b( W
  5.   * * * * * * *
    3 f; z. p" I7 w% T2 M/ m) x+ E
  6. * * * * * * * * *
复制代码
" k  v( D" O) G2 p
源代码:
  1. 6 S1 f+ r* f9 v+ ]% C4 F% h2 J2 j
  2. #include <stdio.h>7 Q1 K4 u; M: d$ v* T. K: @% d" X
  3. int main()0 D5 X. u3 v9 l$ t# d' }
  4. {
    7 C, r2 Y; ?  \
  5.     int i,space,rows,k=0;9 Y% d7 k2 j! ]+ E1 r! i
  6.     printf("Enter the number of rows: ");0 ^+ m' M0 c" q7 r; y- \( ]
  7.     scanf("%d",&rows);
    ! p3 u8 g+ A! d
  8.     for(i=1;i<=rows;++i)
    0 ~2 x; Y5 F6 ]6 Z- U
  9.     {
    : t$ D$ ?: X+ D: s. U8 p% i1 V
  10.         for(space=1;space<=rows-i;++space)! x' i' J, P% j9 D5 `, z& b1 V1 _6 g
  11.         {
    " l3 Z+ N+ i9 w5 v% W( ~0 h
  12.            printf("  ");4 r' P* k- I! o
  13.         }) m( L. o$ |, G" E0 N
  14.         while(k!=2*i-1)
    ; b' o) o+ [7 q1 }( a/ J5 Q
  15.         {- v/ H4 i. F- t$ F2 G3 N
  16.            printf("* ");
    8 Y$ C' S. S2 D6 X
  17.            ++k;, F$ H& M3 m1 N' V
  18.         }
    & G  J) _4 t) O" G, F
  19.         k=0;
    $ u0 R+ u" ]6 Z2 {! u' K( T% ?9 ]
  20.         printf("\n");+ ^/ O7 ~6 [2 s9 _: J, f" V
  21.     }
    * Z( @- ?5 I  }) }
  22.     return 0;
    " P5 Y, y6 E) G5 F( `+ z0 F8 n
  23. }
复制代码
% J% Q, ]& c0 ?% e& ^
用 * 打印倒金字塔
  1. / c: @8 b% C; k% p
  2. * * * * * * * * *& u( L* u  \$ c. S) J4 r' h
  3.   * * * * * * *7 |! n* B0 ?& o
  4.     * * * * *
    4 e8 A$ j- C! [8 K9 E
  5.       * * *9 {& E# {, i, Y$ y
  6.         *
复制代码
5 y' e- U1 j% ^; x
源代码:

  1. 0 X2 k9 _; N" \2 a2 W* x  N3 P8 X
  2. #include<stdio.h>9 T. ^, y+ b& ]
  3. int main()
    ) l1 p7 A" |7 Z. J6 I2 s4 i
  4. {
    + @7 s1 h5 w8 m) e8 L  v5 X
  5.     int rows,i,j,space;  c2 V- m" z9 v8 ~
  6.     printf("Enter number of rows: ");6 j3 C. L3 l& n# o0 |4 G
  7.     scanf("%d",&rows);
    - y( k& o5 E' ?3 I2 a/ X
  8.     for(i=rows;i>=1;--i)( [3 g/ w7 s5 x7 F  x; L( @
  9.     {1 g3 C. [4 a7 n9 i  F, N
  10.         for(space=0;space<rows-i;++space)
    % B. t4 ~/ H  \# E
  11.            printf("  ");
    " M8 H% R$ e9 |) F1 I
  12.         for(j=i;j<=2*i-1;++j)  {0 O! T3 K1 l1 J2 A6 C3 U
  13.           printf("* ");7 C: M1 \9 c% Q, @' s
  14.         for(j=0;j<i-1;++j)
    ) `! b+ I7 H- b' U) X' N* t
  15.             printf("* ");+ l2 Z6 [4 D7 N9 j, Y- [8 ^! }
  16.         printf("\n");# y( _. {% e  D% Y, A9 ^
  17.     }" ^2 f' l" q6 u7 o" F
  18.     return 0;
    ( |! k# Y* c, f2 v, y' f5 A( U0 [
  19. }
复制代码
# ~9 C; y: j2 p* r! ?* j  x9 \
5、简单的加减乘除计算器

, W; j: u- b8 U
源代码:

  1. % E* W7 I7 R' E' g/ H- R+ o6 o  Z) B
  2. /* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C programming. */* G: ^# S7 X! {$ ?

  3. 0 b- A0 g2 [: Q% p# L/ H
  4. # include <stdio.h>% e/ {" S* L; ?  s/ V& j( F% n
  5. int main()
    3 i5 f; Z3 ?9 H5 \7 B. d. O7 F  K6 H
  6. {7 }' L# P2 N$ @; h
  7.     char o;& R. W" v  L# Y+ o8 |( o) e
  8.     float num1,num2;
    8 M# G) {- c- s1 M" w
  9.     printf("Enter operator either + or - or * or divide : ");
    6 L) X% o$ q, p3 T6 J' \! |
  10.     scanf("%c",&o);( _- Y- c/ N: X" [
  11.     printf("Enter two operands: ");
    : }8 D8 Q# W% A- D
  12.     scanf("%f%f",&num1,&num2);+ r8 o  C/ a% `# _( ?" {- {  {
  13.     switch(o) {
    ; _2 B* ?2 V2 ?2 M+ g3 W  M
  14.         case '+':/ T: r. {6 R) C. ^
  15.             printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
      \- e6 |* z, g( v# u' \
  16.             break;: k0 t& n$ ~7 |. A5 [) q. r( k
  17.         case '-':( D& B4 X( t& B& v- w
  18.             printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
      c8 q2 U/ |; S4 r
  19.             break;- ^( n  _, S$ _! H$ L$ c+ c( _
  20.         case '*':
    % r+ T+ T) B' u5 z$ W/ P
  21.             printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
    9 }  J0 w- M# J6 {! z& r
  22.             break;
    & ?- ?) k, z& m
  23.         case '/':
    + O- y4 }  q' M+ v
  24.             printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);5 s: p" q/ d$ D$ R- C
  25.             break;
    ! s4 J' k6 H* [5 T, h: p# W# l
  26.         default:
    6 N6 n) z8 h2 F8 K, D# b
  27.             /* If operator is other than +, -, * or /, error message is shown */
    0 }( x/ L! [# s" K9 ~
  28.             printf("Error! operator is not correct");
      M; s) P% y% F2 S" |
  29.             break;
    " a4 K7 k1 x6 m0 [" J
  30.     }
    3 k! E# m9 `+ F
  31.     return 0;6 m: x* D3 ?1 h7 U+ q8 n9 q
  32. }
复制代码

' F8 w* k5 b/ o( U* w8 J+ o
结果输出:

  1. 3 O5 f! c# v# g' E+ T1 k
  2. Enter operator either + or - or * or divide : -
    $ H3 V; {( @# p# ^2 i
  3. Enter two operands: 3.4/ ~$ p. o9 M7 T$ N5 W, A
  4. 8.4
    % p9 f. Z! E" l
  5. 3.4 - 8.4 = -5.0
复制代码
% x( }$ @0 q- a
6、检查一个数能不能表示成两个质数之和
. V& m- I& `0 _/ A  |7 E
源代码:
  1. 4 I* G2 ~& W- l, |6 R5 a
  2. #include <stdio.h>
    6 Z9 Y. g- x; W
  3. int prime(int n);
    - Y3 W; Z" E& B1 W0 ]0 O
  4. int main()
    ' _. L0 o, {9 Q& a* ~0 G
  5. {9 @0 ?; g+ E; L% y5 C) w* A
  6.     int n, i, flag=0;//微信搜索公众号【C语言中文社区】关注回复C语言,免费领取200G学习资料. q9 y# _' e. ~% J$ p' Y0 |  V, F
  7.     printf("Enter a positive integer: ");
    " D/ r+ S, |, s
  8.     scanf("%d",&n);
    , I; q' _; Z+ n: G
  9.     for(i=2; i<=n/2; ++i)
    . \/ V6 {* v: W# o
  10.     {- _9 ?) N9 d" f- p& L. c9 a' o
  11.         if (prime(i)!=0)
    4 W  z1 w" e* ]- S
  12.         {( j  M) a' @. G. W! b+ Y
  13.             if ( prime(n-i)!=0)
    2 @+ r6 O- t- T3 I
  14.             {1 T0 G6 ]3 I8 Z) _# K: e# w
  15.                 printf("%d = %d + %d\n", n, i, n-i);
    : H: P) [$ p6 i  z# V
  16.                 flag=1;) Q3 [& B3 }+ |- t5 b5 r+ P
  17.             }; F: s2 q5 e: z
  18. ( i8 n* C& h+ s* R$ D3 p% V& G
  19.         }
    ; j3 [( V& @$ |& v5 A
  20.     }5 |+ f0 ]# ?" `! }9 R, b
  21.     if (flag==0)
    4 N8 k7 U2 G/ n
  22.       printf("%d can't be expressed as sum of two prime numbers.",n);
    & c/ h9 F8 m; P
  23.     return 0;7 q' }/ h7 ]+ Q
  24. }4 b2 ^) P0 r* L5 d8 {4 P8 n- j
  25. int prime(int n)      /* Function to check prime number */8 O( k. ]3 W6 v+ x
  26. {6 x! A5 M! o+ t3 H5 j; V9 d" u, P
  27.     int i, flag=1;
    ; k# _: a$ ]/ l6 k. d: {/ q
  28.     for(i=2; i<=n/2; ++i)) J. _8 z' h* j0 V; M& }
  29.        if(n%i==0)
    + {0 M" a. v: `: V) W" v1 w6 P) b
  30.           flag=0;/ l: A$ x, ^0 z) U# M
  31.     return flag;
    ! b  J% n5 S/ D7 G! }% |
  32. }
复制代码
0 p( F. J. I0 J& R* Q5 ]
结果输出:
  1. / n, H& Q8 X- k: A. y
  2. Enter a positive integer: 34
    $ I' O) g( r' J; N. _" l8 I
  3. 34 = 3 + 31
    - @6 q6 T7 {0 o; o: }7 P
  4. 34 = 5 + 29  [8 ?3 ^' N4 s9 w2 ?
  5. 34 = 11 + 23
    " Z1 H( T) d1 Y, P5 v
  6. 34 = 17 + 17
复制代码

0 Z0 z: N. L- R3 X

9 n$ f7 z% d! H: @6 x* U
7、用递归的方式颠倒字符串
7 l* t3 A" _4 x! l( Y! @1 J6 {8 Q
源代码:

  1. 0 m& G: u, k# e  [$ P7 A; I
  2. /* Example to reverse a sentence entered by user without using strings. */: J0 q2 P1 K" I

  3. ' P- S! B4 S0 V2 E; C4 }$ W
  4. #include <stdio.h>
    - p/ U, t; \  p2 J$ [, S( }0 M0 L
  5. void Reverse();
    1 D- y- _2 F1 {' f; p' l/ X- K
  6. int main()* x8 g* o8 [. S; \% u# S- _+ L
  7. {
    0 `) [7 r3 C0 Y4 }/ m9 j7 x
  8.     printf("Enter a sentence: ");9 I, f% Z2 w- n- R  s; G' z- c. H
  9.     Reverse();
    5 z& D5 E9 h% n3 _
  10.     return 0;# E  I* E7 I/ l5 A) _) y
  11. }
    3 O* E% I% F. p1 y: w6 \
  12. void Reverse()
    9 S$ d# G! F" I  q+ n
  13. {
    5 t1 ^  C6 {2 r& F  h
  14.     char c;9 r* y1 R2 x- N% ?
  15.     scanf("%c",&c);6 n) p- \* g: i% E. ~% b* P8 Y3 H$ f
  16.     if( c != '\n')
    ' O$ A) N/ b5 d6 u+ G
  17.     {
    6 B3 v! A8 m8 [  |) k; k1 i
  18.         Reverse();
    % m' q' `5 r+ O! m* T8 }$ V0 O/ v
  19.         printf("%c",c);+ i  C+ O0 `' d: m9 \3 X" ]
  20.     }" o1 l* Z0 u! U
  21. }
复制代码

4 c7 l1 a; D, A1 G
结果输出:
  1. 3 S, n3 {% z4 N, W. ~! |5 n
  2. Enter a sentence: margorp emosewa
    + g+ l2 S5 s) J' Y+ }
  3. awesome program
复制代码
( a% K# m. A" s: j% t$ s
$ q5 n  l% U+ ~& M+ }
8、实现二进制与十进制之间的相互转换
  1. / ^5 x+ C8 k; L3 j$ S% r7 w  ?$ O; R+ z
  2. /* C programming source code to convert either binary to decimal or decimal to binary according to data entered by user. */
    # Z9 `: a9 A3 n, K# Z0 N

  3. 2 R, {" r! ^- ?; z0 H. I: P' `& S
  4. #include <stdio.h>. u3 O8 {' ~9 Q( }* O" E- j& \, J
  5. #include <math.h>
    ' ]9 k* {% S7 `' V
  6. int binary_decimal(int n);
    ! J. G9 Z. S0 N. ^
  7. int decimal_binary(int n);
    ( K( e2 r* X0 o% ]4 \* U: N/ Z
  8. int main()
    . Q2 e! {. M4 x9 O
  9. {
    % ~2 u5 g) ?+ c/ }  L* @2 ]
  10.    int n;( a; y6 K- W6 s' z7 k7 Z
  11.    char c;  X7 I  ]* Y  y6 W3 B- M& E
  12.    printf("Instructions:\n");$ h7 u$ S  ~' O/ w
  13.    printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
    7 a, _$ ^& {4 w6 u- B9 z
  14.    printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
    * m' W0 N) s( F1 @
  15.    scanf("%c",&c);2 V& g9 [- d. g  ]1 j8 j
  16.    if (c =='d' || c == 'D')/ X+ j) S6 z0 w- d* m
  17.    {4 _' Z1 j8 i, O0 Z
  18.        printf("Enter a binary number: ");
    . |& O- O% t3 N' ^/ v% A$ g
  19.        scanf("%d", &n);# A5 f: B3 j$ Y% g
  20.        printf("%d in binary = %d in decimal", n, binary_decimal(n));" t# j- `) ^$ o4 u% e1 t' n9 E
  21.    }
    5 i0 _/ A1 C$ [4 y- Z0 S
  22.    if (c =='b' || c == 'B')$ W" |1 C4 n( q, L/ Z% L( g
  23.    {- @1 }! n" ?4 z/ d
  24.        printf("Enter a decimal number: ");$ q) P7 @- F; R7 b
  25.        scanf("%d", &n);5 R# b( l2 Y1 z, G
  26.        printf("%d in decimal = %d in binary", n, decimal_binary(n));
    ( C  z& E& U# J) D. S
  27.    }
    ; J; f! d' r+ s: y. g
  28.    return 0;; d4 V0 P" z2 n2 A0 Z4 o
  29. }
    / V* v) h2 H& R' F! i& {6 Q

  30. ( X3 X5 Z+ v" H2 {% S( _  q
  31. int decimal_binary(int n)  /* Function to convert decimal to binary.*/! [# U6 p' y* h: v4 Z
  32. {& B1 N" I3 o% x/ P: D
  33.     int rem, i=1, binary=0;
    / W2 k, W$ _4 G8 P
  34.     while (n!=0)
      p8 E7 P( m- _! Y. j8 u
  35.     {' r& w; d: c5 I( Z
  36.         rem=n%2;
    ! q6 k6 Q- @# e
  37.         n/=2;4 l6 }( U0 t! T3 X  G
  38.         binary+=rem*i;2 ~6 M0 ^' `  x7 \' @) k/ c/ v
  39.         i*=10;
    . X( }: f, @+ q$ q
  40.     }
    9 \+ d  i& D+ L0 U
  41.     return binary;
    3 D, B5 @% {, H# x" C, _, b2 V0 |, O' C
  42. }
    # ?) L$ d# X  |0 b' s

  43. : \3 P. C, B8 J1 W/ r9 j: J* w5 C( P
  44. int binary_decimal(int n) /* Function to convert binary to decimal.*/
    0 N: {- e# J( E. T0 M6 Y2 v2 q4 m
  45. {
    ! N9 X) `; N# b
  46.     int decimal=0, i=0, rem;
    0 u2 Z( ^  p1 P
  47.     while (n!=0)
    $ L# a) R. k/ t) P& x, b
  48.     {" \# [( J; T) _
  49.         rem = n%10;
    . @0 e3 v2 ?. Z% a" [
  50.         n/=10;9 A$ ^$ F: n7 v1 D) y; y4 C
  51.         decimal += rem*pow(2,i);8 k; S9 o1 \# u4 p; y7 E
  52.         ++i;
    9 v0 @( P' v+ k) _0 G
  53.     }
    0 W: P$ @3 J- K; ?; E8 B
  54.     return decimal;0 f) `" e7 i1 F
  55. }
复制代码

& o9 y7 Q7 y$ F- |/ W9 K5 r  W

' X) v8 s4 U2 I' d& E
结果输出:
2 d/ Y; D" J' ]8 v+ ?" B* T: o
9、使用多维数组实现两个矩阵的相加

; f9 r4 S4 ^2 R0 Y+ l- p
源代码:
  1. ) v5 t) V, D0 C% }$ T
  2. #include <stdio.h>( U% o+ q. Z- \2 m+ q8 F0 V
  3. int main(){7 D- h% ^. l7 U  ?3 r" a9 D7 E
  4.     int r,c,a[100][100],b[100][100],sum[100][100],i,j;+ n$ g8 ?, `  E# E- R: ]  H
  5.     printf("Enter number of rows (between 1 and 100): ");
    . J; x, Y. J, v5 w& J8 b* S% D+ p
  6.     scanf("%d",&r);
    5 a: p& n& [8 }2 |% C
  7.     printf("Enter number of columns (between 1 and 100): ");% Z/ ?6 U6 q0 ^7 ~) a) e
  8.     scanf("%d",&c);
    3 k! ~5 u  i4 s+ L( h, N
  9.     printf("\nEnter elements of 1st matrix:\n");
    ) F; |% O4 y6 g  @* n. g

  10. * Z0 [) b8 n: @( Z( q7 j- X
  11. /* Storing elements of first matrix entered by user. */
    0 |1 t  N! I" T1 F: k
  12.   `2 }$ Z' a: |8 L
  13.     for(i=0;i<r;++i)0 L% T$ d6 G/ g4 M* j
  14.        for(j=0;j<c;++j)
    3 l; a7 {% E5 \6 k% Z3 }
  15.        {9 q2 O0 N/ X3 t4 k! p0 T0 u
  16.            printf("Enter element a%d%d: ",i+1,j+1);& B" `$ ~4 I& y" [- Z( C
  17.            scanf("%d",&a[i][j]);
    ( I4 e- [, A; f7 G
  18.        }* s7 ^, z) l0 u8 L9 b

  19. ; ?, L! a8 \4 |) P; v- K
  20. /* Storing elements of second matrix entered by user. */; z0 F1 c0 ?  V0 x/ i: E. J

  21. - x. h5 ?4 ~9 |  E: S
  22.     printf("Enter elements of 2nd matrix:\n");
    4 C$ r: A" ?0 I/ [" g& g
  23.     for(i=0;i<r;++i)
    $ M5 V6 k& R3 I7 Z& y
  24.        for(j=0;j<c;++j)) s4 Y, ^/ }. w
  25.        {! y: t$ o6 F) \: ^; e6 `
  26.            printf("Enter element a%d%d: ",i+1,j+1);
    " t$ d" \) z3 @; g9 C4 l
  27.            scanf("%d",&b[i][j]);2 t* S% |* a1 B2 r* J5 W. d, W
  28.        }; p7 N/ j4 `6 C  s

  29. 1 ?; g' i' n4 D2 Q, A0 y
  30. /*Adding Two matrices */
    0 g& }' `2 w! @
  31. ' F( @) Y2 ?% ]- }4 p( R3 H- h/ `
  32.    for(i=0;i<r;++i)/ P- E8 f6 q- t+ x( S' J9 t
  33.        for(j=0;j<c;++j)
    8 {" X7 Z8 D8 l, K( B  R
  34.            sum[i][j]=a[i][j]+b[i][j];0 B1 M5 f1 Y& X" w
  35. . u7 ~: j! ~7 S. y/ Z2 W
  36. /* Displaying the resultant sum matrix. */
    4 _* Z0 U; q+ K: h2 t& A: h
  37. 9 v) q4 h7 U- @3 ]' j* F" C$ X
  38.     printf("\nSum of two matrix is: \n\n");6 h4 v; O* Z: {8 \: o
  39.     for(i=0;i<r;++i)
    0 h  A, n% g2 S( s1 a
  40.        for(j=0;j<c;++j)
    + d2 |6 q, d! r* K9 y6 g( E2 @2 Q
  41.        {1 X, J. o0 M* ^, k% B8 p
  42.            printf("%d   ",sum[i][j]);
    3 p+ t" [. O9 P2 {- z8 E' [: q
  43.            if(j==c-1): e/ m7 R5 T/ v0 }
  44.                printf("\n\n");. l6 o4 W7 `$ {6 g
  45.        }& u4 ^5 m" N+ V
  46. 2 I& y& w4 k4 \1 \
  47.     return 0;( i5 o3 J3 [5 Z  J$ s- D
  48. }
复制代码
$ ?' G2 c# ]. A4 d: R; e
结果输出:

* \* J5 ~  Z) j) @6 I5 p. x+ M
10、矩阵转置
, U& w; \' y/ w0 |. w8 z& g/ ^9 I
源代码:
  1. . B6 U& {* U1 Q; b
  2. #include <stdio.h>
    & |$ v0 m) G# s0 `
  3. int main()
    4 r( `( s& n8 N3 M# N
  4. {* c9 \- |- p3 m& t
  5.     int a[10][10], trans[10][10], r, c, i, j;0 e. f; ]) t* ^% ?
  6.     printf("Enter rows and column of matrix: ");
    & n/ ^9 e/ b/ a9 r: ^1 r
  7.     scanf("%d %d", &r, &c);7 R8 r9 _4 f+ u# p% S8 i& Z
  8. : V* T0 t5 K3 _& P0 v- M) x( i
  9. /* Storing element of matrix entered by user in array a[][]. */4 w& K2 H1 B' A" F
  10.     printf("\nEnter elements of matrix:\n");
    8 e5 h2 O; f3 o
  11.     for(i=0; i<r; ++i)9 W, k: ~) N1 K) j& E5 {
  12.     for(j=0; j<c; ++j), G; X+ b9 H$ Z. g8 N4 R! W
  13.     {. ~' a$ b$ t. k$ c% r) f
  14.         printf("Enter elements a%d%d: ",i+1,j+1);
      f3 x% }* c6 {! K" i: U; n
  15.         scanf("%d",&a[i][j]);
    ; s5 `( u6 S( f( S0 H5 M
  16.     }! A/ S" g' \6 `' w* m% |% H9 |
  17. /* Displaying the matrix a[][] */
    # m# n. o" h7 R8 O, @9 [# ?+ A
  18.     printf("\nEntered Matrix: \n");4 m* ^( p* u" C6 n6 M$ D
  19.     for(i=0; i<r; ++i)( H9 D1 a9 K( i/ P' p
  20.     for(j=0; j<c; ++j), j( Y) {4 k2 V
  21.     {1 Q% l$ Q: C0 r+ g- u2 E: }  E
  22.         printf("%d  ",a[i][j]);; `/ h% Z. }+ K6 G8 a5 B6 G! V
  23.         if(j==c-1)) B" [" i6 K- b4 l
  24.             printf("\n\n");2 _" I- m& o7 `% n. V  @% H
  25.     }
    6 ^' c* i8 _! [6 q7 I
  26. % _: h, H1 L; M
  27. /* Finding transpose of matrix a[][] and storing it in array trans[][]. */' w% v3 v: G+ J( u2 R: n
  28.     for(i=0; i<r; ++i)2 P, }' ?3 n1 n" Q( V
  29.     for(j=0; j<c; ++j)# |% i- w# o" j  ?4 m  G9 D  K
  30.     {
    9 Y, A" S/ T2 {* {
  31.        trans[j][i]=a[i][j];  d+ `0 E! ]/ i- g) z$ }' z
  32.     }
    7 f$ i4 k' G/ X$ _$ x

  33. 5 _! R# w" I3 N' X, s! C! G, F
  34. /* Displaying the transpose,i.e, Displaying array trans[][]. */
    $ Y% d% n' y! z
  35.     printf("\nTranspose of Matrix:\n");2 x7 I& ]: Y  j; N2 `) H3 u
  36.     for(i=0; i<c; ++i)2 f8 W# N1 y) Q, G! V7 B* v; t
  37.     for(j=0; j<r; ++j)8 _7 \7 h8 _  D
  38.     {
    ' U7 t6 q7 {1 X
  39.         printf("%d  ",trans[i][j]);
    ( J: Q8 V) p1 Q" v% u1 l2 e
  40.         if(j==r-1)
    ! I: R  {  L3 Q6 b, N
  41.             printf("\n\n");, P: Z+ W& d5 P8 x3 B1 X
  42.     }* d1 h, L' M' m- h
  43.     return 0;0 e$ t0 x" O5 U" a+ i8 V
  44. }
复制代码
赞 收藏 评论0 发布时间:2020-11-11 18:51

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版