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;
}
查看全文
2
0
0
23
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;
}
查看全文
2
0
0
16
分巧克力
```cpp
// 请把代码粘贴在这里
```
include
using namespace std;
int n,k;
int maxn;
int H[100010];
int W[100010];
int l,r,mid;
int sum,ans;
/猜想:二分难道是从11到以小明所拥有的最大巧克力的较小边为边长的正方形,
以1为左端点,以上述正方形的边长为右端点?取mid值,判断是否能切出等大的K块正方形巧克力,
如果可以,则将左指针向右移一位,再取mid,如果不可以,右指针向左移动一位,取mid,
要设置变量ans存储mid值,直到左指针>右指针,最后一次二分ans保留的值,即为最大边长/
/
问题:可最大边一定不行,除非是正方形,并且这n块巧克力必须全这么大,不剪枝吗,等等,难道是担心这两条边之间的边长可能符合要求?
回答:在二分查找里,右边界表示的是:搜索范围的上限,而不是答案可能的最大值
/
bool check(int x) {
//注意:sum要清0
sum=0;
for(int i=0;i
=k) {
return true;
}else{
return false;
}
}
int main() {
cin>>n>>k;
for(int i=0;i
>H[i]>>W[i];
maxn=max(H[i],maxn);
maxn=max(W[i],maxn);
//不断更新最大边,存入maxn
}
l=1;
r=maxn;
while(l<=r) {
mid=(l+r)/2;
if(check(mid)) {
ans=mid;
l=mid+1;
}else {
r=mid-1;
}
}
cout<<ans<<endl;
return 0;
}
查看全文
2
0
0
5
奇怪的电梯
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;
}
查看全文
2
0
0
13
用map和set的做法
```cpp
// 请把代码粘贴在这里
#include<bits/stdc++.h>
#include<map>
#include<string>
using namespace std;
int main()
{
map<char,int>char_t;
set<char>ss;//去重同次数字母,且自动排序
string line;
int temp=0;
cin>>line;
for(auto &c:line)
{
char_t[c]++;
}
for(auto &p:char_t)
{
temp=max(temp,p.second);
}
for(auto &pi:char_t)
{
if(pi.second==temp)
{
ss.insert(pi.first);
}
}
for(auto &s:ss)
{
cout<<s;
}
}
```
查看全文
1
0
0
14
为何逻辑相似,只是换用了数据输入方式,就能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;
}
```
查看全文
2
0
0
25
关于讨论内AI助手使用教程
目前讨论区已正式上线 AI 助手 露米。
露米会不定期参与大家的讨论,回复帖子,也会主动发起话题和大家互动。如果你希望露米提供帮助,只需在帖子中直接 @露米,她就会第一时间回应。
后续我们还将陆续加入更多 AI 助手,带来不同风格与能力。届时,讨论区将更加丰富、有趣,也更加热闹。
欢迎大家多多互动 ✨
查看全文
5
0
1
62



