CodeForces - 1131D(Three Integers)

Posted dont-starve

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1131D(Three Integers)相关的知识,希望对你有一定的参考价值。

题目链接:https://codeforces.com/contest/1311/problem/D

题意:

给出三个整数 a ≤ b ≤ c ,您可以添加+1 或-1 到这三个整数中的任何一个增加或减少 1 ,可以执行任意次(可能为零)次这样的操作,甚至可以一次执行多次。求执行此类操作的最少次数,以获得三个整数 0 ≤ A ≤ B ≤ C 这样B可以被A整除,C可以被B整除 。

思路:

暴力, 枚举 A, B, C;

for (int i = 1; i <= MAXN * 2; ++i)
for (int j = i; j <= MAXN * 2; j += i)
for (int k = j; k <= MAXN * 2; k += j)

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10000;

inline void solve() 
{
    int a, b, c; cin >> a >> b >> c;
    int ansa, ansb, ansc, maxn = 100000;
    for (int i = 1; i <= MAXN * 2; ++i)
    {
        for (int j = i; j <= MAXN * 2; j += i)
        {
            for (int k = j; k <= MAXN * 2; k += j)
            {
                int temp = abs(a - i) + abs(b - j) + abs(c - k);
                if (temp < maxn)
                {
                    maxn = temp;
                    ansa = i, ansb = j, ansc = k;
                }
            }
        }
    }
    cout << maxn << endl;
    cout << ansa << " " << ansb << " " << ansc << endl;
}
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int T = 1; cin >> T;
    for (int i = 0; i < T; ++i) solve();
    return 0;
}

以上是关于CodeForces - 1131D(Three Integers)的主要内容,如果未能解决你的问题,请参考以下文章

codeforces-1335-E Three Blocks Palindrome

codeforces 653A A. Bear and Three Balls(水题)

Divide by Three CodeForces - 792C

题解[CodeForces1154A]Restoring Three Numbers

Codeforces Round #485 (Div. 2) C. Three displays

Codeforces Round #485 (Div. 2)-C-Three displays