EDA365电子工程师网

标题: C语言经典算法87-94 [打印本页]

作者: dilidili    时间: 2017-10-12 09:21
标题: C语言经典算法87-94
【程序87
1 v7 H9 ]: h' N5 c, d- {* ]8 A: ?  J题目:回答结果(结构体变量传递) $ L9 I; c. A4 C+ ~- C, M
1.程序分析:
3 X9 z7 I% T! P( y2.程序源代码: ! q& l8 X8 W2 _. H; B% ]+ ~! I, N
#include "stdio.h" ( o6 Y* F. O7 R; r' I3 {+ m
struct student
' Z% ~% L) |: y' A7 \- Z{ int x;
- u8 n% O) t) ^) Ychar c; ! ^% O) m4 l+ l' w
} a;
4 _" v- F9 A1 e/ R6 K) A, cmain()
7 g5 I& ~* c* M" d{a.x=3; 2 G) w9 W! {5 @5 f* S/ r  F) z4 O
a.c='a'; 3 h# r7 R( b" r3 {
f(a);
) F) `+ k& z# {' Lprintf("%d,%c",a.x,a.c);
. c. [( c( a6 A: s, T+ j5 b} ! E) X5 M! J2 G# D) R
f(struct student b) + B* D/ Y  }4 Y* a; J2 L. ^. v7 W
{ , G9 X% i' y% L2 B7 ?; \$ f
b.x=20; - t7 m5 `/ {9 R' {5 [: _
b.c='y'; 1 j5 Z" |, K  |0 _
}
4 [% f0 o; V$ I2 k) t7 S9 l( _& ]/ w==============================================================
% K  ?/ D% t$ c) {! Y7 L【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要 " O' X4 z6 |: t1 E2 k
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。 1 Z5 `- O7 {; A) M, P6 g
1.程序分析:
3 }+ X5 D' C3 U+ Q: ~" f2.程序源代码:
, B* o! ~; v8 V. @, o; Xmain()
5 j2 N9 a. W. j  f9 R. Z" o# M  t{int i,a,n=1; 4 M' m2 z1 P0 e/ s- X3 ~
while(n<=7)
  K2 n4 R8 K7 T+ N$ ?: {5 p{ do {
3 z9 n/ ~- _% z- Kscanf("%d",&a); / R9 C4 _. D( J
}while(a<1||a>50); 2 N- Q# I0 _% a
for(i=1;i<=a;i++)
! n2 Y9 M5 Q3 n3 V4 C! A; Uprintf("*");
6 R5 V4 g0 x1 s( ^% o/ e' s* ]2 w) O+ nprintf("\n"); 4 D9 a; B  r( h* x, o4 d6 x
n++;} 2 Y" G: e1 p+ x1 s! X  _
getch();
7 x: e- n: l* r4 f} 8 B6 m. N' B2 D# F5 G7 D
============================================================== / A1 |0 s: m% y: r
【程序89
' Q( z) \) O, Q  z9 B2 {, I题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
0 ?( k( I# F! N& N每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
- X5 N& P/ f1 E/ b6 T% z1.程序分析:
9 _& ~3 u* [" l! ^. S4 N& L* H2.程序源代码: ; M+ L; h* W6 I3 d  c- z: g
main() * x, s3 d- j; P: U" c( C
{int a,i,aa[4],t; 2 }2 Z2 ~) @) h$ Z/ y
scanf("%d",&a); ; _! l1 C+ H, V6 }* E
aa[0]=a ; ! a; G: S4 A' ^* W! D, ^2 D1 {: K" ~
aa[1]=a 0/10; 6 z7 ?3 V# f  I5 G+ [, i
aa[2]=a 00/100;
" W6 d$ \9 a; c( Aaa[3]=a/1000; 2 k# m" c0 B' j8 f
for(i=0;i<=3;i++) ( n' C% h3 j. V3 [
{aa+=5; : o1 t0 b. W" G6 h+ Z
aa%=10; 8 \8 F8 w$ x$ g0 Y. d% s1 p5 v9 j
} : I7 x* S7 ~0 n9 M/ a2 t
for(i=0;i<=3/2;i++)
5 [8 C! I$ A" b% u2 h{t=aa; ( t: q; i- P( p; k3 I' h3 _- L
aa=aa[3-i];
; z  H* \, n; ^. g. Raa[3-i]=t; 3 P0 A( c0 o0 _: G- d) }  Q
}
$ z# C) Q8 O( j0 bfor(i=3;i>=0;i--)
2 o6 m5 z, {- ^1 R- ]; Hprintf("%d",aa); 3 B' F0 G- i/ S
} % [. b8 r; @5 i8 @- I
==============================================================
; x1 o! B4 g& {3 V【程序90
. o/ F. v/ d1 x! z题目:专升本一题,读结果。
6 R# H; ]1 J9 A( M1.程序分析:
% b3 V+ t3 O: C6 _2.程序源代码:
7 l. ]' P0 ?9 I% _#include "stdio.h" ) C3 p% v) Q7 w+ N/ Y
#define M 5
9 C! y0 [; w- c7 }main() ' E$ L' k4 I; d9 u$ C5 G$ B( F! m
{int a[M]={1,2,3,4,5}; 4 [  Y5 @; Z+ q" P, H' W' V$ u
int i,j,t; % X, ?& j1 I& Z1 U4 R& r* z0 ]
i=0;j=M-1; 6 B/ ^, a% |- I; [9 p; T! z5 j
while(i {t=*(a+i);
7 u' v$ [3 A1 |% M) j7 v5 ~*(a+i)=*(a+j); ' E* O% r8 R9 M, M! I. s& q; F% T
*(a+j)=t;
2 R; S# T" Y; }/ c+ Ti++;j--;   C/ ?/ W, J% O' q4 L
}
' O0 X( ?# T: wfor(i=0;i printf("%d",*(a+i));
; N/ O! M7 e# i6 I- E1 r" _% {}
* P5 ]$ V: C% s) O* r5 d9 Z【程序91& F4 j  u) g4 F: s
题目:时间函数举例1 . c! `  l7 S2 h, {: P6 z2 Y9 D
1.程序分析:
6 H/ u- [. D4 w& M! P2.程序源代码: 7 l8 L% {! |' e: I" }
#include "stdio.h"
: G$ K+ ], N3 ]#include "time.h"
0 n# @+ `4 X+ H, Bvoid main() / d# y+ P5 x& Z$ ]
{ time_t lt; /*define a longint time varible*/ - P# z3 Q1 x  w+ i7 y5 J
lt=time(NULL);/*system time and date*/
! c% j0 F8 C: z' D; }+ P; T4 r4 \5 Nprintf(ctime(<)); /*english format output*/ ) {1 J0 M8 @: L) V: Q/ |; @
printf(asctime(localtime(<)));/*tranfer to tm*/
. c) ]3 Q+ k3 H* \3 d% Eprintf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ 7 V; I6 |' [+ a
} . Y7 q' k5 [  R8 v& T( Y2 a) O8 S
==============================================================
% A' M% v" B& A5 T  U【程序928 H0 c( |. w2 J+ w; \' y; @2 m
题目:时间函数举例2
( q1 J: h; f5 S& E/ Q8 v1.程序分析:
. a  `: h" [1 s2 h. `) O6 R- G2.程序源代码:
, s/ Q$ P/ E( T' h3 A3 y5 B/*calculate time*/
+ E% ]5 v& e1 V6 w#include "time.h"
* u) m, i6 y$ t: K0 L#include "stdio.h"
! e" ?7 m& t- D- V. [" Wmain() 4 Y2 d3 r# B% S4 B. Q; q
{ time_t start,end; * u/ u7 v/ F: z- S( i. ^$ t
int i; 8 f1 g- `" p5 h1 h& s
start=time(NULL); 4 Y1 n. W' _6 N- c1 w
for(i=0;i<3000;i++) $ x+ C+ P# K2 I+ z  p
{ printf("\1\1\1\1\1\1\1\1\1\1\n");} ' x& i6 k) U9 E- ^0 x
end=time(NULL); : S; B: h2 c1 Y- j+ R0 ?
printf("\1: The different is %6.3f\n",difftime(end,start));
: k7 j1 o9 k, @! ?! G}
6 Q  E7 x* N3 K) l==============================================================
3 g3 v, ?8 D) j: o3 I) e【程序93
6 C9 l+ N% \9 s( X: G) d题目:时间函数举例3 2 ]+ _( J% n+ S: a6 Q' ^
1.程序分析: 9 J3 `) F7 }( ^
2.程序源代码:
1 b4 C4 @* Y. S. ?& o4 [0 R/*calculate time*/ : T/ I4 {1 h" c3 N7 Z/ W, H
#include "time.h"
; c% r6 i5 b, ?- N% c#include "stdio.h"
8 e% o' o+ V. H4 h/ Zmain()
/ O; I: c$ ?) d* N{ clock_t start,end;
: {; x) h0 U9 i; O4 Pint i;
! _0 K& n) |: Fdouble var; # r" ^. S- h9 {- O: N
start=clock();
  R& Q4 T# G, p/ U6 Kfor(i=0;i<10000;i++)
6 A/ O# G, `' p4 w6 n9 ?9 _{ printf("\1\1\1\1\1\1\1\1\1\1\n");} 7 s- c* G/ ^+ h3 Q
end=clock(); 9 W! j+ M0 r9 u: W1 g$ g. z
printf("\1: The different is %6.3f\n",(double)(end-start));
8 |# Z& w3 a4 J3 C) j2 |$ K, @}
2 k# j9 H2 S1 E+ J' K+ j: M3 r0 g; M============================================================== : @5 @* D" |  s, Q; w9 f
【程序94
) X( y; m; K& L) D/ W; [题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的) ' \8 y# g- W3 A
1.程序分析:
; ?8 m6 s. N; E5 f. y- P2.程序源代码:
4 e! \6 _4 j3 U5 Q6 \* d( ~9 K! O6 q#include "time.h"
( V( Z: v# Y: C4 t8 _#include "stdlib.h"
6 V! P8 K" I/ r2 H0 e4 E$ m( d: x#include "stdio.h"
" M8 b5 F9 I9 |, t) a1 @" Qmain()
5 `5 y: X8 P9 X{char c;
# C7 Z) |. w3 S. ?6 L. uclock_t start,end; 9 I; d5 z) s3 `; C
time_t a,b; 7 @8 q; q8 X( j: x6 s# G
double var; 1 }6 b3 v2 M# b# W7 a
int i,guess;
9 R6 m- ~3 @; ?6 [srand(time(NULL));
5 t2 B' I. l1 {# M( D$ tprintf("do you want to play it.('y' or 'n') \n");
' @$ ^! Z6 y) `9 G& E2 ^) C% b% ploop:
0 e8 p: K4 \2 U( @9 Owhile((c=getchar())=='y') 7 N: X' Q4 ?8 l0 F7 c) }
{
! Y! ^. z) V! Oi=rand() 0;
/ [4 }, s8 Q& X2 m( R9 t2 kprintf("\nplease input number you guess:\n"); $ z9 r0 {" e3 J- P$ ~( z* M
start=clock(); % R3 h' d% I) j7 J5 w/ K
a=time(NULL);
& O* ^2 q: `) i9 ^9 Uscanf("%d",&guess);
6 I6 W4 X' Z/ w( t2 y6 m( F" g# Mwhile(guess!=i) & q6 I" n. L. W) r' W
{if(guess>i) 3 o5 s, V& t" R: d0 x8 Q( p
{printf("please input a little smaller.\n");   h( a2 |9 B- H' g# g3 F3 H
scanf("%d",&guess);} ' W; m% q2 ^; c' R4 V
else 5 t9 P6 r! f. Y; e8 M- ~. k  m* ]0 K
{printf("please input a little bigger.\n"); $ _+ N  e( G$ I3 |" r9 L0 Z* A9 R2 i2 S
scanf("%d",&guess);}
3 [: Q1 x9 o( c1 \. R3 f}
# P0 X  l$ E2 h5 Y) z& g* I; s: qend=clock(); 8 l7 {  z: b) P. V; n! j! p2 a
b=time(NULL);
! o1 Y0 N' `, k; C+ s) s" zprintf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
. W' t  A* ^0 g) r/ V3 N8 j+ o, iprintf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
! [! h# }# r* {  }if(var<15)
: C8 w4 L3 M5 G1 @printf("\1\1 You are very clever! \1\1\n\n"); - r4 U% A5 ~; K$ f2 o% _
else if(var<25)
3 p3 [; e; u0 g( H6 h* Bprintf("\1\1 you are normal! \1\1\n\n"); 1 z+ x+ q5 e/ N
else
! i: D$ ]+ G' ?1 z4 Kprintf("\1\1 you are stupid! \1\1\n\n"); : r9 k; s& q) D) ?0 ]
printf("\1\1 Congradulations \1\1\n\n"); # g9 [: [5 p. O- j4 {$ c( v* R
printf("The number you guess is %d",i);
7 r; S! }/ {8 P' p5 s}
) G& ~/ n. x/ v- z( zprintf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
. S! W* x" c5 }if((c=getch())=='y') : a$ i9 L9 h& n8 F* a
goto loop;
8 _& _' K" @2 J4 e. K8 D. U}
. \1 R1 r3 e% {  F, n

作者: liangfs    时间: 2018-5-3 17:36
66666666666666




欢迎光临 EDA365电子工程师网 (http://bbs.elecnest.cn/) Powered by Discuz! X3.2