Professional Documents
Culture Documents
1)
2)""
3)
4)
1
5.1
C
3
k! f=1;
n!+m! for(i=1; i<=n; i++)
f=f*i;
4
# include <stdio.h>
# include <stdio.h> void main()
void main() {
{ int k, m, n;
int k, m, n; float fk, fm, fn;
float fk, fm, fn; float fact(int n);
6
,
""
8
xy
v
v = volume(x, y);
9
5.2
""
#include <stdio.h> //
#include <math.h> //
10
stdio.h
math.hstdlib.hfloat.h
time.h
ctype.h
string.h
malloc.hstdlib.h
11
5.2100100
#include <stdio.h> /* */
#include <time.h> /* */
#include <stdlib.h> /* */
int main()
{ int k; long now;
srand(time(&now));//
/* */
for(k = 0; k < 10; k++) /*10100 */
printf("%d\n", rand()%100); /* */
return 0;
}
/*clock():
void srand(unsigned seed); */
12
rand()0~32767
time()19701100:00:00
srand()rand()
rand()
srand()rand()
13
C
printfscanf
strlen strcat pow(x,y) volume
fabs(s)
14
"" "void"
15
double volume(double radius, double height)
{ double PI = 3.1415926, vol;
vol = PI * radius * radius * height;
return vol; //
}
void main()
{ .
return; //
}
16
void main(){.}
()
()
pow(x,y);
17
C main
A B C
D E F G H I J
K L
main( )
main( ),main( )
main( )
main( )
main( )
main( )
18
main(),
""
19
5.3
:
{
}
20
int max (int x, int y)
{ int z;
z = x > y ? x : y;
return(z);
}
main()
C
voidreturn
22
void main()
{
short i,j,x;
for(i=1;i<257;i++)
{
for(j=0;j<3;j++)
if(j==0)TransBit(2,i);
else if(j==1)TransBit(8,i);
else TransBit(16,i);
if(i%20==0)system("pause");//20
}
system("pause");
}
23
5.4gcd()
ab
gcd(a-b, b), a > b
gcd(a, b) = gcd(a, b-a), a < b
a , a = b
printnl()
26
(2) 1
return
return
int max(int a,int b)
{
return a>b?a:b;
}
return
return
.
int max(float a,float b)
void main( )
{
{
return a>b?a:b;
printf("%d\n",max(12.3,20.6));
}
} 20
27
int
charintchar
max(int a, int b)
{
return a>b?a:b;
}
(2)
return
28
"void"
void PRINT(void)
{
printf("This is a example\n")
}
()
{
}
29
5.4
1.:
()
()
30
2.
(1)
printf("Hello,world!\n");
scanf ("%d",&a);
(2)
c=max(a,b);
(3)
minvalue=min(a, min(c, d));
//
printf("%f\n",min(u-v,a+b));
/**/
31
3.
:
c=fun(a,b); main
fun(int x, int y) fun void main()
{ int z; { int a,b,c;
z=x>y?x:y; scanf("%d,%d",&a,&b);
return(z);
} c=fun(a,b);
printf("Max is %d",c);
}
32
mainfun
fun fun
main
fun
fun fun
main
main
main return
33
()
"return"(
)
(static)
34
4.
35
x y x y
5 8 15 18
5.8
x y x y
#include <stdio.h> 5 8 5 8
void func(int x, int y)
{ x += 10; y += 10;
printf(" funcx = %d, y = %d\n", x,y);
}
int main()
{ int x = 5, y =8;
printf(" xyx = %d, y = %d\n",x,y);
func(x, y);
printf("funcx=%d,y=%d\n",x,y);
return 0;
}
36
,xy,x=5,y=8
fun,x=15,y=18
fun,x=5,y=8
,
func()
37
:
int main()
{ int x[2] = {5, 8};
printf(" x1x2x0= %d, x1= %d\n",
x[0],x[1]);
func(x[0], x[1]);//
printf("funcx0=%d,x1=%d\n",
x[0],x[2]);
return 0;
}
,
,
38
1.""
2.
39
void swap( int x[]) a
{ x
int j; swap(a)a[2]
j=x[0]; axxa
x[0]=x[1];
x[1]=j; xa
}
40
01
1 1 1 1 1 1 1 1 1 1
0 0 0 1 0 0 0 1 0 1
1 1 0 1 0 0 0 1 0 1
1 0 0 0 0 1 1 0 0 1
1 0 1 1 1 0 0 0 0 1
1 0 0 0 1 0 0 0 0 0
1 0 1 0 0 0 1 0 0 1
1 0 1 1 1 0 1 1 0 1
1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
(x,y+1),(x+1,y),(x,y-1),(x-1,y)
41
2
1) maze[N1][N2]
01
2) stack[N1*N2][2]
1
2
xy
42
#include <stdio.h>
#define N 10
int getpath(int maze[N][N]) /**/
{ int stack[N*N][2];
int i,x=1,y=0,ok,top=0;/*xystack*/
stack[top][0]=x;
stack[top][1]=y;/*toptop1top1*/
while(1)
{ ok=0; /*ok*/
if (maze[x][y+1]==0) {y=y+1;ok=1;} /**/
else if (maze[x+1][y]==0) {x=x+1;ok=1;} /**/
else if (maze[x][y-1]==0) {y=y-1;ok=1;} /**/
else if (maze[x-1][y]==0) {x=x-1;ok=1;} /**/
if(!ok) /*4top1*/
{ top--;
if(top==0) /*top10*/
{ printf("!\n");return 0;}/*2*/
x=stack[top][0]; /*Xx*/
y=stack[top][1]; /*Yy*/
}
else /*2stack*/
{ maze[x][y]=2; /*2*/
top++;/*2*/
stack[top][0]=x;/*Xstack*/
stack[top][1]=y;/*Ystack*/
if (x==N-2 && y==N-1)/**/
{ printf(":\n");
for (i=0;i<top;i++){ printf("(%d,%d)-->",stack[i][0],stack[i][1]);
if ( (i+1)%5==0) printf("\n");}
printf("(%d,%d)\n",stack[top][0],stack[top][1]); return 1;
}
}
}
}
43
printmaze(int maze[N][N]) /**/
{ int i,j;
printf(":\n");
for(i=0;i<N;i++){
for(j=0;j<N;j++)
printf("%2d",maze[i][j]);
printf("\n");
}
}
main( ){ int A[N][N]={ 1,1,1,1,1,1,1,1,1,1,
0,0,0,1,0,0,0,1,0,1,
1,1,0,1,0,0,0,1,0,1,
1,0,0,0,0,1,1,0,0,1,
1,0,1,1,1,0,0,0,0,1,
1,0,0,0,1,0,0,0,0,0,
1,0,1,0,0,0,1,0,0,1,
1,0,1,1,1,0,1,1,0,1,
1,1,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1 };
printmaze(A);
getpath(A);
} 44
1 1 1 1 1 1 1 1 1 1
0 2 2 1 2 2 2 1 0 1
1 1 2 1 2 2 2 1 0 1
1 2 2 2 2 1 1 0 0 1
1 2 1 1 1 0 0 0 0 1
1 2 2 2 1 0 0 0 0 0
1 0 1 2 2 2 1 0 0 1
1 0 1 1 1 2 1 1 0 1
1 0 0 0 0 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1
mazestack
12(top=12)x=1,y=4,
(top--)top=4,x=3,y=2x=3,y=1
0,
top=0
45
stack[N1*N2][2]
top=12top=4
x,y
46
0x1stack[0][0]=1; 0
y0stack[0][1]=01xy
stack[1][0]stack[1][1]nxy
stack[n][0]stack[n][1]1,0
1 1 1 1 1 1 1 1 1 1
0 0 0 1 0 0 0 1 0 1
1 1 0 1 0 0 0 1 0 1
1 0 0 0 0 1 1 0 0 1
1 0 1 1 1 0 0 0 0 1
1 0 0 0 1 0 0 0 0 0
1 0 1 0 0 0 1 0 0 1
1 0 1 1 1 0 1 1 0 1
1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
47
""""""
""
:
1.
2.C
48
C
goto
49
5.
50
main a b
a b
:
51
5.10
#include <stdio.h>
#include <math.h>
int istri(float a, float b, float c) /* */
{ if ( a+b<=c || a+c<=b || b+c<=a ) return 0;/* */
if ( a <= 0 || b <= 0 || c <= 0 ) return 0;/* */
return 1; /* */
}
52
int main() /* */
{ float a, b, c, area;
do
{ printf("a, b, c");
scanf("%f%f%f", &a, &b, &c);
area = triangle(a, b, c);/* triangle() */
if(area == 0)printf("\n");
} while(area == 0); /* */
printf("%f\n", area);
return 0;
}
a, b, c1,2,3
a, b, c3,4,5
6.000000
53
(
)
#include<math.h>
54
5.5
55
1
2
3
56
(1) (1 12 2 )
void
57
#include <stdio.h>
#include <stdio.h>
void main( )
void main( )
{
{
float fun(float,float);
float fun(float x,float y);
printf("Sum=%f\n",fun(2,5));
printf("Sum=%f\n",fun(2,5));
}
}
float fun(float x,float y)
float fun(float x,float y)
{
{
return x+y;
return x+y;
}
}
Sum=7.000000 Sum=7.000000
58
#include <stdio.h>
void main( )
{
float fun( ) //
printf("Sum=%f\n",fun(2,5));
}
float fun(float xfloat y)
{
return x+y
}
59
#include <stdio.h>
float fun(float x,float y)
{
return x+y;
}
void main( )
{
printf("Sum=%f\n",fun(2,5));
}
60
61
5.6
C
,
a
b a b
b a
a a b
a b a
a b
62
5.12
n!
123n
n! n = 4
4!43! 3!32! 1!=1
n!=n*(n-1)!,(n-1)!=(n-1)*(n-2)!,
1 n=0, 1
n!=
n*(n-1)! n>1
""
63
#include <stdio.h> int fac(int n)
int fac(int n) { int s; int i;
for (s = 1, i = 1; i <= n; i++)
{
s *= i;
if(n<=1)return 1; return s;
return n*fac(n-1); }
} fac( )
1
void main( )
{
int n;
scanf("%d",&n);
printf("%d!=%d\n",n,fac(n));
}
64
n3main( )fac(3)
1fac( )
1fac(n-1)*n
(n1)fac( )n
12fac( )
1fac( )12
fac(2) 2.
65
n!
""
n!(n-1)!(n-1)!
(n-2)!1!
""
""
1!2!3!n
1!=1
66
1 n*fac(n-1)
2()if
if
67
type fun()
{
if()
p = /**/
else
p = fun() /**/
return p
}
68
2
fun()
{
if()
return //
else
return fun()//
}
int fac(int n )
{ if( n == 1 ) /**/
return 1; /**/
else
return( n * fac(n-1)); /**/
}
69
Fib(n)
n, n 0,1
Fib(n)
Fib(n 1) Fib(n 2), n 1
70
71
5.13Hanoi
Tower of Hanoi
3
64
A B C A B C
ABCAn
72
64 264 1 = 1.81019
ACB
nn>1AC
A B C A B C
1
2A,BC
3
73
(Tower of Hanoi)
1~n-1
n
A B C
(1)A1~n-1
(1) 1~n-1
BC() n
A B C
:13 ,.
2n-1
74
(Tower of Hanoi)
3
A B C
75
A B C
76
A B C
77
A B C
78
A B C
79
A B C
80
A B C
81
~
A B C
82
#include <stdio.h>
void move(int m,char from,char to)
{ printf("%d%c --->%c\n",m,from,to);
//ac
} A, C, B
void hanoi(int n,char A,char C,char B)// 3:A,C,B
2:A,B,C
{ if( n==1 ) move(n,A,C); 1:A,C,B
else 1 A--->C
{ hanoi(n-1,A,B,C); //n-1AB,C 2 A--->B
1:C,B,A
move(n,A,C); //AC 1 C--->B
hanoi(n-1,B,C,A ); //n-1BC,A 3 A--->C
} 2:B,C,A
1:B,A,C
} 1 B--->A
2 B--->C
void main() 1:A,C,B
1 A--->C
{
hanoi(3,'A','C', 'B');
} 83
84
5.7
5.14
nn10n0
10n
c0c1n10n
100
#include <stdio.h>
int digits(int n) int main()//
{ int c=0; { int n;
do { printf("");
c++; n/=10; scanf("%d", &n);
} while(n); printf("%d%d.\n",
return c; n, digits(n));
} return 0;
}
85
5.15
""
23235312321
n
n
n
86
#include <stdio.h> //1 void main()
int circle(int n) { int c=123321;
{ int t[12],k = 0, j; if(circle(c))
do { t[k++] = n%10; printf("%d ", c);
// ,t else printf("%d ", c);
n /= 10; /* */ }
} while(n);
for(j = 0, k--; j < k; j++, k--)
if(t[j] != t[k]) return 0;/* */
return 1; /* */
}
//2p67_23
int circle(int n)
{ int s=0,m=n;
while(m)
{ s=s*10+m%10; //,
m/=10; //
} return s==n;
} 87
5.16
6
6 = 3 + 38 = 3 + 510 = 3 + 7
6
n
n=x+yxx3
2yy
y
xy
88
#include <stdio.h>
int isPrime(int n) //n
{ int k;
if (n == 1) return 0; /* 1 */
if (n == 2) return 1; /* 2 */
if (n % 2 == 0) return 0;// 2
for(k = 3; k*k <= n; k += 2)
if (n % k == 0) return 0; //nkn
return 1; // knn
}
89
int main()
{ int n, x, y, m, count=0;
printf("6");
scanf("%d", &m);
for( n = 6; n <= m; n += 2)
for( x = 3; x <= n/2; x+=2)
if(isPrime(x) && isPrime(y = n - x))
{
if(count++ % 4 == 0) printf("\n");
printf("%d = %d + %d\t", n, x, y);
break; /* */
}
printf("\n");
return 0;
} 90
5.17xy
xy
xy-1x01
x0xxy
#include <stdio.h>
int power(int x, int y) /* x y */
{ if(y == 0) return 1; /* 001 */
return x*power(x, y-1);
}
int main() /* */
{ int x, y;
printf(" x(!=0) y ");
scanf("%d%d, &x, &y);
printf("%d^%d = %d\n", x, y, power(x, y));
return 0;
} 91
5.18
back()
#include <stdio.h>
void back(int n) /* */
{ printf(" %d", n%10); /* */
if(n < 10) return;
back(n/10);
}
int main() /* */
{ int x;
do {printf(""); scanf("%d", &x);
} while( x <= 0);
printf(""); back(x); printf("\n");
return 0;
}
92
p125_4,6,78()
p124_1,2,5
93