程序八
来源:互联网 发布:电脑屏幕监控软件 编辑:程序博客网 时间:2024/04/29 09:05
题目:输入两个正整数m和n,求其最大公约数和最小公倍数.
1.程序分析:利用辗除法.
2.程序源代码:
#include <iostream>
#include <math.h>
using namespace std;
void getMaxDivisor( unsigned int numOne, unsigned int numTwo )
{
if( numOne < 2 || numTwo < 2 )
return ;
unsigned int bufOne[1024] = {0};
unsigned int bufTwo[1024] = {0};
unsigned int idxOne = 0;
unsigned int idxTwo = 0;
unsigned int maxDivisor = 1;
for( int i=2; i<=sqrt(numOne); i++ )
{
while( (numOne % i) == 0 )
{
numOne = numOne / i;
bufOne[idxOne++] = i;
}
}
if(numOne >= i)
bufOne[idxOne++] = numOne;
for( int j=2; j<=sqrt(numTwo); j++ )
{
while( (numTwo % j) == 0 )
{
numTwo = numTwo / j;
bufTwo[idxTwo++] = j;
}
}
if(numTwo >= j)
bufTwo[idxTwo++] = numTwo;
for( int k=0; k<idxOne; k++)
cout<<bufOne[k]<<" ";
cout<<endl;
for( k=0; k<idxTwo; k++)
cout<<bufTwo[k]<<" ";
cout<<endl;
for( i=0; i<idxOne; i++)
{
for( j=0; j<idxTwo; j++)
{
if( (bufOne[i] == bufTwo[j]) && (bufOne[i] != 0) )
{
maxDivisor *= bufOne[i];
bufOne[i] = 0;
bufTwo[j] = 0;
}
}
}
cout<<"The max Divisor is "<<maxDivisor<<endl;
}
void getMinMultiple( unsigned int numOne, unsigned int numTwo )
{
if( numOne < 2 || numTwo < 2 )
return;
unsigned int bufOne[1024] = {0};
unsigned int bufTwo[1024] = {0};
unsigned int idxOne = 0;
unsigned int idxTwo = 0;
unsigned int maxDivisor = 1;
for( int i=2; i<=sqrt(numOne); i++ )
{
while( (numOne % i) == 0 )
{
numOne = numOne / i;
bufOne[idxOne++] = i;
}
}
if(numOne >= i)
bufOne[idxOne++] = numOne;
for( int j=2; j<=sqrt(numTwo); j++ )
{
while( (numTwo % j) == 0 )
{
numTwo = numTwo / j;
bufTwo[idxTwo++] = j;
}
}
if(numTwo >= j)
bufTwo[idxTwo++] = numTwo;
for( int k=0; k<idxOne; k++)
cout<<bufOne[k]<<" ";
cout<<endl;
for( k=0; k<idxTwo; k++)
cout<<bufTwo[k]<<" ";
cout<<endl;
for( i=0; i<idxOne; i++)
{
for( j=0; j<idxTwo; j++)
{
if( (bufOne[i] == bufTwo[j]) && (bufOne[i] != 0) )
{
maxDivisor *= bufOne[i];
bufOne[i] = 0;
bufTwo[j] = 0;
}
}
}
for( i=0; i<idxOne; i++)
{
if(bufOne[i] != 0)
maxDivisor *= bufOne[i];
}
for( j=0; j<idxTwo; j++)
if(bufTwo[j] != 0)
maxDivisor *= bufTwo[j];
cout<<"The min Multiple is "<<maxDivisor<<endl;
}
void main()
{
getMinMultiple( 12, 234 );
}
- 程序八
- 八皇后问题程序
- 八、程序流程控制
- 行政区划程序的设计(八)
- 模板元程序(八)
- 八皇后C++完整程序
- 八皇后C++完整程序
- 性能优化(八) - 前台程序
- Android天气预报程序(八)
- 小程序八:操作反馈
- 微信小程序(八):地图
- 八人过河 程序 C++
- 八皇后问题程序及注解
- [转帖]八皇后问题程序及注解
- 网络数据包监视程序开发(八)
- .NET Remoting程序开发入门篇(八)
- 程序物语(八):我心戚戚
- C程序100实例之八
- 添加删除修改用户命令
- 添加删除修改用户命令
- 测试
- mvc
- 如何让ORACLE索引不起作用(不可见)
- 程序八
- Windows Xp 扫雷外挂(控制台版)
- mysql清除日志以及mysql清空表的方法介绍
- 从罗马帝国精英军团/秦帝国军制谈iOS/Android
- LINUX目录结构
- 刘铁岩《良师益友》系列
- WCF系列(一)地址与绑定
- QQ靓号申请器v1.1.0.0【已更新】
- Model View Presenter (MVP) design pattern and data binding