1,暴力法 原理当a,b互质时(1,2,3,....(b-2),(b-1))*a%b!=0 #include<cstdio> int main() { int step, mod, x, count; while (~scanf("%d%d", &step, &mod)) { x = 0, count = 0; do { x = (x + step) % mod; ++count; } while (x && count != mod); printf("%10d%10d %snn", step, mod, count == mod && x == 0 ? "Good Choice" : "Bad Choice"); } return 0; } 2 辗转相除法 a>b 如果a,b有公约数则a-b,与b也有公约数#include <iostream> #include <string> #include <iomanip> using namespace std; int main() { long a,b,t=0; while(cin>>a>>b) { long A=a,B=b; if (a<b) { t=a; a=b; b=t; } while(b!=1&&b!=0) { t=a; a=b; b=t%b; } if(b!=1) cout<<setw(10)<<A<<setw(10)<<B<<" Bad Choice"<<endl; else cout<<setw(10)<<A<<setw(10)<<B<<" Good Choice"<<endl; cout<<endl; } return 0; }
uva 408
最新推荐文章于 2024-04-24 15:15:48 发布