#Three Integers

You are given three integers a≤b≤ca≤b≤c.

In one move, you can add +1+1 or −1−1 to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this operation several times with one number. Note that you cannot make non-positive numbers using such operations.

You have to perform the minimum number of such operations in order to obtain three integers A≤B≤CA≤B≤C such that BB is divisible by AA and CC is divisible by BB.

You have to answer tt independent test cases.

Input
The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The next tt lines describe test cases. Each test case is given on a separate line as three space-separated integers a,ba,b and cc (1≤a≤b≤c≤1041≤a≤b≤c≤104).

Output
For each test case, print the answer. In the first line print resres — the minimum number of operations you have to perform to obtain three integers A≤B≤CA≤B≤C such that BB is divisible by AA and CC is divisible by BB. On the second line print any suitable triple A,BA,B and CC.

Example
Input
8
1 2 3
123 321 456
5 10 15
15 18 21
100 100 101
1 22 29
3 19 38
6 30 46
Output
1
1 1 3
102
114 228 456
4
4 8 16
6
18 18 18
1
100 100 100
7
1 22 22
2
1 19 38
8
6 24 48
题意就是找到使b能被a整除,c能被b整除的数,且(+1或-1)所用的次数最少。这一题没想到什么简单的方法,只能用三层for循环暴力写,幸亏没有出现超时什么的错误。下面是代码部分:

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		int a,b,c,a1,b1,c1,i,j,k,m=999999999,sum=0;		
		scanf("%d %d %d",&a,&b,&c);
		for(i=1;i<=20000;i++)
		{
			for(j=i;j<=20000;j=j+i)
			{
				for(k=j;k<=20000;k=k+j)
				{
					sum=abs(a-i)+abs(b-j)+abs(c-k);
					if(m>sum)
					{
						m=sum;
						a1=i;
						b1=j;
						c1=k;
					}
				}
			}
		}
		printf("%d\n",m);
		printf("%d %d %d\n",a1,b1,c1);
	}
	return 0;
}
发布了18 篇原创文章 · 获赞 0 · 访问量 207

猜你喜欢

转载自blog.csdn.net/qq_46304792/article/details/105233134