您的位置:首页 > 编程语言 > C语言/C++

武汉科技大学ACM :1006: 华科版C语言程序设计教程(第二版)例题4.15

2014-12-05 20:06 225 查看

Problem Description

输入两个整数,判断他们是否互质。

Input

两个整数。(多组数据)

Output

如果他们互质,则输出“yes”,否则,输出“no”。

Sample Input

2 3

Sample Output

yes

我的代码:
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main()
{
long a,b,t;
while(cin>>a>>b)
{
int r=0;
if((a==-1)&&(b==-1))break;
if(b==1)r=1;
long temp;
while(a!=b)
{
if(a<b)
{
temp=a;
a=b;
b=temp;
}
t=b;
b=a-t;
a=t;
}
if(b==1)r=1;
if(r==1)cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
system("pause");
}
其他代码:
#include <iostream>

using namespace std;
int mgcd(int a,int b)
{
int t;
if(a<b)
{
t=a;a=b;b=t;
}
while(a%b)
{
t=b;
b=a%b;
a=t;
}
return b;
}
int main()
{
int a,b;
while(cin>>a>>b)
{
if(mgcd(a,b)==1)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐