社区讨论
求助90pts
学术版参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m56heu74
- 此快照首次捕获于
- 2024/12/27 16:19 去年
- 此快照最后确认于
- 2025/11/04 12:18 4 个月前
P1874 快速求和
CPP#include <bits/stdc++.h>
using namespace std;
int f(const string& s, int n, int x, int y, int z) {
int len = s.size();
if (x == len) {
return z == n ? y : INT_MAX;
}
int ans = INT_MAX;
int num = 0;
for (int i = x; i < len; ++i) {
num = num * 10 + (s[i] - '0');
if (num > n) break;
ans = min(ans, f(s, n, i + 1, y + 1, z + num));
}
return ans;
}
int main() {
string s;
int n;
cin >> s >> n;
int ans = f(s, n, 0, -1, 0);
if (ans == INT_MAX) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...