9.14百度B题 1 0.32
第一题
#include <bits/stdc++.h> using namespace std; int fanZhuan(int a) { int res = 0; while (a > 0) { res += a % 10; a = a / 10; if (a > 0) res *= 10; } return res; } int main() { int N, K; cin >> N >> K; int maxRes = INT_MIN; int curRes; int temp = 0; for (int i = 0; i != K; ++i) { temp += N; curRes = fanZhuan(temp); maxRes = max(maxRes, curRes); } cout << maxRes << endl; system("pause"); return 0; }
第二题
#include <bits/stdc++.h> using namespace std; string input; int resLeft; int resRight; bool hasNeed(int left, int right, vector<int>& dp) { int OneCount = dp[right] - dp[left - 1]; while (right < dp.size()) { ++right; left++; if (right <= dp.size() && OneCount == dp[right] - dp[left - 1]) { resLeft = left; resRight = right; return true; } } return false; } int main() { cin >> input; int n = input.size(); // n个数 vector<int> dp(n + 1, 0); for (int i = 0; i != n; ++i) { if (input[i] == 1) dp[i + 1] = dp[i] + 1; else dp[i + 1] = dp[i]; } int left = 1, right = n - 1; while (right >= left) { if (hasNeed(left, right, dp)) break; --right; } cout << left << " " << right << " " << resLeft << " " << resRight << endl; return 0; }