|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
(1)阅读下列程序,写出其运行结果,并指出其功能。
<1>
#include <iostream.h>
int hcf( int u , int v )
{
int a,b,t,r;
if (u > v) {
t = u ;
u = v ;
v = t ;
}
a = u ;
b = v ;
while ( ( r = b % a ) != 0 ){
b = a ;
a = r ;
}
return( a ) ;
}
int lcd( int u , int v , int h )
{
return( u * v / h ) ;
}
void main()
{
int u , v , h , l ;
cin >> u >> v ;
h = hcf ( u , v ) ;
cout << “hcf=” << h << endl ;
l = lcd ( u , v, h ) ;
cout << “lcd=” << l << endl ;
}
输入:48,32
<2>
#include <iostream.h>
long fib ( int g )
{
switch ( g )
{
case 0 : return 0;
case 1 :
case 2 : return 1;
}
return ( fib( g – 1 ) + fib( g – 2 ) ) ;
}
void main ( )
{
long k;
k = fib ( 7 );
cout << “k=” << k << endl ;
}
<3>
#include <iostream.h>
int k = 1 ;
void main()
{
int i = 4 ;
int fun (int);
fun( i ) ;
cout << “(1) “ << i << ‘,’ << k << endl ;
}
fun( int m )
{
m += k;
k += m;
{
char k = ’B’;
cout << “(2)” << char(k-‘A’) << endl ;
}
cout << “(3)” << m << ‘,’ << k << endl ;
}
<4>
# include <iostream.h>
# include <iomanip.h>
void subp ( )
{
static int x = 0 , y = 0 ; /*问题(1)*/
int a, b;
a = x – y ;
b = x + y ;
cout << setw(5) << a << setw(5) << b << endl ;
cout << setw(5) << x << setw(5) << y << endl ;
}
int x , y ;
void main()
{
int a = 9,b = 3;
x = a – b ;
y = a + b ;
subp();
cout << setw(5) << a << setw(5) << b << endl ;
cout << setw(5) << x << setw(5) << y << endl ;
}
1)运行结果是:
2)如果问题(1)处语句改为static int x,y ; ,运行结果会发生改变么?为什么? |
|