D - Three Integers

D - Three Integers

D - Three Integers

 

 D - Three Integers

 

 D - Three Integers

 

 思路:枚举,还是枚举,走完所有的数字,最后找到最小的,然后输出。

代码:

#include <iostream>
#include<algorithm>
#include<string>
#include<math.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int main()
{
    int t, a, b, c, x, y, z, cnt, sum;
    scanf("%d", &t);
    while (t--)
    {
        cin >> a >> b >> c;
        cnt = inf;
        for (int i = 1; i <= 20000; i++)
        {
            for (int j = 1; j*i <= 20000; j++)
            {
                for (int k = 1; i*j*k <= 20000; k++)
                {
                    sum = abs(i-a) + abs(j*i - b) + abs(i*j*k - c);
                    if (cnt>sum)
                    {
                        cnt = sum;
                        x = i;
                        y = i*j;
                        z = i*j*k;
                    }
                }
            }
        }
        printf("%d\n", cnt);
        printf("%d %d %d\n", x, y, z);
    }
    return 0;
}

 

 

 

 

上一篇:用指向指针的指针的方法对n个整数排序并输出要求将排序单独写成一个函数。n个整数在主函数中输入,最后在主函数中输出


下一篇:C语言2.0