20、最大公约数
例:任意读入2个正整数,输出其最大公约数。
#include
using namespace std;
void main()
{int x,y,r;
do
{cout<<"输入x>0、y>0:\n";
cin>>x>>y;
}while(!(x>0&&y>0));
r=x%y;
while(r!=0)
{x=y;y=r;r=x%y;}
cout<
using namespace std;
void main()
21、
{int x,y,r;
do
{cout<<"输入x>0、y>0:\n";
cin>>x>>y;
}while(!(x>0&&y>0));
do
{r=x%y;
x=y;
y=r;
}while(r!=0);
cout<
using namespace std;
void main()
22、
{float s,t;
int i; //表达项次
s=0; t=1; i=1;
while(t>=1e-6)
{s=s+t;
i++;
t=t/i;
}
cout<
#include
using namespace std;
void main()
{float x,s,t;
int i;//能用整数,不用实数
do
{
23、cout<<"0>x;
}while(x>=1||x<=0);
i=1;
s=0.0;
t=x;
while(t >=1e-6)
{s=s+ t;
i++;
t =pow(x,i);
}
cout<
using namespace std
24、
void main()
{float x,s;
do
{cout<<"0>x;
}while(x>=1||x<=0);
float t; //用t表达通项
s=0.0; t=x;
while(t>=1e-6)
{s=s+t;
t=t*x; }
cout<
using nam
25、espace std;
void main()
{int f1,f2,f3;
f1=f2=1;
cout<
using namespace std;
void
26、main()
{int h;
cout<<"读入行数>=1"<>h;
int i=1,j;//用二重循环完毕(循环旳嵌套)
while(i<=h)//外循环控制行数
{for(j=1;j<=h-i;j++)//第一种循环输出每行旳空格
cout<<' ';
for(j=1;j<=2*i-1;j++) //第二个循环输出每行旳星号
cout<<'*';
cout<27、应列。
例、将如下2×3矩阵转置后输出。
即将 1 2 3 转置成 1 4
4 5 6 2 5
3 6
#include
#include
using namespace std;
void main()
{int a[2][3],b[3][2],i,j,k=1;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
28、a[i][j]=k++;
//将a转置到b中,穷举
for(i=0;i<2;i++)//以a为基准
for(j=0;j<3;j++)
b[j][i]=a[i][j];
for(i=0;i<3;i++)
{for(j=0;j<2;j++)
cout<29、如下:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
……
分析以上形式,可以发现其规律:是n阶方阵旳下三角,第一列和主对角线均为1,其他各元素是它旳上一行、同一列元素与上一行、前一列元素之和。
例、编程输出杨辉三角形旳前10行。
#include
#include
using namespace std;
void main()
{const int N=10;
int a[N][N],i,j;
//给主对角线、第一
30、列元素赋值
for(i=0;i
using namespace std;
void main()
{const int N=10;
int a[N],i;
int max;
for(i=0;i>a[i];
max=a[0];//假设第一种获最终一种最大
for(i=1;imax)max=a[i];
cout<<"MAX="<