机械必威体育网址

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

作者: tainqing    时间: 2017-10-12 10:14
标题: C语言算法经典87-94
【程序87
2 T8 X) T% `% \$ S% y2 Y; c  s  k题目:回答结果(结构体变量传递)
1 u1 ]" B, w# S, c2 V, x& V1.程序分析:
9 \7 I% @; w; ^( d0 _; `* Q# F: x2.程序源代码: 5 ^$ b8 Z; _" n! r. S+ M
#include "stdio.h" ' q( K+ N8 N: A, o, g
struct student
: B6 }% G8 a: q# [{ int x;
& |. }, H0 T4 M; ^4 _$ x' S; zchar c;
8 R. q6 a3 ]0 ?} a;
$ h, U7 A2 J( ?- X8 E3 p8 Bmain()
+ h4 N, X; }: H  r& v! ^{a.x=3; 0 N, A; Z$ ~) ~9 p0 _. n1 e; U5 G
a.c='a'; & u! _; B& _5 C( S* {1 g
f(a);
4 F2 f/ _* E7 M  Qprintf("%d,%c",a.x,a.c);
# @  e- [6 i2 v8 ~: W' L: \}
! S6 c5 Q/ G3 v* |4 w0 r  X. o) Sf(struct student b) & k; P* [. V9 m) V
{
& b+ [* r2 a3 M" N2 \) Jb.x=20;
( F6 ~! a2 a9 v, Ub.c='y'; - M) H1 h* ^! e$ K$ G6 f. W
} & ^8 h6 f4 B$ L; e* v% l5 K8 h
============================================================== / o2 i. x/ H* z7 p* e
【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要 6 f+ |- c4 ]  {1 A1 k
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。 / s; {' x8 v. H. U$ C+ v" }+ `
1.程序分析: 1 l9 ^1 S* H+ j( C, `% A
2.程序源代码:
" p. o- {+ \9 F* ~1 F% m# nmain()
2 o+ j& @1 I# i{int i,a,n=1;
* D/ V- v% w; Z  _! d$ Rwhile(n<=7)
7 X+ q7 ^7 ?( D2 S8 I+ e- n{ do { ! Q' b8 c: J- m  w9 X% \3 u
scanf("%d",&a); 8 D! |- K1 N- n; H6 l6 m
}while(a<1||a>50);
6 `, j& t9 F* H% l0 Sfor(i=1;i<=a;i++) % b! v5 |$ ?  n) q
printf("*"); # W) ~& q' E/ R4 W  u4 _; I' m
printf("\n"); ( C) u8 j+ @, b: L
n++;}
. ~; H9 ]: u  E$ L/ X7 X/ f' X" Egetch(); / C$ _2 r5 N1 c0 j
}
( O6 ^; y/ E1 t" K- S7 j& V==============================================================
7 p) B1 m) V% o5 X+ h, V& p6 G; {5 J【程序89
% k5 w' w. G6 A1 x8 K5 }题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: 9 J' D) K+ [5 c% _
每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
# Z( ]3 X: x+ K# J( p; k4 s1.程序分析: $ W& u# a7 B7 w0 ]/ G0 Z* b
2.程序源代码: & c/ [& e/ E$ R1 H) _. d
main() / p+ V+ `! j, E0 `
{int a,i,aa[4],t;
7 X8 G1 h* q$ O, D" @scanf("%d",&a);
0 O" X* ?) x+ a0 i. p$ S* faa[0]=a ;
% {- r6 t/ X0 A$ ~% Eaa[1]=a 0/10; 0 L1 F: M2 C* W
aa[2]=a 00/100; 7 D7 J; N2 N+ [3 P0 d0 Q" V0 ^
aa[3]=a/1000;
" J5 k. O0 n. _& afor(i=0;i<=3;i++) - _, V* K: Z$ e. B8 L% v9 R! z9 p6 U
{aa+=5; $ d$ a. r3 M6 {2 S" h$ J2 @
aa%=10;
2 O2 o' _- l* S$ _6 J4 \}
7 {. v; D% ^% y3 L. a6 I  yfor(i=0;i<=3/2;i++) - m- v  H+ [% J/ O
{t=aa; ! c; W. H7 ]$ e4 Y
aa=aa[3-i]; $ V( G5 a1 [( q, H
aa[3-i]=t;
$ U( b( S, @3 |- g: u2 T( Y} " I9 B6 ~5 b! _. ^7 U& O- ]* \
for(i=3;i>=0;i--)   o# ^4 r; h: E; D) R- B
printf("%d",aa);
  |# f) X0 y2 x0 c* f8 B; V}
4 w0 P( j/ D: q. j3 p2 d/ s8 y* L' ?============================================================== , H8 i4 I" q4 _6 R) y- F% F- O
【程序90' s) t, g5 V* w5 n8 [7 s
题目:专升本一题,读结果。
4 d( P; K2 [  a1 m) K) T0 }/ A9 y* I1.程序分析:
* h1 O- E/ u* X1 G* z2.程序源代码:
) n. S5 |, z0 f; V# k#include "stdio.h"
1 [. h* _( b# H* }0 q#define M 5
1 z& K/ v( F% U; ?/ Z: ]8 Rmain()
: ?) _5 e2 t$ V4 C' a{int a[M]={1,2,3,4,5};
$ n; R7 [- i: f0 |& a* u* X1 Hint i,j,t; + M( s9 D! D, O+ Z
i=0;j=M-1; $ P' J* E1 R, Y- |8 p$ z2 n
while(i {t=*(a+i);
( j3 d' T  u; O% p5 B6 v*(a+i)=*(a+j);
+ e" l/ C" ]9 N0 L5 E9 P6 ]*(a+j)=t;
5 y; C4 c* `, D) W. I0 Ki++;j--;
$ _# f: S- _1 [$ K$ m* @/ B9 A} 5 F+ g1 o3 v0 b
for(i=0;i printf("%d",*(a+i)); * k1 v2 B& U' i$ g0 |/ k
}
$ _, }5 \- u0 e7 U& `# k【程序91
' B! c' L5 b& S: D: I题目:时间函数举例1 6 ~' G$ n: m4 V" q/ r
1.程序分析:
* @( `4 r1 i5 C' S. k3 j' ?) |2.程序源代码:
/ j) y7 t& s% l6 u  C#include "stdio.h" ) f. E6 u2 x; N2 a0 R- c
#include "time.h"
. B: v. W! [4 U+ Cvoid main()
3 m) K' }/ E4 P3 C, b' u{ time_t lt; /*define a longint time varible*/ + s/ _# i' ?+ t0 ]$ o
lt=time(NULL);/*system time and date*/   K% z- x. P8 S% {+ z$ F
printf(ctime(<)); /*english format output*/   x1 }$ Y( i2 z
printf(asctime(localtime(<)));/*tranfer to tm*/
+ x: s+ `2 ^% D4 ]" dprintf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ / ~# B& @9 @1 H9 s4 U9 g& N& z7 A
}
9 G: E7 N/ a2 \$ v============================================================== # a9 k7 A6 P5 g1 @# ?2 b  L
【程序92
  U; y9 `7 |& C+ L# P6 P% P7 R6 i% j. B题目:时间函数举例2
" a1 ]" o1 u5 @3 d  @2 K. V. l1.程序分析:
5 i1 ], E6 k" X6 _+ ~& }2.程序源代码: 8 t8 F. _: n8 b& H3 d& n
/*calculate time*/ ! W; k: A& K7 ~! [( y* g0 x
#include "time.h"
8 c" D+ M3 }: ^% `#include "stdio.h" ' R) a0 _4 R% _6 H5 r/ s
main() # u" U: b/ ^2 u2 R  s
{ time_t start,end;   j0 u) e; y. g) d1 }
int i; : @7 s. t! r) p! ?  h
start=time(NULL); 3 A9 g( r6 R# X! O9 e( W* b% i
for(i=0;i<3000;i++)
4 m( a+ h# P5 f% X1 z# Y3 U" I{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
$ W! _& f- I1 A1 kend=time(NULL);
( ?2 X, N/ ^/ Uprintf("\1: The different is %6.3f\n",difftime(end,start)); 3 B9 V2 d5 n# F9 W
}
' w; s- I& v) a# O$ O9 G* D6 s9 x5 D7 e8 {============================================================== 8 C. j+ ^  i3 k0 b5 H
【程序93
4 p  z3 B, `& z) u$ ^$ M' A题目:时间函数举例3
$ Q4 `! j# E% s0 n  G1.程序分析: 3 t) d% F/ K4 `' h  N7 F2 k
2.程序源代码:
: k0 Z1 h' ?( E$ }/*calculate time*/
; ^0 P" {+ P8 y1 c#include "time.h" # r/ V0 X! b$ i- P/ t4 d" x4 t
#include "stdio.h" 8 k4 R. p, i) P7 c
main() ) z: ^! ~5 K! C. W1 V
{ clock_t start,end;
4 w$ r# n3 ^5 ^) `! j9 t5 {' aint i; 9 t5 q4 Y3 ]) s, |$ K' @
double var;
. U' R, @3 i! x" {. u8 xstart=clock(); ( u6 e1 g! U3 L) ?; @$ q
for(i=0;i<10000;i++)   i9 E0 z; j3 z& l, i  ^- j
{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
2 r4 O6 e; g) U8 I) P3 rend=clock();
$ P! g2 J$ z: t! Vprintf("\1: The different is %6.3f\n",(double)(end-start));
' n, s! c& ?0 x}
1 s( |, j& ^1 a) N' F3 Q4 I============================================================== 4 f+ f9 ]4 |# N3 \
【程序942 |8 S- V, d2 u
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的) 5 Y) V( p4 X- _+ Z4 f, }
1.程序分析: 5 z2 ?" t& m& Y0 R1 P0 K" w
2.程序源代码:
: ?4 A0 q8 q, b( J: E% M#include "time.h"
; K; n7 T9 R+ |3 l5 n$ U#include "stdlib.h" 9 s& T$ ^0 k& C! M- B& p
#include "stdio.h"
4 ]# e) k' Q7 \% C2 d# {6 Hmain() $ D7 q( T8 q' ]/ N$ \
{char c; ) |3 L& L& S  T& z5 i$ l' W1 }
clock_t start,end;
" J0 i" p! U) _$ ~time_t a,b; 3 K9 n# z) Y' d' l
double var; 5 e. i3 \0 B% u& z
int i,guess;
: ^6 C0 @& n; }9 T3 K& E6 usrand(time(NULL));
8 t& I) A8 c0 p* f: zprintf("do you want to play it.('y' or 'n') \n");
: z9 W) K9 i7 Xloop:
7 c$ s; u: p6 @! T. x. i9 Vwhile((c=getchar())=='y')
! `* S' W- i7 k' Y+ G$ J$ W: |{ 6 W' ^3 R$ @9 L5 j
i=rand() 0;
6 ]: E4 X* D! i4 g0 E/ a) Jprintf("\nplease input number you guess:\n"); & J. {, O: a8 C( K% h
start=clock();
* X( ^2 u# I$ b+ N9 ea=time(NULL);
2 J2 @: M& b! a; {) X  \" Pscanf("%d",&guess);
- {; \' l* X; a- mwhile(guess!=i) % J2 x) M5 x# {1 t5 R
{if(guess>i) / M8 K1 q. {5 y
{printf("please input a little smaller.\n"); ; m9 \/ C! x# `
scanf("%d",&guess);} " ~- W+ Z4 h' s6 j( |# E/ ?4 F
else 0 r! L% Y8 F( O
{printf("please input a little bigger.\n");
6 h1 ~' T2 O9 L+ s5 G* ascanf("%d",&guess);}
$ ^1 k: a$ G, N  P7 k: d} # p* Y6 D. [0 ]
end=clock();   ^3 _0 M& ^8 [2 L5 p- f
b=time(NULL);
- ?8 {; a  x- D, A7 }9 N+ O! Xprintf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2); * Z" A+ ~% ?" ^8 `0 X
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
3 g1 Q8 \; `' ?( l, f. _if(var<15) 5 Q9 o% T! M6 ^) \
printf("\1\1 You are very clever! \1\1\n\n");
) H3 y7 G& }, _/ R, qelse if(var<25) # q1 @6 H, [: t! w- ^
printf("\1\1 you are normal! \1\1\n\n"); 4 }# L* b/ o4 w4 t# ?
else
. I& U( Z  c( t7 D8 |printf("\1\1 you are stupid! \1\1\n\n");
+ e7 ^* v. C" q& h9 ~1 r4 K8 Q' H) Hprintf("\1\1 Congradulations \1\1\n\n");
6 h0 G; [4 W+ S$ H% r8 r4 b: q; Kprintf("The number you guess is %d",i);
. A; S" j/ v" v}
  H. i1 F4 a* R* C: oprintf("\ndo you want to try it again?(\"yy\".or.\"n\")\n"); 5 a! p- P# f0 h( v6 Y2 s
if((c=getch())=='y')
- d* q+ f9 y- {9 V; N- P& @goto loop;
: N9 R$ l$ u8 m/ E/ A6 u}

, L( f/ F( M$ ~7 J0 }/ h
作者: moldzsdj    时间: 2017-10-12 10:29
我一路跟着学习。谢谢楼主!




欢迎光临 机械必威体育网址 (//www.szfco.com/) Powered by Discuz! X3.4