CodeForces 1101A Minimum Integer(思维)

题意:给出q个问题,每个问题占一行,每行有3个整数l,r,d,求出d的最小倍数x,使得x不在闭区间[l,r]内。

思路:
d有两种情况:
1.d小于l或者d大于r,则x=d;
2.d在[l,r]内,则x是d的r/d+1倍,即x=d*(r/d+1);
伪代码如下:
int q,l,r,d;
int ans;
scanf("%d",&q);
while(q–){
scanf("%d%d%d",&l,&r,&d);
if(d<l || d>r) 输出d;
else{
ans=d*(r/d+1);输出ans;
}
}
return 0;

代码如下:

#include<cstdio>
using namespace std;
int main()
{
	int q;
	long long l,r,d,ans;
	scanf("%d",&q);
	while(q--)
	{
		ans=0;
		scanf("%lld %lld %lld",&l,&r,&d);
		if(d<l||d>r) printf("%lld\n",d);
		else
		{
			ans=d*(r/d+1);
			printf("%lld\n",ans);
		}
	}
	return 0;
 }

猜你喜欢

转载自blog.csdn.net/shamansi99/article/details/87898491