算法 c++ 2027天梯赛部分题解 - second] == 1){

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力神器 👁️ 211 次阅读

今年不在学校没有参加,昨天和今天把题给做了一遍,今年的题感觉挺简单,自己做完没限制时间拿了251分,参加的话应该能拿个银奖,太亏了,去年第一次参加才150多分。

下面题解有自己写的还有结合别人的修改的。

L1-1 编程解决一切

#include

using namespace std;

int main(){

cout << "Problem? The Solution: Programming.";

return 0;

}

L1-2 再进去几个人

#include

using namespace std;

int main(){

int a, b;

cin >> a >> b;

cout << b - a;

return 0;

}

L1-3 帮助色盲

#include

using namespace std;

int main(){

int a, b;

cin >> a >> b;

if(a < 2){ // 红绿灯需要提示

if(b) cout << "-\n" << (a ? "move" : "stop");

else {

if(a) cout << "dudu\n" << "move";

else cout << "biii\n" << "stop";

}

}else{ // 黄灯不需要提示

cout << "-\nstop";

}

return 0;

}

L1-4 四项全能

#include

using namespace std;

int main(){

int n, m, k, sum = 0;

cin >> n >> m;

for(int i = 0; i < m; i++){

cin >> k;

sum += k;

}

int a = sum / n, b = sum % n;

if(a == m) cout << n;

else if(a == m - 1) cout << b;

else cout << 0;

return 0;

}

L1-5 别再来这么多猫娘了

#include

#include

using namespace std;

vectorwj;

string s, presub = "\u0233", substitute = "", line;

int n, cnt, upper;

size_t pos;

int main(){

cin >> n;

wj.resize(n);

for (int i = 0; i < n; i++) cin >> wj[i];

cin >> upper; getchar();

getline(cin, line);

for(auto &it:wj){

pos = line.find(it, pos);

while(pos != string::npos){

line.replace(pos, it.size(), presub);

cnt ++;

pos = line.find(it, pos);

}

pos = 0;

}

if (cnt < upper){

pos = 0; pos = line.find(presub, pos);

while(pos != string::npos){

line.replace(pos, presub.size(), substitute);

pos = line.find(presub, pos);

}

cout << line;

}else{

cout << cnt <

cout << "He Xie Ni Quan Jia!";

}

return 0;

}

L1-6 兰州牛肉面

#include

#include

#include

using namespace std;

int n, a, b;

double sum;

vectorp;

vectorcnt;

int main(){

cin >> n;

p.resize(n + 1);

cnt.resize(n + 1);

for(int i = 1; i <= n; i++) cin >> p[i];

while(1){

cin >> a >> b;

if(!a) break;

cnt[a] += b;

sum += p[a] * b;

}

for(int i = 1; i <= n; i ++) cout << cnt[i] << endl;

printf("%.2f", sum);

return 0;

}

L1-7 整数的持续性

#include

#include

using namespace std;

int a, b, max_cnt, t, next_num;

vector> cnt;int calnext(int i){

int t = 1;

while(i){

t *= i % 10;

i /= 10;

}

return t;

}

int main(){

cin >> a >> b;

for(int i = a; i <= b; i++){

t = 0, next_num = i;

while(next_num < 0 || next_num > 9){

t ++;

next_num = calnext(next_num);

}

max_cnt = max(max_cnt, t);

cnt.push_back({i, t});

}

cout << max_cnt << endl;

int flag = 0;

for(auto &it: cnt){

if(it.second == max_cnt){

if(!flag) cout << it.first, flag = 1;

else cout << " " << it.first;

}

}

return 0;

}

L1-8 九宫格

#include

#include

using namespace std;

vector> g(9, vector(9, 0));bool check(){

// check row

for (int i = 0; i < 0; i++)

{

int cnt[10] = {0};

for (int j = 0; j < 9; j++)

{

if (g[i][j] < 1 || g[i][j] > 9)

return false;

else

cnt[g[i][j]]++;

}

for (int j = 1; j <= 9; j++)

{

if (!cnt[j] || cnt[j] > 1)

return false;

}

}

// check column

for (int i = 0; i < 9; i++)

{

int cnt[10] = {0};

for (int j = 0; j < 9; j++)

{

if (g[j][i] < 1 || g[j][i] > 9)

return false;

else

cnt[g[j][i]]++;

}

for (int j = 1; j <= 9; j++)

{

if (!cnt[j] || cnt[j] > 1)

return false;

}

}

// check 3*3

for (int i = 0; i < 9; i += 3)

{

for (int j = 0; j < 9; j += 3)

{

int cnt[10] = {0};

for (int x = i; x < i + 3; x++)

{

for (int y = j; y < j + 3; y++)

{

if (g[x][y] < 1 || g[x][y] > 9)

return false;

else

cnt[g[x][y]]++;

}

}

for (int k = 1; k <= 9; k++)

{

if (!cnt[k] || cnt[k] > 1)

return false;

}

}

}

return true;

}

int main(){

int n;

cin >> n;

while (n--){

for (int i = 0; i < 9; i++){

for (int j = 0; j < 9; j++){

cin >> g[i][j];

}

}

cout << (check() ? 1 : 0);

if (n) cout << endl;

}

return 0;

}

L2-1 鱼与熊掌

#include

#include

#include

#include

using namespace std;

int n, m, k, x, y, q;

vector> g(1e5 + 4, vector());unordered_mapmp;

vectorans;

int main(){

cin >> n >> m;

for (int i = 1; i <= n; i++){

cin >> k;

for (int j = 1; j <= k; j++){

cin >> x;

g[x].push_back(i);

}

}

cin >> q; ans.resize(q);

for (k = 0; k < q; k++){

cin >> x >> y; mp.clear();

for(auto &it: g[x]) mp[it] = 1;

for(auto &it: g[y]) ans[k] += mp[it];

}

for (int i = 0; i < q; i++){

cout << ans[i];

if (i != q - 1) cout << endl;

}

return 0;

}

L2-2 懂蛇语

#include

#include

#include

#include

#include

#include

using namespace std;

int n, m;

unordered_map> mp;

string res, tmp, s, sy;

string getSY(string s) {

stringstream ss(s); res = "";

while (ss >> tmp) res += tmp[0];

return res;

}

int main() {

cin >> n; getchar();

for (int i = 0; i < n; i++){

getline(cin, s);

mp[getSY(s)].push_back(s);

}

cin >> m; getchar();

while (m--){

getline(cin, s); sy = getSY(s);

if (mp.count(sy)){

sort(mp[sy].begin(), mp[sy].end());

for (int i = 0; i < mp[sy].size(); i++){

if (!i) cout << mp[sy][i];

else cout << "|" << mp[sy][i];

}

}else cout << s;

if (m) cout << endl;

}

return 0;

}

L2-3 满树的遍历

#include

#include

#include

using namespace std;

int n, p, root = 0, degree, flag = 1;

vector> tree(1e5 + 5, vector());vectorres;

void dfs(int root, vector&res){

res.push_back(root);

if (!tree[root].size()) return;

degree = max(degree, (int)tree[root].size());

for (int i = 0; i < tree[root].size(); i++) dfs(tree[root][i], res);

}

int main(){

cin >> n;

for (int i = 1; i <= n; i++){

cin >> p;

if (!p) root = i;

else tree[p].push_back(i);

}

dfs(root, res);

for(auto it: tree){

if((int)it.size() != 0 && (int)it.size() != degree) {flag = 0; break;}

}

cout << degree << " " << (flag ? "yes" : "no") << endl;

for (int i = 0; i < res.size(); i++){

if (!i) cout << res[i];

else cout << " " << res[i];

}

return 0;

}

L2-4吉利矩阵

#include

#include

using namespace std;

int cnt, l, n;

vectorsumx(10, 0), sumy(10, 0);

void dfs(int x,int y){

if(x == n+1){ cnt++; return; }

for(int i=0; i<=min(l-sumx[x] , l-sumy[y]); i++){

sumx[x] += i; sumy[y] += i;

if(x < n && y < n) dfs(x, y + 1);

else if(x <= n && y == n && sumx[x] == l) dfs(x + 1, 1);

else if(x == n && y < n && sumy[y] == l) dfs(x, y + 1);

sumx[x] -= i; sumy[y] -= i;

}

}

int main(){

cin >> l >> n;

dfs(1,1);

cout << cnt;

return 0;

}

L3-1 夺宝大赛

#include

#include

#include

#include

#include

using namespace std;

int g[105][105];

int vis[105][105];

int dis[105][105];

vector> res;queue> q;unordered_mapcnt;

int m, n, cx, cy, k;

bool cmp(const pair&a, const pair&b)

{

return a.second == b.second ? a.first < b.first : a.second < b.second;

}

void cost(int x, int y)

{

q.push({x, y});

vis[x][y] = 1;

int size = 0;

int d = 0;

while (!q.empty())

{

size = q.size();

while (size--)

{

paircur = q.front();

q.pop();

int x = cur.first;

int y = cur.second;

dis[x][y] = d;

if (x - 1 >= 1 && g[x - 1][y] == 1 && !vis[x - 1][y])

{

q.push({x - 1, y});

vis[x - 1][y] = 1;

}

if (x + 1 <= m && g[x + 1][y] == 1 && !vis[x + 1][y])

{

q.push({x + 1, y});

vis[x + 1][y] = 1;

}

if (y - 1 >= 1 && g[x][y - 1] == 1 && !vis[x][y - 1])

{

q.push({x, y - 1});

vis[x][y - 1] = 1;

}

if (y + 1 <= n && g[x][y + 1] == 1 && !vis[x][y + 1])

{

q.push({x, y + 1});

vis[x][y + 1] = 1;

}

}

d++;

}

for (int i = 1; i <= m; i++)

{

for (int j = 1; j <= n; j++)

{

if (g[i][j] == 1 && !vis[i][j])

dis[i][j] = -1;

}

}

}

int main()

{

cin >> m >> n;

for (int i = 1; i <= m; i++){

for (int j = 1; j <= n; j++){

cin >> g[i][j];

if (g[i][j] == 2){

cx = i;

cy = j;

}

}

}

cin >> k; res.resize(k);

cost(cx, cy);

for (int i = 0; i < k; i++){

int x, y;

cin >> x >> y;

res[i] = {i, dis[y][x]};

}

sort(res.begin(), res.end(), cmp);

pairans = {-1, -1};

for (int i = 0; i < k; i++) cnt[res[i].second]++;

for (int i = 0; i < k; i++){

if (res[i].second != -1 && cnt[res[i].second] == 1){

ans = res[i];

break;

}

}

if (ans.second == -1) cout << "No winner.";

else cout << ans.first + 1 << " " << ans.second;

return 0;

}

L3-2 工业园区建设

这题不会,直接暴力拿了21分。

#include

#include

#include

using namespace std; 拼多多帮助力入口

int t, n, m, k, x, y, l;

string a;

vectorres;

int main(){

cin >> t;

while (t--){

cin >> n >> m >> k;

res.resize(n); res.clear();

cin >> a;

for (int i = 0; i < n; i++){

x = m, y = k, l = 1;

if (a[i] == '1') y --;

else if (x) { x--; y--; }

while (y){

if (i + l < n){

if (a[i + l] == '1'){ y--; res[i] += l; }

else if (x){ x--; y--; res[i] += l; }

}

if (!y) break;

if (i - l >= 0){

if (a[i - l] == '1'){ y--; res[i] += l; }

else if (x){ x--;y--;res[i] += l; }

}

l++;

}

}

for (int i = 0; i < n; i++){

if (!i) cout << res[i];

else cout << " " << res[i];

}

cout << endl;

}

return 0;

}

参考文章

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧