Dashcoding 讨论区
Dashcoding 讨论区简介
字符串编号——题解
include
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s,t;//定义string类型的变量s和t,s用来输入数字序列,t用来存放后续提取的子串
int sd;//sd用来将提取的数字子串转换为数字,对应上26个字母的下标
cin>>s;
char zm[27]={'0','A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
//创建字母数组,注意A从下标为1的地方开始
for(int i=0;i
=1&&sd<=26)//判断如果数字在1-26之间,那我们可以直接输出这个数字下标对应的字母
{
cout<<zm[sd];
i++;//完成之后i要加1,因为进入这个if条件意味着我们提取的是两位数字字符
//之后for循环里面也还要加1,下一个子串从已经提取过后的剩下的子串的第一位开始提取
}
else//如果提取的两位字符转为数字后超出了字母的下标范围
{
t=s.substr(i,1);//这时我们只提取一位数字字符放入t当中
sd=stoi(t);//将其转换为数字
cout<<zm[sd];//输出其对应下标的字母
}
}
return 0;
}
查看全文
1
0
0
2
vector排序——题解
include
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,c,t;
cin>>n;
vector
a[n+1];//创建了n+1个vector对象,也就是一维数组a的每个元素都是vector
for(int i=1;i
>c;//输入数组的大小
while(c--)//利用while循环c次,将对应的数组元素推入每一个数组
{
cin>>t;
a[i].push_back(t);
}
sort(a[i].begin(),a[i].end());//每完成一次就先将当前数组排序
}
sort(a+1,a+n+1);//按字典序对n个数组进行排序,因为数组一旦创建,下标就是从0开始的
//而我们在存数据的时候是从1开始的,所以要使用a+1,至于a+n+1,因为C++的 sort(first, last) 排序范围是 [first, last),
//包含first,不包含last,所以sort的第二个参数要指向最后一个元素的下一个位置,是 a+n+1
for(int i=1;i<=n;i++)
{
for(int j=0;j<a[i].size();j++)
{
cout<<a[i][j]<<" ";//输出数组元素
//i代表是第i组数组,j代表是第i组数组的第j个元素
}
cout<<endl;
}
return 0;
}
查看全文
1
0
0
2
奇怪的电梯
include
include
include
include
using namespace std;
int N, A, B;
vector
K;
vector
visited;
int min_steps = INT_MAX; // 记录全局最优解
void dfs(int current_floor, int steps) {
// 1. 剪枝:如果当前步数已经超过已知最优解,没必要继续
if (steps >= min_steps) {
return;
}
// 2. 结束条件:到达目标
if (current_floor == B) {
min_steps = min(min_steps, steps);
return;
}
// 3. 获取当前楼层的“魔力数字”
int step_size = K[current_floor];
// 如果当前楼层数字为0,无法移动,死胡同
if (step_size == 0) return;
// --- 尝试上升 ---
int next_up = current_floor + step_size;
if (next_up
= 1 && !visited[next_down]) {
visited[next_down] = true; // 标记
dfs(next_down, steps + 1);
visited[next_down] = false; // 回溯
}
}
int main() {
// 输入
cin >> N >> A >> B;
K.resize(N + 1); // 1-based indexing
for (int i = 1; i
> K[i];
}
// 初始化
visited.resize(N + 1, false);
visited[A] = true; // 标记起点已访问
// 开始搜索
dfs(A, 0);
// 输出
if (min_steps == INT_MAX) {
cout << -1 << endl;
} else {
cout << min_steps << endl;
}
return 0;
}
查看全文
1
0
0
9
为何逻辑相似,只是换用了数据输入方式,就能AC?
下面这是我让AI写的代码,后面接着我的代码,看着除了输入方式不一样之外,好像就没什么差别了,想不明白为什么我自己的代码AC不了,AI的却可以。。。。求大佬解答,感谢
```cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Student {
string name;
int id;
string className;
};
// 比较函数
bool compare(const Student& lhs, const Student& rhs) {
if (lhs.className != rhs.className) {
return lhs.className < rhs.className;
}
return lhs.id < rhs.id;
}
int main() {
// 优化 I/O 速度
ios::sync_with_stdio(false);
cin.tie(0);
int n;
// 尝试处理多组输入,防止 WA
while (cin >> n) {
vector<Student> students(n);
for (int i = 0; i < n; ++i) {
cin >> students[i].name >> students[i].id >> students[i].className;
}
sort(students.begin(), students.end(), compare);
for (const auto& s : students) {
cout << s.name << " " << s.id << " " << s.className << endl;
}
}
return 0;
}
```
```cpp
#include <cstdio>
#include "string"
#include "vector"
#include "algorithm"
using namespace std;
struct Student{
string name;
int id;
string className;
Student(char *_name, int _id, char *_className){
name = _name;
id = _id;
className = _className;
}
};
bool compare(Student lhs, Student rhs){
if(lhs.className < rhs.className) return true;
else if(lhs.className == rhs.className && lhs.id < rhs.id) return true;
else return false;
}
int main() {
vector<Student> students;
int n, id;
char buffer_name[50], buffer_className[50];
while (scanf("%d", &n) != EOF){
students.clear();
while (n --){
scanf("%s %d %s", buffer_name, &id, buffer_className);
students.emplace_back(buffer_name, id, buffer_className);
}
sort(students.begin(), students.end(), compare);
for(auto each: students){
printf("%s %d %s\n", each.name.c_str(), each.id, each.className.c_str());
}
}
return 0;
}
```
查看全文
1
0
0
13
关于讨论内AI助手使用教程
目前讨论区已正式上线 AI 助手 露米。
露米会不定期参与大家的讨论,回复帖子,也会主动发起话题和大家互动。如果你希望露米提供帮助,只需在帖子中直接 @露米,她就会第一时间回应。
后续我们还将陆续加入更多 AI 助手,带来不同风格与能力。届时,讨论区将更加丰富、有趣,也更加热闹。
欢迎大家多多互动 ✨
查看全文
5
0
1
54
题解
```cpp
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
string s;
if (!(cin >> s)) return 0;
int ans = 0;
int lzone = 0, mzone = 0, szone = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'L') lzone++;
else if (s[i] == 'M') mzone++;
else szone++;
}
int lstart = 0, lend = lzone - 1;
int mstart = lzone, mend = lzone + mzone - 1;
int sstart = lzone + mzone, send = (int)s.length() - 1;
int minl = 0, sinl = 0, linm = 0, sinm = 0, lins = 0, mins = 0;
for (int i = lstart; i <= lend; i++) {
if (s[i] == 'M') minl++;
else if (s[i] == 'S') sinl++;
}
for (int i = mstart; i <= mend; i++) {
if (s[i] == 'L') linm++;
else if (s[i] == 'S') sinm++;
}
for (int i = sstart; i <= send; i++) {
if (s[i] == 'M') mins++;
else if (s[i] == 'L') lins++;
}
ans += min(lins, sinl) + min(linm, minl) + min(sinm, mins);
// 每三本形成一个环的书需要两次交换
ans += (abs(lins - sinl) + abs(linm - minl) + abs(sinm - mins)) / 3 * 2;
cout << ans;
return 0;
}
```
本质上就是先按L,M,S分块,本来在对应块里的不用动,优先换互相占了对方位置的书,这样换一次就能使两本书到对应位置,剩下的书每三本成环,每个环要交换两次
查看全文
1
0
0
22



