尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

牛客刷题-Day18

牛客刷题-Day18
📅 发布时间:2026/6/18 0:28:56
模拟、枚举与贪心 https://ac.nowcoder.com/acm/contest/20960?from=acdiscuss

牛客刷题-Day18

今日刷题:\(1001-1010\)

1001 模拟 · 例1-字符串展开

d71249fba6e004fe1cbcfc4eb1fb6d15

解题思路

模拟,注意细节就可以。

C++ 代码

#include <bits/stdc++.h>
#include <sstream>
using namespace std;
const int N = 20, M = 110;
typedef long long LL;int p1, p2, p3;
string str;int toLower(char c) {if (c >= 'A' && c <= 'Z')return c - 'A' + 'a';return c;
}int toUpper(char c) {if (c >= 'a' && c <= 'z')return c - 'a' + 'A';return c;
}bool isDigit(char c) {return c >= '0' && c <= '9';
}bool isUpper(char c) {return c >= 'A' && c <= 'Z';
}bool isLower(char c) {return c >= 'a' && c <= 'z';
}int main() {cin >> p1 >> p2 >> p3 >> str;int i = 0;while (i < str.size()) {if (str[i] != '-') { // 字母直接输出cout << str[i++];} else {if (str[i + 1] - str[i - 1] == 1) { // 后继忽略 -i++;continue;}if (!(isUpper(str[i - 1]) && isUpper(str[i + 1]) || isLower(str[i - 1]) && isLower(str[i + 1]) || isDigit(str[i - 1]) && isDigit(str[i + 1]))) { // 两侧不同cout << str[i++];continue;}if (str[i - 1] - str[i + 1] >= 0) { // 非递增直接输出cout << str[i++];continue;}if (str[i + 1] - str[i - 1] > 0) { // 递增if (isDigit(str[i - 1])) { // 数字if (p1 != 3) { // 填充数字if (p3 == 1) {for (char c = str[i - 1] + 1; c < str[i + 1]; c++)for (int i = 0; i < p2; i++)cout << c;} else {for (char c = str[i + 1] - 1; c > str[i - 1]; c--)for (int i = 0; i < p2; i++)cout << c;}} else { // 填充 *for (char c = str[i - 1] + 1; c < str[i + 1]; c++)for (int i = 0; i < p2; i++)cout << '*';}i++;continue;}if (p1 == 1) { // 填充小写字母if (p3 == 1) {for (char c = toLower(str[i - 1] + 1); c < toLower(str[i + 1]); c++)for (int i = 0; i < p2; i++)cout << c;} else {for (char c = toLower(str[i + 1] - 1); c > toLower(str[i - 1]); c--)for (int i = 0; i < p2; i++)cout << c;}} else if (p1 == 2) { // 填充大写字母if (p3 == 1) {for (char c = toUpper(str[i - 1] + 1); c < toUpper(str[i + 1]); c++)for (int i = 0; i < p2; i++)cout << c;} else {for (char c = toUpper(str[i + 1] - 1); c > toUpper(str[i - 1]); c--)for (int i = 0; i < p2; i++)cout << c;}} else { // 填充 *for (char c = str[i - 1] + 1; c < str[i + 1]; c++)for (int i = 0; i < p2; i++)cout << '*';}i++;}}}return 0;
} 

1002 模拟 · 例2-多项式输出

10886dee-d31f-43dd-81bb-0d311ad4fb3a

解题思路

模拟,要考虑各种特殊情况。

C++ 代码

#include <bits/stdc++.h>
#include <sstream>
using namespace std;
typedef long long LL;
const int N = 110, M = 30;int n;
int a[N];
string item[N];int main() {cin >> n;for (int i = 0; i <= n; i++)cin >> a[i];for (int i = 0; i <= n; i++) {if (a[i] == 0) {item[i] = "";} else {if (n - i == 0) {item[i] = "" + to_string(a[i]);} else if (n - i == 1) {if (abs(a[i]) != 1) {item[i] = "" + to_string(a[i]) + "x";} else {if (a[i] == -1) {item[i] = "-x";} else {item[i] = "x";}}} else {if (abs(a[i]) != 1) {item[i] = "" + to_string(a[i]) + "x^" + to_string(n - i);} else {if (a[i] == -1) {item[i] = "-x^" + to_string(n - i);} else {item[i] = "x^" + to_string(n - i);}}}}}string res = "";for (int i = 0; i <= n; i++) {if (res.size() == 0) {res = res + item[i];} else {if (item[i].size() > 0 && item[i][0] != '-') {res = res + "+" + item[i];} else {res = res + item[i];}}}if (res.size() == 0)res = "0";cout << res << endl;return 0;
} 

1005 枚举 · 例2-最大正方形

2591ab77-e958-4c8c-8fe4-e59c2f898581

解题思路

枚举,枚举相邻的两个点,然后根据边长度相等和平行垂直的性质,计算余下两个点。

C++ 代码

#include <bits/stdc++.h>
using namespace std;
const int N = 110;int n;
char g[N][N];
int x[4], y[4];int main() {scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%s", g[i] + 1);int len = 0;for (int x1 = 1; x1 <= n; x1++)for (int y1 = 1; y1 <= n; y1++)if (g[x1][y1] == '#')for (int x2 = 1; x2 <= n; x2++)for (int y2 = 1; y2 <= n; y2++)if (g[x2][y2] == '#') {int x3 = (x1 + x2 + y2 - y1) / 2, y3 = (y1 + y2 + x1 - x2) / 2;int x4 = (x1 + x2 + y1 - y2) / 2, y4 = (y1 + y2 + x2 - x1) / 2;if (x3 >= 1 && x3 <= n && y3 >= 1 && y3 <= n && x4 >= 1 && x4 <= n && y4 >= 1 && y4 <= n) {if (g[x3][y3] == '#' && g[x4][y4] == '#') {int t = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);if (t > len) {len = t;x[0] = x1, x[1] = x2, x[2] = x3, x[3] = x4;y[0] = y1, y[1] = y2, y[2] = y3, y[3] = y4;}}}}for (int i = 0; i < 4; i++)printf("%d %d\n", x[i], y[i]);return 0;
}

1010 枚举 · 例8扩展-校门外的树:hard

427eb698-2c52-4438-af90-284e5ad49793

解题思路

区间合并。

C++ 代码

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef pair<int, int> PII;int n, len;
vector<PII> a, c; void merge(vector<PII> a, vector<PII> &c) {int st = -1, ed = -1;for (int i = 0; i < a.size(); i++) {if (ed < a[i].first) {if (st != -1)c.push_back(make_pair(st, ed));st = a[i].first, ed = a[i].second;} else {ed = max(ed, a[i].second);}}if (st != -1)c.push_back(make_pair(st, ed));
}int main() {scanf("%d%d", &len, &n);for (int i = 1; i <= n; i++) {int l, r;scanf("%d%d", &l, &r);a.push_back(make_pair(l, r));}sort(a.begin(), a.end());merge(a, c);int cnt = 0;for (int i = 0; i < c.size(); i++) {// printf("%d %d\n", c[i].first, c[i].second);cnt += (c[i].second - c[i].first + 1);}printf("%d\n", len + 1 - cnt);return 0;
}

本文来自博客园,作者:Cocoicobird,转载请注明原文链接:https://www.cnblogs.com/Cocoicobird/p/19175620

相关新闻

  • 使用 Java 解析验证码:结合 Tesseract OCR 进行文本识别
  • 代码大全2阅读笔记(2)
  • 三元组 - MKT

最新新闻

  • 如何配置stock-scanner数据源:AkShare数据获取与优化终极指南
  • 同一人公证书在国内可以办理吗?同一人公证书在国内怎么操作?解析身份 - 指上通
  • Exchange-AD-Privesc修复脚本详解:如何快速检测和修复Exchange部署中的Active Directory安全漏洞
  • 应用层核心(一):从FTP到DNS的进阶指南
  • 毕节黄金回收指南:六家靠谱店铺推荐,让闲置安心变现 - 清奢黄金上门回收
  • AI炒股不是预测股价,而是校准认知:信息保真度实战指南

日新闻

  • 2026年不锈钢卷板厂家推荐排行榜:冷轧热轧/304/201不锈钢卷板,高颜值耐腐蚀源头厂家实力精选 - 企业推荐官【官方】
  • FLUX.1-dev FP8模型实战指南:24GB以下显卡高效部署方案
  • 2026佛山长途搬家价目表:跨省跨市搬家费用完整计算指南 - 从来都是英雄出少年

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号