// commonDivisor.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
int getcd(int u,int v)
{
int r;
while(v!=0)
{
r=u%v;
u=v;
v=r;
}
return u;
}
int getCommonDivisor(int u,int v)
{
while(v!=u)
{
if(u>v)
u-=v;
else
v-=u;
}
return u;
}
int main(int argc, char* argv[])
{
int ret=getcd(33,96);
int ret2=getCommonDivisor(33,96);
printf("%d,%d\n",ret,ret2);
ret=getcd(75,95);
ret2=getCommonDivisor(75,95);
printf("%d,%d\n",ret,ret2);
ret=getcd(175,496);
ret2=getCommonDivisor(175,496);
printf("%d,%d\n",ret,ret2);
int a,b;
srand(time(NULL));
int count=0;
for(int i=0;i<1000;i++)
{
a=rand() % 10000;
b=rand()% 10000;
ret=getcd(a,b);
ret2=getCommonDivisor(a,b);
printf("%d,%d:%d,%d\n",a,b,ret,ret2);
if(ret!=ret2)
count++;
}
printf("ret!=ret2:%d\n",count);
return 0;
}
/*
9150,160:10,10
6445,1161:1,1
7054,1058:2,2
9419,2257:1,1
2191,2515:1,1
4523,4173:1,1
8441,7627:1,1
3216,7105:1,1
4209,9242:1,1
6736,1182:2,2
1024,8758:2,2
8067,9516:3,3
ret!=ret2:0
Press any key to continue
*/
最大公约数
最新推荐文章于 2018-12-04 17:31:00 发布