完成99页练习5.3~5.5。
要求:5.4,用注释写出运行结果,并在相应的语句处解释得到运行结果的原因。
5.3
#include <iostream>
#include <iomanip>
using namespace std;
void cfb1()
{
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= 9; j++)
cout << left << setw(2) << i * j << " ";
cout << endl;
}
}
void cfb2()
{
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
cout << left << setw(2) << i * j << " ";
cout << endl;
}
}
void cfb3()
{
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = i; j <= 9; j++)
cout << left << setw(2) << i * j << " ";
cout << endl;
}
}
int main()
{
cfb1();
cout << endl << endl;
cfb2();
cout << endl << endl;
cfb3();
cout << endl << endl;
return 0;
}
5.4
输出结果:
Main--x = 5, y = 1, n = 1
Fcun--x=6,y=21,n=11
Main--x=5,y=1,n=11
Func--x=8,y=31,n=21
n是全局变量,作用域是整个工程,所以在func函数中的操作影响n的值。
main函数中的x和y都是局部变量,所以在main函数的cout语句中,输出的都是main函数中的变量的值,跟func函数没关系。
func函数中,x是静态变量,static int x = 4只执行依次,第二次调用Func函数的时候,继承第一次调用后x的值,也就是说,第一次调用Func函数后,x的值变成6,所以,在第二次调用Func函数时,static int x=4这一句不再执行,x继承第一次Func函数后的值,也就是x=6,执行x+=2后,x的值变成8,这个x的作用域仅在Func函数中,对Main函数中的x没有影响。
代码注释:
void func();
int n = 1; //n是全局变量
int main()
{
static int x = 5; //静态局部变量
int y = n; //y = n = 1;
cout << "Main--x=" << x << ",y=" << y << ",n=" << endl;
//显示 Main--x=5,y=1,n=1
func();
//func函数执行过程:
{
static int x = 4; //静态变量,初始化只执行1次
int y = 10; //局部变量
x += 2; //x = 4+2 = 6
n += 10; //n=1+10=11 n是全局变量
y += n; //y=10+11=21 y是局部变量
cout << "Func--x=" << x << ",y=" << y << ",n=" << n << endl;
//显示 Func--x=6,y=21,n=11
}
cout << "Main--x=" << x << ",y=" << y << ',n=' << n << endl;
//显示 Main--x=5,y=1,n=11 这里的x是main函数中的x,y是main函数中的y,n是全局变量
func();
func函数执行过程:
{
static int x = 4; //静态变量,这一句不在执行,x=6(继承上一层func函数中的x的值)
int y = 10; //局部变量
x += 2; //x = 6+2 = 8
n += 10; //n=11+10=21 n是全局变量
y += n; //y=10+21=31 y是局部变量
cout << "Func--x=" << x << ",y=" << y << ",n=" << n << endl;
//显示 Func--x=8,y=31,n=21
}
}
5.5
#include <iostream>
#include <iomanip>
using namespace std;
int a0, a1;
int fbn(int n)
{
if (n == 1)
return a0;
else if (n == 2)
return a1;
else
return fbn(n - 1) + fbn(n - 2);
}
int main()
{
int n;
cout << "请输入第1项和第2项的值:";
cin >> a0 >> a1;
cout << "请输入n:";
cin >> n;
cout << "第"<<n<<"项=" << fbn(n) << endl;
return 0;
}
3题都给你写出来了,望采纳!
5.3
(1)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(4)<<"*";
for(int i=1;i++;i<=9)
cout<<i<<endl;
/* for(int k=1;k++;i<=9)
{
cout<<k;
for(int j=1;j<=9;j++)
cout<<i*j;
cout<<endl;
}*/
}
(2)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(4)<<"*";
for(int i=1;i<=9;i++)
cout<<setw(4)<<i;
cout<<"\n...........................................\n";
for(int k=1;k<=9;k++)
{
cout<<setw(4)<<k;
for(int j=1;/*j<=9*/j<=k;j++)//将j<=9更改成j<=k即可
cout<<setw(4)<<k*j;
cout<<endl;
}
cout<<endl;
}
(3)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(4)<<"*";
for(int i=1;i<=9;i++)
cout<<setw(4)<<i;
cout<<"\n...........................................\n";
for(int k=1;k<=9;k++)
{
cout<<setw(4)<<k;
if(k!=1)
cout<<setw(4*k-4)<<" ";
for(int j=k;j<=9;j++)//此时,J从k开始循环
cout<<setw(4)<<k*j;
cout<<endl;
}
cout<<endl;
}
5.4
Main--x = 5, y = 1, n = 1
Fcun--x=6,y=21,n=11
Main--x=5,y=1,n=11
Func--x=8,y=31,n=21
5.5
#include<stdio.h>
#include<iostream>
//Fibonacci数列非递归 C++ 实现
using namespace std;
int fib(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
int f1=1;
int f2=0;
int fn=0;
for(int i=2;i<=n;i++)
{
fn=f2+f1;
f2=f1;
f1=fn;
}
return fn;
}
int main()
{
int n;
cout<<"请输入一个整数:"; //表示要求的是数列中的第几个数。
cout<<endl;
cin>>n;
cout<<fib(n)<<endl;
return 0;
}
5.3
#include<iostream>
#include<cmath>
using namespace std;
void cf1()
{
cout << "*\t1\t2\t3\t4\t5\t6\t7\t8\t9" << endl;
cout << "-----------------------------------------------------------------------------------" << endl;
for (int i = 1; i <= 9; i++)
{
cout << i;
for (int j = 1; j <= 9; j++)
cout << "\t" << i*j;
cout << endl;
}
}
void cf2()
{
cout << "*\t1\t2\t3\t4\t5\t6\t7\t8\t9" << endl;
cout << "-----------------------------------------------------------------------------------" << endl;
for (int i = 1; i <= 9; i++)
{
cout << i;
for (int j = 1; j <= i; j++)
cout << "\t" << i*j;
cout << endl;
}
}
void cf3()
{
cout << "*\t1\t2\t3\t4\t5\t6\t7\t8\t9" << endl;
cout << "-----------------------------------------------------------------------------------" << endl;
for (int i = 1; i <= 9; i++)
{
cout << i;
for (int j = 1; j < i; j++)
cout << "\t";
for (int j = i; j <= 9; j++)
cout << "\t" << i*j;
cout << endl;
}
}
int main()
{
cf1();
cout << endl;
cf2();
cout << endl;
cf3();
return 0;
}
5.4
输出
Main--x = 5, y = 1, n = 1
Fcun--x=6,y=21,n=11
Main--x=5,y=1,n=11
Func--x=8,y=31,n=21
5.5 非递归的
#include<iostream>
#include<cmath>
using namespace std;
int f(int n)
{
int a=0,b=1,t,i; //如果第一项需要为1改成a=1
for (i = 2; i <= n; i++)
{
t = a + b;
a = b;
b = t;
}
return a;
}
int main()
{
int n;
cin >> n;
cout << f(n) << endl;
return 0;
}