题目1493:公约数
时间限制:1 秒
内存限制:128 兆
特殊判题:否
提交:2354
解决:375
-
题目描述:
-
给定两个正整数a,b(1<=a,b<=100000000),计算他们公约数的个数。
如给定正整数8和16,他们的公约数有:1、2、4、8,所以输出为4。
-
输入:
-
输入包含多组测试数据,每组测试数据一行,包含两个整数a,b。
-
输出:
-
对于每组测试数据,输出为一个整数,表示a和b的公约数个数。
-
样例输入:
-
8 16 22 16
-
样例输出:
-
4 2
-
-
#include<cstdio> #include<cmath> using namespace std; int max(int a,int b) { if(b>a){a=a+b;b=a-b;a=a-b;} if(a%b==0)return b; else return max(b,a%b); } int num(int n) { int i,j,count=0; for(j=(int)sqrt(1.0*n),i=1; i<=j; ++i) if(n%i==0)count+=2; if(j*j==n)--count; return count; } int main() { int a,b,c; while(scanf("%d%d",&a,&b)!=EOF &&a &&b ) { c=num( max(a,b) ); printf("%d\n",c); } return 0; } /************************************************************** Problem: 1493 User: 3011216016 Language: C++ Result: Accepted Time:20 ms Memory:1032 kb ****************************************************************/