武汉科技大学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; }
相关文章推荐
- 武汉科技大学ACM:1006: 华科版C语言程序设计教程(第二版)例题4.17
- 武汉科技大学ACM :1009: 华科版C语言程序设计教程(第二版)例题4.18
- 武汉科技大学ACM:1002: 华科版C语言程序设计教程(第二版)例题6.6
- 武汉科技大学ACM:1003: 华科版C语言程序设计教程(第二版)例题6.6.改编
- 武汉科技大学ACM:1005: 华科版C语言程序设计教程(第二版)例题5.8
- 武汉科技大学ACM :1006: 华科版C语言程序设计教程(第二版)习题7.15
- 武汉科技大学ACM:1009: 华科版C语言程序设计教程(第二版)例题5.4
- 武汉科技大学ACM :1010: 华科版C语言程序设计教程(第二版)例题7.8
- 武汉科技大学ACM :1007: 华科版C语言程序设计教程(第二版)例题4.13
- 武汉科技大学ACM:1001: 华科版C语言程序设计教程(第二版)习题6.7
- 武汉科技大学ACM :1001: 华科版C语言程序设计教程(第二版)课后习题3.12
- 武汉科技大学ACM:1004: 华科版C语言程序设计教程(第二版)习题5.6
- 武汉科技大学ACM :1004: 华科版C语言程序设计教程(第二版)课后习题3.7
- 武汉科技大学ACM :1007: 华科版C语言程序设计教程(第二版)习题5.7
- 武汉科技大学ACM :1007: 华科版C语言程序设计教程(第二版)习题7.10
- 武汉科技大学ACM :1008: 华科版C语言程序设计教程(第二版)习题6.14
- 武汉科技大学ACM :1009: 华科版C语言程序设计教程(第二版)习题6.11
- 武汉科技大学ACM:1009: 华科版C语言程序设计教程(第二版)习题5.12
- 1239: 华科版C语言程序设计教程(第二版)例题6.17
- 1206: 华科版C语言程序设计教程(第二版)例题4.3