一、实验题目
利用四种不同的算法进行对二个数求最大公约数。四种算法分是辗转相除法、穷举法、更损相减法、Stein算法。
一.实验目的
- 明确算法的概念和特点。
- 通过对问题的分析,设计合理的算法解决问题;
二.实验内容
运行最大公约数的常用算法,并进行程序的调式与测试,要求程序设计风格良好,并添加异常处理模块(如输入非法等)。
1.辗转相除法
2.穷举法
3.更损相减法
4.stein算法
# include<iostream>
# include<cstdlib>
# include<time.h>
#include<cmath>
using namespace std;
class TIME//定义一个时间类
{
public://对成员函数进行声明
int first(int a,int b);
int secend(int a,int b);
int third(int m,int n);
int four( unsigned int x, unsigned int y );
};
//对成员函数进行函数的定义
int TIME::first(int a,int b)
{
int temp; /*定义整型变量*/
if(a<b) /*通过比较求出两个数中的最大值和最小值*/
{ temp=a;a=b;b=temp;} /*设置中间变量进行两数交换*/
while(b!=0) /*通过循环求两数的余数,直到余数为0*/
{
temp=a%b;
if(temp==0)
printf("最大公约数为%d\n",b);
a=b; /*变量数值交换*/
b=temp;
}
return (a);
};
int TIME::secend(int a,int b)
{
int temp; /*定义义整型变量*/
temp=(a>b)?b:a; /*采种条件运算表达式求出两个数中的最小值*/
while(temp>0)
{
if (a%temp==0&&b%temp==0) /*只要找到一个数能同时被a,b所整除,则中止循环*/
{
printf("最大公约数为%d\n",temp);
break;
}
temp--; /*如不满足if条件则变量自减,直到能被a,b所整除*/
}
return (temp); /*返回满足条件的数到主调函数处*/
};
int TIME::third(int m,int n)
{
int i=0,temp,x;
while(m%2==0 && n%2==0) //判断m和n能被多少个2整除
{
m/=2;
n/=2;
i+=1;
}
if(m<n) //m保存大的值
{
temp=m;
m=n;
n=temp;
}
while(x)
{
x=m-n;
m=(n>x)?n:x;
n=(n<x)?n:x;
if(n==(m-n))
{
break;}
}
if(i==0)
{
printf("最大公约数为%d\n",n);
return n;
}
else { printf("最大公约数为%d\n",(int )pow(2,i)*n);
return (int )pow(2,i)*n;
}
};
int TIME::four( unsigned int x, unsigned int y )
/* return the greatest common divisor of x and y */
{
int factor = 0;
int temp;
if ( x < y )
{
temp = x;
x = y;
y = temp;
}
if ( 0 == y )
{
return 0;
}
while ( x != y )
{
if ( x & 0x1 )
{/* when x is odd */
if ( y & 0x1 )
{/* when x and y are both odd */
y = ( x - y ) >> 1;
x -= y;
}
else
{/* when x is odd and y is even */
y >>= 1;
}
}
else
{/* when x is even */
if ( y & 0x1 )
{/* when x is even and y is odd */
x >>= 1;
if ( x < y )
{
temp = x;
x = y;
y = temp;
}
}
else
{/* when x and y are both even */
x >>= 1;
y >>= 1;
++factor;
}
}
}
printf("最大公约数为%d\n",x << factor);
return ( x << factor );
};
int main()//主函数
{
int x;//定义x
printf("请选择求最大公约数的方法:\n");//输出语句使用户进行选择
printf("辗转相除法选1\n");
printf("穷举法选2\n");
printf("更损相减法选3\n");
printf("Stein算法选4\n");
cin>>x;
long int s = 10000000L; //关于时间函数的调用
clock_t start, finish;//定义对象
double duration;
/* 测量一个事件持续的时间*/
start = clock();//计时开始
switch(x)//switch语句的调用
{
case 1:
{
TIME h;
for(int i=0;i<20;i=i+2)
{
int str[20]={12,21,33,43,37,45,15,45,11,76,21,51,44,78,98,67,76,28,36,64};//定义一个20位数字的数组
h.first(str[i],str[i+1]);//求最大公约数函数调用
}
break;
}
case 2:
{
TIME h;
for(int i=0;i<20;i=i+2)
{
int str[20]={12,21,33,43,37,45,15,45,11,76,21,51,44,78,98,67,76,28,36,64};//定义一个20位数字的数组
h.secend(str[i],str[i+1]);//求最大公约数函数调用
}
break;
}
case 3:
{
TIME h;
for(int i=0;i<20;i=i+2)
{
int str[20]={12,21,33,43,37,45,15,45,11,76,21,51,44,78,98,67,76,28,36,64};//定义一个20位数字的数组
h.third(str[i],str[i+1]);//求最大公约数函数调用
}
break;
}
case 4:
{
TIME h;
for(int i=0;i<20;i=i+2)
{
int str[20]={12,21,33,43,37,45,15,45,11,76,21,51,44,78,98,67,76,28,36,64};//定义一个20位数字的数组
h.four(str[i],str[i+1]);//求最大公约数函数调用
}
break;
}
}
finish = clock(); //计时结束
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "运行时间为%f毫秒\n", duration*1000 );//输出时间单位为毫秒
system("pause");
return 0;
}