机械社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 1366|回复: 1

C语言算法经典87-94

[复制链接]
发表于 2017-10-12 10:14:07 | 显示全部楼层 |阅读模式
【程序876 D0 B# A6 u3 _( `9 L: q  G5 n
题目:回答结果(结构体变量传递) : j! S# b# z3 o3 R  r  U) H
1.程序分析:
5 j& n* r2 d6 z& j+ Q; Z8 K% B' j2.程序源代码:
8 P  I2 i, ^& M/ }$ \3 l#include "stdio.h" ( O/ ^- a4 @* B
struct student 1 z  V- J* T! A. \0 J
{ int x; " [. H% S+ Z, w. _& j
char c;
' G: ^9 W. W* S! B: z5 k, a; c9 S} a;
1 P* A. l$ d6 V; N( l* h5 ?) dmain() 9 V. N' s/ m, q' G  G
{a.x=3;
6 g7 \4 f& E! Ta.c='a';
1 W* y# \" y: R+ ]* ]1 O; df(a);
# w; U% {5 y! U8 O1 \printf("%d,%c",a.x,a.c);
2 z+ ~; L7 f/ n* P* o1 A0 n. b} " L+ I9 r. N# r/ f/ Z9 P
f(struct student b)
8 \  ]7 H: A8 p6 @2 }{
3 ]! S& E/ r' H+ f6 Q4 `b.x=20;
0 A2 i" t2 V! F+ F1 yb.c='y'; ! c% @/ ^* ?' @1 t- t4 K6 s$ E! a/ M
}
/ F$ f& g# ^' B! q4 X! t0 R- {============================================================== ) _9 w, f6 F0 e. k$ V6 Z
【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要 % J# I7 e( @- _4 g, u$ O5 ]
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
+ Q8 W: V8 B5 [( f1.程序分析:
- F5 h* n0 Z! w/ |2.程序源代码:
' O  q. I# k& ?) M& S. R9 _main()   o: a2 G. ]! H% M, g3 v% a5 n8 Q1 x
{int i,a,n=1; ! v) B8 n- g0 c# }7 Y  ^
while(n<=7)
! v5 i5 }) p, k& ^7 s( O* Z4 V1 a3 J{ do {
; H% U! M" B9 e* Q/ nscanf("%d",&a);
' c# L. E- R: B1 z' Z, [+ a9 \}while(a<1||a>50); 7 n3 a' W% e0 O2 y
for(i=1;i<=a;i++)
* ]1 R2 q2 j5 M# Aprintf("*"); 1 f. Y3 y* J1 o, b! r+ ]5 \0 w. Z
printf("\n"); + M/ g# n) G/ U" V9 A- s) `
n++;}
3 d5 a( y: J/ P* o* h7 w, [getch(); . ]" P% f/ }+ |" L5 }' q
}
0 h9 o8 X1 f& U: z- r; \: m8 x==============================================================
0 P  X" R1 ]) G  ^【程序89
6 K4 V- I% q. t) I( p题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: ) @  H* G0 E- c. l0 U* z$ l
每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
2 Z3 s# v# o/ [1.程序分析:
+ a0 Z, D3 j! H$ c$ f8 G1 }2.程序源代码: ! F1 f2 ?) T+ e+ z5 ^
main() 2 w# n" Z6 r8 r+ Z
{int a,i,aa[4],t;
% ~2 x9 {( y# A' _' r" }0 iscanf("%d",&a); , _' p6 N3 f0 q) G  e( ^) S6 h5 j
aa[0]=a ; ( y0 ^  ]( l6 _& Z& p3 `2 H9 u
aa[1]=a 0/10;
) Q: c& k+ f" @7 t5 ?aa[2]=a 00/100;
+ [6 s, g) K5 z4 ]. yaa[3]=a/1000;
' O* [% ^$ A. a1 w+ R: ~( X  zfor(i=0;i<=3;i++)
8 k% ?6 |) s; V& o{aa+=5;
' B# R% P# o$ ]* _& r+ }aa%=10;
- M6 w- ~3 u2 ~" {  x- ?4 w} ; T% o+ X, C5 E8 h& p. C; M6 {
for(i=0;i<=3/2;i++) 2 i5 }8 R; w1 s$ i- d- X
{t=aa; # I! U* ^# T: z7 F# i/ K
aa=aa[3-i]; 3 F+ @* v$ @0 V0 T) \: n5 c
aa[3-i]=t;
8 J$ p2 z0 t, T% Z/ Y8 @} $ g- |3 R+ w- S  M1 H% H6 Q9 Z8 U
for(i=3;i>=0;i--) 3 s# @9 D1 a5 s* ]& D) _, W2 U1 z
printf("%d",aa); & G: k. \3 p1 L! R
} / s# N( M5 t% R$ S  Y$ h
==============================================================
4 p/ f; j" K4 S3 u% S【程序90
" R% R: P( G) H, O/ P: U题目:专升本一题,读结果。 0 G( O5 B& |3 D
1.程序分析:
" R2 {3 n) y+ D% }4 E+ u7 ?2.程序源代码: 7 x- T+ `" f% R6 O, d* z
#include "stdio.h" ' B; u* Z9 C- S7 ~* \& ^2 B
#define M 5
( h/ f0 i  A: R3 g- f- O  ?& }2 {main()
3 O6 |" x; K  d. E5 Z* u{int a[M]={1,2,3,4,5}; ) @' D4 l9 W' k' g+ W( g- r. q
int i,j,t;
  Q. W) B  r+ p5 }" z: _i=0;j=M-1; " }% \! T* L4 m+ Q8 T
while(i {t=*(a+i);   i  {3 }7 N$ T/ c8 {3 W: c
*(a+i)=*(a+j);
  M" k. X% {2 Y*(a+j)=t; 8 u1 ^; f8 O6 e1 L6 b  u7 ?$ I  N
i++;j--; 6 I( ?. a" I+ f. R( f+ X
} 1 q3 A( {2 s& a3 N  n
for(i=0;i printf("%d",*(a+i)); ! S  A" m3 z" w2 O$ L2 p8 C
} 0 x  A/ ~9 n7 n$ G( I) c3 r
【程序914 g3 q) F/ L- L  @& V' h$ l- n7 u
题目:时间函数举例1 ' L7 K! E5 p5 _0 R
1.程序分析: 3 n6 O0 l2 k9 j
2.程序源代码:
8 ^+ P. t2 o" w7 K# v#include "stdio.h" 0 K* I/ y& ^, F) }' }1 c8 J% b) H
#include "time.h"
$ E3 }3 ?  ?1 @4 ]  ~6 dvoid main() % a# f  R, m3 _7 v! m1 B
{ time_t lt; /*define a longint time varible*/ 3 s' Z+ v8 @# M1 M% v
lt=time(NULL);/*system time and date*/ / w3 p6 j# l/ a! E: t: J* F3 y' f
printf(ctime(<)); /*english format output*/
$ ?, j* O% F% }. R9 sprintf(asctime(localtime(<)));/*tranfer to tm*/
5 e* K* I  L! `- gprintf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
1 u9 c8 N4 Q+ z0 N. I}
8 A% @4 t! p& ~0 F# Y============================================================== $ C: V6 I- G- X% o# S
【程序92- g  {) h+ ]& J3 n
题目:时间函数举例2 $ M7 P3 L' F1 p" D5 T0 l
1.程序分析:
3 [) H8 l- z- M' R( r2.程序源代码: ' D8 u+ r$ k/ s/ _6 _) G$ ?+ B
/*calculate time*/
& ^) z& _! z, u* ]#include "time.h"
( ^, h8 G! o0 F  y! @#include "stdio.h"
: l9 U. p' x8 {. |- Nmain()
/ G2 t4 P% @, g{ time_t start,end; & Z) v; s& S( F" P! m* I
int i;
1 |$ n* I- W5 `$ q8 H# S+ Q& Rstart=time(NULL);
1 p4 x0 [( N  l" C1 ?for(i=0;i<3000;i++)
7 P' s( D7 N5 c: M{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
3 O* k9 \7 B5 \" S+ V( xend=time(NULL);
% I6 v: e+ G6 C. j: b9 [. `. H" A1 m: oprintf("\1: The different is %6.3f\n",difftime(end,start)); 2 K# W, K- }, R- n! `/ A+ X
}
& b) h/ z  s: W9 {& V1 y============================================================== 6 c! n2 m# [' \# M
【程序930 W* T# A: @. p
题目:时间函数举例3
  p- R1 }! L0 R6 |- c6 ?1.程序分析:
5 i( y# w( [2 ]' _2 L2.程序源代码:
1 R) B1 L! m" \! D" n/*calculate time*/ 5 @7 h  f0 G; Z" O! u0 C
#include "time.h" 5 U6 t& p* C0 F( F/ h# m- A
#include "stdio.h"
7 e$ Y, {8 q: X- O6 W: F) [0 Vmain() 8 ^& _* `3 A0 m' h  o  s
{ clock_t start,end;
+ A6 @( y# ~8 z+ ~( gint i;
+ T2 g. t( d" v: D$ g" Ndouble var;
) ~1 U$ P1 _3 B) F; E" N  `, Lstart=clock(); 2 R+ t& Z# V- [% V& M0 T
for(i=0;i<10000;i++) * |& B: ~' ]2 m* f6 y  Z3 M8 q6 b; Q
{ printf("\1\1\1\1\1\1\1\1\1\1\n");} * B9 D( \2 g& ^, X( j! K
end=clock(); 4 u/ @+ W- I2 x. ~
printf("\1: The different is %6.3f\n",(double)(end-start));
0 D( |: J" |0 B( W# L( l}
1 M/ t# S: Y$ d" _7 m============================================================== 9 c: @$ p* o2 ~7 X( `5 y
【程序94
: B" {7 i$ z0 V. o4 ~6 f, N3 q题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的) 3 K* _4 a- ?2 T3 @. [2 V
1.程序分析: 0 I$ Z7 P3 h3 }7 U- c
2.程序源代码: * x1 D2 ~0 `8 l* i6 A; m+ W# A* d
#include "time.h"
) `. E; E. u' X' l#include "stdlib.h" ! W# b6 G; P; N9 c. I
#include "stdio.h" 0 i2 a' x  \% {1 j& I4 W! _
main() 0 t7 Z( P7 M* _; U5 o
{char c;
* ~4 _- p4 U6 s9 b% T. b5 c4 Z! eclock_t start,end;
! C3 T; B- R7 h0 A1 _2 z9 \time_t a,b; 2 k( Y* u8 y( N/ _6 Q7 g
double var; 9 J1 L1 Q: Z' r3 T9 C  Y: f! Z, @
int i,guess; ( \* R1 Q! Z) G- P, d3 Y
srand(time(NULL));
" x" [; v/ B/ Z1 Q# tprintf("do you want to play it.('y' or 'n') \n"); 7 p& F+ w; E7 G
loop: " G" p) R0 f; @  y7 o
while((c=getchar())=='y') ( p# N4 y3 Y, q
{
  Z8 E. d) ?9 V. F9 r/ ^# Fi=rand() 0;
( u) \9 [% i! |* t6 t$ r9 kprintf("\nplease input number you guess:\n");
+ W" D5 J: D: u5 x# \" u6 o7 d2 `start=clock(); + S$ _/ Q& l  A2 O6 p
a=time(NULL);
5 ~- e( L. r* w# o7 g% Rscanf("%d",&guess);
4 m. t8 a2 i! n) }1 kwhile(guess!=i) ' M7 k$ T+ A  J2 L
{if(guess>i)
, i- v) U- ~2 ~4 s* F{printf("please input a little smaller.\n");
; F" i8 U! n7 F2 e  T* h! Q1 Gscanf("%d",&guess);}
$ B2 E! T! w7 Z/ M6 Eelse
9 {9 K% \$ S! q6 E  ~{printf("please input a little bigger.\n");
  l9 R: U6 c, nscanf("%d",&guess);}
* m& R/ m9 \4 G2 P, p" @} & q8 |' t8 @2 a$ w
end=clock();
! s. S9 M3 b1 E; tb=time(NULL); 6 ~3 [; a. u. V
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2); 8 {/ n; f. C) B$ q
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
1 S+ V: u, s% u1 g) X5 {) b9 Pif(var<15) * }; ?8 U7 p7 R* Z/ _$ b* L( U
printf("\1\1 You are very clever! \1\1\n\n"); ) z; H8 M0 D& x9 h' \4 m. z
else if(var<25)
: \4 J/ j% }  X' \1 _printf("\1\1 you are normal! \1\1\n\n");
! I' j6 a+ @+ v4 X5 relse
9 s6 K2 o( D, C4 F  H. R0 v, Lprintf("\1\1 you are stupid! \1\1\n\n");
1 p$ f; z: G9 x) Vprintf("\1\1 Congradulations \1\1\n\n"); 1 W/ Z6 I3 w$ \3 l! n
printf("The number you guess is %d",i);   J/ H" r+ z8 J
}
; a( U! a5 S0 q" l2 U0 Z- Dprintf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
0 T+ z$ y2 w: N! V- cif((c=getch())=='y')
7 X+ ?8 R& k4 c: I1 k0 A0 Ngoto loop;
9 Z- n8 I1 |; g, G6 `( [, l}

2 i# V* v5 l& o7 k9 d
回复

使用道具 举报

发表于 2017-10-12 10:29:05 | 显示全部楼层
我一路跟着学习。谢谢楼主!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

GMT+8, 2024-4-26 11:47 , Processed in 0.053603 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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