Educational Codeforces Round 96 (Rated for Div. 2)
A - Number of Apartments
题意
给一个数n,输出一组a,b,c,使得 3 × a + 5 × b + 7 × c = n 3\times a+5\times b+7\times c=n 3×a+5×b+7×c=n ,输出任意一组解。( 1 ≤ t ≤ 1000 1≤t≤1000 1≤t≤1000 , 1 ≤ n ≤ 1000 1≤n≤1000 1≤n≤1000)
分析
先考虑枚举,先枚举 5b 和 7c,判断 (n-5b-7c)%3==0。复杂度完全可以。
code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
while (n--) {
int x;
scanf("%d", &x);
bool flag = false;
for (int i = 0; i <= x; i += 5) {
for (int j = 0; j <= x; j += 7) {
int sum = i + j;
if (sum > x) break;
if ((x - sum) % 3 == 0) {
printf("%d %d %d\n", (x - sum) / 3, i / 5, j / 7);
flag = true;
break;
}
}
if (flag) break;
}
if (!flag) printf("-1\n");
}
return 0;
}
分析
简单的做法,观察一下会发现 3%3=0,7%3=1,5%3=2,这样除了1,2,4这三个数没有答案,其他数字都可表示出来。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
if (n == 1 || n == 2 || n == 4) {
cout << -1 << endl;
} else if (n % 3 == 0) {
cout << n / 3 << ' ' << 0 << ' ' << 0 << endl;
} else if (n % 3 == 1) {
cout << (n - 7) / 3