2018年8月30日拼多多笔试题题解
好久没水贴了,发一个凑凑热闹(
第一题:打怪物,贪心,比一下蓄力和不蓄力区别就行了
#include <bits/stdc++.h>
using namespace std;
long long int hp, n, b;
int ans = 0;
int main() {
std::ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
cin >> hp >> n >> b;
ans = 0;
if (2*n > b) {
long long int t = hp / (2*n);
ans += t*2;
t = hp % (2*n);
while(t > 0) {
t -= n;
ans ++;
}
} else {
long long int t = hp / b;
ans += t*2;
t = hp % b;
int c = 0;
while(t > 0) {
t -= n;
c ++;
}
ans = ans + min(c, 2);
}
cout << ans << endl;
return 0;
}
第二题:模拟按规则木块下落
#include <bits/stdc++.h>
using namespace std;
int n, m;
char mat[20][20];
int main() {
std::ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%s", &mat[i]);
}
for (int i = 0; i < m; ++i) {
for (int j = n-1; j >= 0; --j) {
if (mat[j][i] == 'x') continue;
if (mat[j][i] == '.') continue;
int now = j;
int k = j + 1;
while(k < n) {
if (mat[k][i] == 'x') break;
if (mat[k][i] == 'o') break;
swap(mat[k][i], mat[now][i]);
now ++;
k ++;
}
if (k == n) {
mat[now][i] = '.';
}
}
}
for (int i = 0; i < n; ++i) {
printf("%s\n", mat[i]);
}
return 0;
}
第三题:求小数循环节开始和循环节长度
模拟除法
#include <bits/stdc++.h>
using namespace std;
int n, m;
unordered_map<int, int> ma;
pair<int, int> solve(int n, int m) {
ma.clear();
int now = 0;
int t, h;
n = n % m;
while(true) {
n = n % m;
t = n % m;
if (ma.find(n) == ma.end()) {
ma[n] = now;
} else {
h = now - ma[n];
return make_pair(ma[n], h);
}
//cout << n << " " << m << " " << t << endl;
if (t == 0) {
return make_pair(now, 0);
}
n = t;
n *= 10;
now ++;
}
}
int main() {
std::ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
cin >> n >> m;
auto ans = solve(n, m);
cout << ans.first << " " << ans.second << endl;
return 0;
}
第四题:按照字典序dfs一下就行了,输入最大为2000个字符串,所以蛮快的,2000次必结束
#include <bits/stdc++.h>
using namespace std;
int n, m;
string sarr[2005];
set<string> se;
set<char> cse[15];
string gs;
bool flag = false;
string ans;
void dfs(int id) {
if (id >= m) {
// find
if (se.find(gs) != se.end()) {
return;
} else {
ans = gs;
flag = true;
return;
}
}
auto it = cse[id].begin();
while(it != cse[id].end()) {
gs[id] = *it;
dfs(id + 1);
if (flag) return;
++it;
}
}
int main() {
std::ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> sarr[i];
se.insert(sarr[i]);
for (int j = 0; j < m; ++j) {
cse[j].insert(sarr[i][j]);
}
}
gs = "";
for (int i = 0; i < m; ++i) gs += 'a';
flag = false;
dfs(0);
if (flag) {
cout << ans << endl;
} else {
cout << "-" << endl;
}
return 0;
}
#拼多多##题解#