找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 2228|回复: 1

C语言算法经典87-94

[复制链接]
发表于 2017-10-12 10:14:07 | 显示全部楼层 |阅读模式
【程序87
% T! i) \6 ~: |. \, C" N4 U题目:回答结果(结构体变量传递)
! J0 F& }! k8 p; y, D& \1.程序分析:
0 h. A( X- U4 u7 f; R; V7 W2.程序源代码: . h$ k$ U; P; g/ D; Z# y4 _9 `
#include "stdio.h"
( f( Q) _- h) N6 h& m% ~struct student
7 v* a8 @8 D+ n, Y7 s+ U{ int x; 7 B2 t  O5 P" h! K
char c; ' z5 J/ m: V2 Q( ?6 K: A
} a; 2 i% }4 }3 @' e8 ?, ^
main() 0 s- R: x( u  x1 s# c
{a.x=3; 6 Z- \7 J9 K& I% r
a.c='a'; ! j  U' M: L. S6 W5 r1 u
f(a);
( f0 U) @1 \5 Y9 {printf("%d,%c",a.x,a.c); ) a/ V; W1 _( S/ D& c! y
} 1 G) D/ g. I; `2 y8 R
f(struct student b)
& v! c3 T8 F" N$ C; i! r( I{
4 t- ^% z1 L; G# A# jb.x=20; ( h$ w+ r6 F$ U$ x. D
b.c='y';
9 u  N/ p6 ^9 p; K; V} " k- i! R( a! o. Z+ d' M! T' v; [' `$ f
============================================================== * D! T& C8 p7 M4 ]0 L
【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要
9 I$ J. B$ }$ V6 p: f题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。 . M9 S" i% `* U7 t
1.程序分析:
8 R  e( x3 j: n/ l* b7 Q6 Q) J2.程序源代码: 4 N9 F, k- ]. Q4 g5 T4 c5 P
main()
% o" z" P, d% H{int i,a,n=1; ) }4 |: U7 ?. `/ z) n
while(n<=7)
4 b  ?8 g9 C; s/ A! w. U8 B" O{ do {
7 m, Z! e8 D! C! l! A4 Mscanf("%d",&a);
% K3 J! M- k2 f9 W" i: g}while(a<1||a>50); . X( y; Y  N: O, ]
for(i=1;i<=a;i++)
8 R2 M- t7 Y% c: E) h2 v: vprintf("*");   H0 ~- A( a; w2 O6 C& p0 X
printf("\n");
* g4 K6 ^' f) r7 J7 w% wn++;}
% q. d/ H% E5 g8 o  ^7 D- G$ z6 Tgetch();
7 R+ h7 ^- F# o2 h# M} ' u, T/ b- \* ^' a" F- s$ L
==============================================================
& R3 \0 a: {. r' c, Y. b' w5 S【程序89
8 J! F  K+ W/ R2 U: J题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
0 e$ |+ L; v" ^7 h5 z) i; Q每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
3 k2 C* a  w3 [1.程序分析:
3 [0 ]4 A5 ?2 m* `7 s- I! G: }+ g2.程序源代码:
, ^& j* d% r4 bmain()   k0 o7 U. ]. [# U
{int a,i,aa[4],t; . S8 E3 x  Z0 t0 e
scanf("%d",&a);
) w* K3 n/ M( f4 z" P) Baa[0]=a ; 7 {) h2 `. J  J9 n2 @3 V
aa[1]=a 0/10; ) G6 X2 g, n( T9 J; h6 w  q8 ]$ _. I
aa[2]=a 00/100; : t& ^0 T0 q. o/ s" z
aa[3]=a/1000;
. L  b8 V7 ~  O7 ofor(i=0;i<=3;i++) ! P% m, M' C- R
{aa+=5;
4 N) R" w- M2 V# y: W! j4 V! C& Aaa%=10;
9 e* v, Q6 j" h5 d; K}
- a- x. P7 G, }+ |! Ffor(i=0;i<=3/2;i++)
2 c7 x. R5 H, c+ K9 R) C; p{t=aa; ) T+ k7 j4 t5 l, n3 A8 D7 G
aa=aa[3-i];
, m% [! Q8 B  P/ ~& j+ uaa[3-i]=t;
4 }; J2 g# e$ X- z9 h}
. y; s' `; X/ v  N% X4 [( ifor(i=3;i>=0;i--)
! E; d6 p$ C- p. _printf("%d",aa); , I' N# p6 e0 R
} 7 ~& u' y; R7 N5 ~( m+ ]. B
==============================================================
. N$ d( A# Y) Q+ ~; ~【程序909 C. r4 n0 g( \( [# Z5 c
题目:专升本一题,读结果。 ! z% ^$ D8 B# l% [& K; K) `3 Z
1.程序分析: ' q6 B) {, k/ p: T( R* S% h
2.程序源代码: % ?, Z  a2 e( n, L2 Z5 P5 n9 i# G
#include "stdio.h" : l- p. o  p" Y
#define M 5
# K9 L& k1 {. F$ Q! U( ?/ W/ Bmain()
! W" o4 ~0 j8 n{int a[M]={1,2,3,4,5};
1 S! H( @) ~& l4 |! Vint i,j,t; ; p) e8 F& {2 m& Q4 d
i=0;j=M-1;
0 r( `' J9 q4 ]% Q% _while(i {t=*(a+i); 0 G0 j0 z* j) I( H
*(a+i)=*(a+j);
) ~7 q* F* p7 p5 }6 u*(a+j)=t; 0 l! q6 C( ^6 G2 z' k/ v$ V3 i
i++;j--; - K  D$ o. \' o6 R" c
} . A) h" @0 R" Q8 [
for(i=0;i printf("%d",*(a+i)); : Z" Q" Q6 g* K( Q  j: {) C9 |
} % x" K. B2 x8 Z4 A
【程序91: c2 r  a/ \1 o. H4 m
题目:时间函数举例1 $ t- ~( g) |: h2 U% [/ s9 h
1.程序分析: - l# ^/ l9 ]4 c: L" j
2.程序源代码:
9 o7 _* m. [* O" `) m" C! @4 f  Z4 z#include "stdio.h"
) J5 C0 Y- O# _4 ^#include "time.h" 4 F# s4 q1 n% g  P. b/ i5 X
void main() . f) s. ~* ?8 `* _. C
{ time_t lt; /*define a longint time varible*/ 1 H! u" Y9 i" ~- E  e( t
lt=time(NULL);/*system time and date*/
9 ~1 k% ~  V# s# k8 `- J7 Sprintf(ctime(<)); /*english format output*/ 5 Y% u' }$ \  h% Z9 ]
printf(asctime(localtime(<)));/*tranfer to tm*/ 7 q# V7 F9 ~1 f8 g7 U" X4 v0 \  _6 k
printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ 2 ~5 _+ O8 i; N  L2 Z
}
$ K4 X6 I# F; |' S% _. Y( O8 F==============================================================
$ W; l5 z& u+ L, C  b+ H【程序923 c. M+ W. Y" [) o( C+ K
题目:时间函数举例2 9 V/ f. r7 k; J% a7 a. X0 X
1.程序分析:
1 G& Q! U7 D# h2.程序源代码: 5 s. e" j: |  F$ m3 \
/*calculate time*/ 5 j; Q4 y: N/ p( Q
#include "time.h" # Y0 ?2 o5 k0 S# p
#include "stdio.h" 0 a# `. x. ^# v. _# ~9 c- a+ P; P- p
main()
# w- M7 d) U& P{ time_t start,end; . u3 }1 W0 r" N) o
int i; " G% R) r* O7 U- L4 X
start=time(NULL);
9 V/ {6 R) A/ e# O7 c) j8 S) Lfor(i=0;i<3000;i++) 9 B" U9 W- X- s  e
{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
; W1 Q4 h  r* k/ X+ Kend=time(NULL);
' j$ x6 e4 t" i7 L0 S. P4 `printf("\1: The different is %6.3f\n",difftime(end,start)); / w: B/ ?; P/ b: o( m- {" z
}
+ F: g) g* j/ d* {: i# W============================================================== , l0 I  r1 N# l7 o
【程序93
& ?1 Y5 M8 W, v; Z7 x8 |- X( e5 r题目:时间函数举例3 9 V& j5 @" M- L
1.程序分析: 1 r! S7 h0 }5 U2 y0 n
2.程序源代码: 7 y# G/ j6 ]3 ^( K3 e  Y% i
/*calculate time*/
3 W& i( M3 p- U9 v/ A* ~#include "time.h" / A, ]2 j- P  f  k
#include "stdio.h" - m, ~3 R. A- X1 Q
main() / n. }* z8 h* ^( A4 |
{ clock_t start,end; - z5 c& v5 @" n& P, y! {1 E
int i; ! Y7 t3 W: Y0 U; `
double var;
5 c6 ?* d3 S6 x! w. U+ }start=clock();
, {) ~5 `6 t1 Afor(i=0;i<10000;i++)
5 q# |8 ~2 N# ]9 h4 e{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
5 w8 T' Y: y* t0 j- u& t2 H% |end=clock(); * F/ z1 ~" O+ Q4 e( X
printf("\1: The different is %6.3f\n",(double)(end-start));
2 d! r# `) E8 X7 X* k} $ g" ^% |* ^4 c1 l; s" s$ \
==============================================================
0 \; Y0 l- ?/ m% M7 _【程序94" z* G2 a- H3 g( F* t
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
' I3 E- O/ [  H4 U2 A4 V; e1.程序分析: . J* G# G% k% @. U, Q8 P
2.程序源代码: 3 }! X: A- Y& ~  ~
#include "time.h" ; E2 q! `1 @; G7 v7 W5 J
#include "stdlib.h" 5 L9 K) g, u1 c3 N2 f& I
#include "stdio.h"
+ L6 c. m1 Q& L+ c% jmain() ( m+ i7 f' z& X
{char c;
: r1 c9 A7 G) V9 }) Nclock_t start,end; 3 }9 f3 p! o1 Q: m: W% E. g" b
time_t a,b;
! j' L1 \: G  G+ G5 f( L0 Ydouble var; . x5 W7 V8 [# V( R
int i,guess;
& \2 r4 w3 I! W& [, o; Wsrand(time(NULL));
' }5 H3 l0 y2 ^3 u: j  J( Wprintf("do you want to play it.('y' or 'n') \n"); ( Z1 L- l5 F7 U- m/ f5 ^
loop:
" g3 f4 B2 y; @4 Awhile((c=getchar())=='y')
5 t! C7 A9 O; Y& o{
+ V5 P! a; z! K7 K2 o( ji=rand() 0; ) k) I9 h0 L) O8 \; [6 x
printf("\nplease input number you guess:\n"); * t9 t& v; w& R3 A
start=clock();
0 S; j4 b+ Y) R1 f, V! Oa=time(NULL);
+ @! D, W; H0 S7 r' W" K0 Tscanf("%d",&guess); : b2 K$ `8 G( K$ I/ F9 t# I
while(guess!=i)
8 ~( ^$ g' C2 N3 b{if(guess>i)
: X0 @2 c5 V2 F8 c{printf("please input a little smaller.\n");
' e" G- g" I0 {' q; ^9 oscanf("%d",&guess);} & I7 p5 _/ i) U# \# c
else ( x& t; d" d; w& {
{printf("please input a little bigger.\n"); 1 D' i! G7 _; E: j4 j
scanf("%d",&guess);} ; `: Z. `, J5 E" U4 ?$ q+ D; K: j
}
. p/ I- A, e2 c" A# P7 nend=clock(); 9 {4 r- h. V; X! o
b=time(NULL);
& l6 K2 W, E2 w$ x( b& R% ?5 m8 Nprintf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
- Z3 E5 \5 U" z1 H% J+ Vprintf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
0 g& F6 w( P" @0 F9 u/ Z7 v& kif(var<15) + {% {% c5 p4 w% F& \" i1 z2 i5 l
printf("\1\1 You are very clever! \1\1\n\n");
8 \9 O: s4 e2 d0 R6 |else if(var<25) % w3 T1 H3 I9 O) j! A
printf("\1\1 you are normal! \1\1\n\n");
" z( s! x! w+ W6 J! [8 Oelse - B" }9 }) y3 ]
printf("\1\1 you are stupid! \1\1\n\n"); % C; T9 y2 e, E! x2 H* G
printf("\1\1 Congradulations \1\1\n\n");
+ s3 u, n5 k! Y( ~4 a, ^6 M( `: eprintf("The number you guess is %d",i);
/ M' M9 w7 g% }} 4 R% F' j0 b: H" c
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
3 R$ q& K; v0 d* Bif((c=getch())=='y') . Z5 k( D2 C8 J: |1 a
goto loop; 0 P" M0 W- e. F; V% J; C- p" j2 b
}
$ E. S5 w5 A4 |9 u
回复

使用道具 举报

发表于 2017-10-12 10:29:05 | 显示全部楼层
我一路跟着学习。谢谢楼主!
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Archiver|手机版|小黑屋|机械社区 ( 京ICP备10217105号-1,京ICP证050210号,浙公网安备33038202004372号 )

GMT+8, 2025-7-2 09:07 , Processed in 0.064244 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表