找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 2236|回复: 1

C语言算法经典87-94

[复制链接]
发表于 2017-10-12 10:14:07 | 显示全部楼层 |阅读模式
【程序874 k7 F+ m3 V# O( }8 z
题目:回答结果(结构体变量传递)
# A  v7 D* v5 G' r1.程序分析: $ d" N) o/ f$ f2 O
2.程序源代码:
, D+ |; i. I- P8 I#include "stdio.h"
) Q" @' o. m" O) y# ~8 [' k5 F4 ]struct student
: o( q' G' D0 W2 y{ int x;
: C) H" z9 v5 Xchar c; 1 D" V6 o3 W% j' Q; B
} a;   ~/ T. n* ?/ Z1 S
main() 9 ]. ^0 r" ~; A1 }5 s" \( h- J; B
{a.x=3; , V+ c) u% ]- H
a.c='a';
+ F8 `  m/ b3 v9 n; Uf(a);   |& N& y" y  v, L3 ~! k4 i
printf("%d,%c",a.x,a.c); % C/ C6 |8 A8 U7 x2 \( G) f
}
2 F" \( Y" B2 a, I, u& @f(struct student b)   Y9 O+ {& B% b$ u+ w4 `9 I  y1 C
{
8 ~/ K9 }; A1 r  o- ib.x=20;
. ^2 \3 q, s2 d2 C$ p8 fb.c='y';
% Q6 d# n3 k7 Q9 g1 d8 v6 p' g} : ~: {' y2 P5 u2 m3 N' Y
============================================================== * w, K* P5 b. ^% Q& l4 u. M7 ^* V
【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要
8 X9 h0 s6 l  a. ~题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
9 @& ?- v5 H- L" _  J! }- ~1.程序分析:
' D9 N6 |4 n- L: \' T4 s5 X6 l2.程序源代码:
8 s8 o' }; x- D( Lmain() 5 `% q( U8 V& z) U9 F2 \  v0 a) ]9 K
{int i,a,n=1;
: U; d  b4 z' Z) d# m2 w) w* n' gwhile(n<=7) + o- v) a# \8 C' S+ v! p
{ do {
" V  D" K, d5 Qscanf("%d",&a);
6 P  g- t  [2 g- L! q: Z3 R) O}while(a<1||a>50); * D( h, R1 G1 y  z! P7 t( Q
for(i=1;i<=a;i++) 2 @/ W$ j1 H7 n9 V
printf("*");
* V. \, g9 u9 C: `* M" w2 Uprintf("\n"); 1 {- ?2 `; F* @
n++;}
5 L0 L5 ~& A9 ?9 Bgetch(); 2 r$ W5 t9 a$ c+ a; _1 x
}
2 I4 x7 i" U2 x============================================================== ! U& G. h8 Z2 e1 @) y
【程序89
1 O7 e3 t  G0 M' g. k. s3 x: M题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
2 R6 l: ]* @, X& Y' a: i每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。 ( u) B, x) @6 ^% D: q( {
1.程序分析: 7 s  e# q6 T" \; g/ ]
2.程序源代码:
% @3 Z4 K  X4 n  _main()
/ g) q6 i& @9 s* C; R{int a,i,aa[4],t;
/ U0 i; t) N5 D4 g  oscanf("%d",&a);
: j& ^8 m6 [: Q2 Z9 k5 l. Jaa[0]=a ;
# h) N* S0 e+ h! g- j9 e3 taa[1]=a 0/10;
  ?- y8 }; s9 d; W! p; P% taa[2]=a 00/100;
% w6 {: k! J& v. ]+ n% Yaa[3]=a/1000; 8 `7 X0 p" Z# g7 O: l1 G$ b
for(i=0;i<=3;i++) 5 N2 ]# q$ q; C+ A# a1 K' w: \3 Q
{aa+=5;
$ k5 J/ j; K  Naa%=10; 5 n$ Q. N7 ]/ v
}   w8 l; G& o6 c3 N3 r* b
for(i=0;i<=3/2;i++)
+ N$ o5 g  B0 @/ d% r{t=aa; 8 p$ E$ ]+ R: m. u, w" ~
aa=aa[3-i]; : d. D9 g( \9 ?, B5 U( c
aa[3-i]=t;   T$ ~! P; ?, B" v6 G2 ~! y# e! p
} 6 \* i  D! g1 Q, I1 C
for(i=3;i>=0;i--)
1 R+ ]! y; @7 z' A4 v& Hprintf("%d",aa); 4 C) o5 L6 K* x- u/ T& Z8 S
}
- z. u: F8 {  G$ y( N% Z1 K============================================================== : t: D3 {9 K6 \" p
【程序904 K/ r) u# [  ?" p7 @' U$ w
题目:专升本一题,读结果。
3 Q3 _' v) R8 g" C% n+ d& [1.程序分析: . j! P+ J$ u% p) S) H
2.程序源代码:
  r$ A+ u+ v4 b- b#include "stdio.h"
! Y& F% y+ F2 L" @% N1 q#define M 5 ) A( J$ K7 [7 A% t. v8 a4 m! k* d: W
main()
6 o8 O' t( R% s( S6 s% v# R+ Y5 u{int a[M]={1,2,3,4,5};
* z5 ~; k3 Q$ Q2 Jint i,j,t; ( o  U) \5 s, Y7 Q* y. q
i=0;j=M-1;
0 N- N$ {0 L. l0 Y* Y6 f" fwhile(i {t=*(a+i); ) K: s0 `! C6 J6 v$ Q6 ^/ i
*(a+i)=*(a+j); + J5 F! [6 R) d. `
*(a+j)=t; + C1 y2 i% Z2 L) ?" s3 t
i++;j--;
% }4 T! l5 R& W" ~}
) q, C! ], _' j+ \! U& f: U/ Bfor(i=0;i printf("%d",*(a+i)); 0 \- C" N0 a2 f$ d) v) o: ?$ X
}
- Z9 v9 T4 x8 W; l+ [【程序91, {  [) a5 H* x0 E( S. @/ \
题目:时间函数举例1
2 `; C$ a7 S" b2 u& V, q1.程序分析:
2 B( M; O& s% L* x2 U2.程序源代码:
# W0 m* q4 I: ~2 e1 a#include "stdio.h" - u. d/ F! V7 ]/ L% C
#include "time.h" " V$ F) i* m0 a  @, {1 U! ^. l# l
void main() * o+ {% F  ?& [* G' b( d/ a* J
{ time_t lt; /*define a longint time varible*/ 0 G' I8 ?' c1 M  o) U' u
lt=time(NULL);/*system time and date*/ ) C2 e. N9 M& h* M% e6 \; M
printf(ctime(<)); /*english format output*/ : W% I; [0 \* X- |. V4 ~+ x0 K1 ]
printf(asctime(localtime(<)));/*tranfer to tm*/ 2 V; ]+ d$ n' L9 r1 h0 M( B% }
printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ + B5 W) h/ {* B6 s
} $ t. [  U4 X- ^1 w8 L- m  P
==============================================================
, D3 ~: D/ b  G& ^6 J【程序92
( b7 q( F3 d' i" w9 P3 H+ ^题目:时间函数举例2 ) C3 g7 S1 m3 g6 w/ |
1.程序分析:
& ], C; X' \9 i3 B& v8 ?1 Z2.程序源代码: 6 n8 h( ~* `3 {. d7 Z* g& L
/*calculate time*/ % b% k7 h$ q7 _7 H
#include "time.h"
& D3 t1 t/ g0 `" ?7 F& Z#include "stdio.h"
7 l  j; r" M* K/ imain()
6 ^; F  o, a* N; D/ K* T{ time_t start,end;
) p. }7 @" a9 W" {8 Fint i; - ^, l$ d+ A) m+ G: W9 O
start=time(NULL);
2 u, |# {/ K1 K/ F; M$ k7 K6 tfor(i=0;i<3000;i++) : r1 Q( x: ]6 g8 @% ~! t1 n
{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
: h0 j- n& ^' I, a  Pend=time(NULL); ' ]( T- A7 F& l4 Z2 }6 ?& I
printf("\1: The different is %6.3f\n",difftime(end,start));
6 W) g# W8 M) S1 X, G} , [+ L: b% C0 G' O9 ]
==============================================================
5 g: G" n  q) q4 P% ~% j6 V【程序936 R$ a1 m* S% j, g1 i
题目:时间函数举例3 : {' x7 k; G, [* G! z
1.程序分析: $ Z1 v: T3 K$ a2 l# K  [8 e
2.程序源代码: $ w1 x5 G0 _5 C* N. W& e- W" `
/*calculate time*/
! m' N; l2 m$ y% |4 M6 F#include "time.h"
2 J/ K+ m* V5 [; C+ t- y#include "stdio.h" / _; J7 _, ?7 x* b/ l4 @
main() % q1 h9 F* E. E/ J$ U) [9 @
{ clock_t start,end;
) Y. T/ n6 B# V# H/ S4 [2 p, Xint i;
, i, n! T  X1 v: m# x6 C  k. [double var; 5 R& D% E7 d0 o: {2 Y& R; \
start=clock();
" o' ]( v, |* ~. j0 P2 [- A, Wfor(i=0;i<10000;i++) : p+ j4 x* O* @7 A0 e7 I6 |
{ printf("\1\1\1\1\1\1\1\1\1\1\n");} 2 L" z  }* a1 ^' O6 X
end=clock(); . [" h9 M) k5 r, e- Y( f
printf("\1: The different is %6.3f\n",(double)(end-start));   [1 l  w1 Y" M( J) N' L
}
1 V6 G% e6 `/ o============================================================== - ?3 @' E6 N7 ]
【程序94# T, ?2 ~2 A  D& {/ y
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
$ y9 {& b0 X$ z9 Z8 p$ j1.程序分析:
8 }5 u+ A) L& e! W. Y- `2.程序源代码: 5 r0 F' r( |+ o! x) ]4 D
#include "time.h"
' r4 Q- K" o0 u) {5 U1 Q#include "stdlib.h" 5 m" h1 f# f6 [. L0 Z- o9 \" q; ]; ^
#include "stdio.h" : t" F6 _6 ^7 Z$ D8 d9 V  z" b9 ]5 A
main()
! G- @/ B: f+ m{char c; 1 p) l: F4 x: e" K
clock_t start,end; 9 g! H. r( c3 p" `. ]  m5 h7 Y$ F3 ^
time_t a,b;
9 [0 k, @4 R7 |6 {4 fdouble var;
4 T4 T! {, |: m7 Hint i,guess;
7 b6 p: d# N$ bsrand(time(NULL)); % Q- r! M* ?+ ?- q8 z) x
printf("do you want to play it.('y' or 'n') \n");
; _7 n' \$ Q+ t8 d9 n* oloop:
7 `  Q6 b4 J4 k9 Mwhile((c=getchar())=='y')
! V  B: X9 l7 ]3 _$ x8 O{ 1 \5 E2 m$ _& a5 Z6 u) o4 j
i=rand() 0;
1 o) @: p  X- ?  g% \! l9 s& vprintf("\nplease input number you guess:\n");
: p9 b; v' m' d) i5 V$ U1 ~% n! pstart=clock(); 9 b3 H5 V; x: t/ `
a=time(NULL); 5 u. |8 U4 P7 p' W( @' V; U) l
scanf("%d",&guess); 8 O$ F; @+ a; {- a: @: y: h
while(guess!=i)
* F. X; L7 Q5 C{if(guess>i) # @$ @0 E9 |2 K9 V2 g- x
{printf("please input a little smaller.\n");
2 t: H0 B3 U/ X* H+ y$ J; }scanf("%d",&guess);}
4 t' l4 _. _; c( j+ g2 gelse
- @3 K" k' R, c; s4 {; r{printf("please input a little bigger.\n"); * h. U. {! O( ^2 x' g& h% t' i
scanf("%d",&guess);}
, l9 r. C; I  r} * N" K* `9 O5 q: [! N
end=clock(); & F' |; K" C; k$ e6 t8 W
b=time(NULL);
; ?/ z8 P; a* B1 y( z  D5 Pprintf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
4 f; I0 F! Y5 r% a  R4 pprintf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
' f$ U' @8 }* ?0 Qif(var<15)
; ~9 e& o& L; `2 |7 h! eprintf("\1\1 You are very clever! \1\1\n\n"); 0 c! a2 Y. Z3 `9 z
else if(var<25) & o  V$ v5 u5 J; g/ H8 o( y
printf("\1\1 you are normal! \1\1\n\n"); 9 }! ^2 N' ~) G( E5 [2 U
else
" J  Y7 m  u8 g& w; R1 M  d) Qprintf("\1\1 you are stupid! \1\1\n\n");
. n2 G4 w1 p2 L: U, M: G. W1 U; x( I+ Hprintf("\1\1 Congradulations \1\1\n\n");
+ z3 M& N2 z/ t% |" m+ yprintf("The number you guess is %d",i);
/ c* X+ M( B* @. p% k# W1 B}
; `/ L* Y' }' a1 t# [printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
% C2 ~3 Y. Z4 E  Iif((c=getch())=='y')
4 O2 f' G5 {! }- Y9 R& @/ qgoto loop; 9 Y3 @9 q' {" w) r
}
" s7 T, u1 u& i" L5 ^  t
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 09:03 , Processed in 0.076845 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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