合适数对

由于 \(a\)\(b\)\(n\)\(x\)\(y\) 都是非负整数,所以 \(n≤1000\) 的话 \(x,y ≤ 1000\)
数据范围并不大,我们直接枚举从 \(1 \sim 1000\) 枚举 \(x\)\(y\) 就行。

#include <iostream>
#include <cstdio>
using namespace std;
int n, a, b;
int main() {
    cin >> n >> a >> b;
    for (int x = 0; x <= 1000; x ++ )
        for (int y = 0; y <= 1000; y ++ ) {
            if((a * x + b * y) == n) {
                printf("YES\n%d %d", x, y);
                return 0;
            }
        }
    printf("NO");
    return 0;
}

顺便提一下,由于 \(x\) 是从小到大枚举的,所以第一个答案一定是 \(x\) 最小的方案。

posted @ 2022-02-05 20:19  福宝j  阅读(38)  评论(0编辑  收藏  举报