HDU2504

在这里插入图片描述

你说为啥每个题的坑我都会踩呢
惯性的以为c=2*b就行了
其实不是
比如144和4
如果c等于8
那么最大公约数不是4
而是8
这就说明
要得到最小的满足题意的c就得进行判断吧
用gcd吧

#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
	return b?gcd(b,a%b):a;
}
int main()
{
	int t,a,b;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&a,&b);
		int c=2*b;
		while(gcd(a,c)!=b)
		{
			c+=b;
		}
		printf("%d\n",c);
	}
	return 0;
}
发布了54 篇原创文章 · 获赞 4 · 访问量 878

猜你喜欢

转载自blog.csdn.net/weixin_45460987/article/details/103465351
hdu
今日推荐