实验九

来源:互联网 发布:全问号 乱码 java 编辑:程序博客网 时间:2024/04/28 23:34

LAP9_1


/*    求两个整数的最大公约数和最小公倍数。*/

#include<iostream>#include<conio.h>using namespace std;int main()  {      int m,n,t;      m=15;      n=10;      if(m < n) //m小于n时交换两数,即m中始终存放两数中较大的    {          t = m;          m = n;          n = t;      }      int m0=m,n0=n;      while(n!=0) //循环判断,m中存放最大公约数    {          t = m % n;          m = n;          n = t;      }      cout <<"最大公约数为: " <<m <<endl;      cout <<"最小公倍数为: " <<m0*n0/m <<endl; //通过找到的最大公约数,求最小公倍数    _getch();    return 0;} 

LAP9_2

/*
    用循环结构语句编程并输出下图:
                1
                22
                333
                4444
                55555
                4444
                333
                22
                1

*/

#include<iostream>#include<conio.h>using namespace std;int main(){    for(int i = 1; i <= 5; i++)    {        for(int j = 1; j <= i; j++)            cout<<i;        cout<<endl;    }    for(int i = 4; i >= 1; i--)    {        for(int j = 1; j <= i; j++)            cout<<i;        cout<<endl;    }    _getch();    return 0;}


原创粉丝点击