这种输入格式有什么简单的代码输入数据到vector数组?

ooolinux 2023-05-12 22:25:03

 

 

...全文
252 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
CSDN-Ada助手 2023-05-12
精选
  • 打赏
  • 举报
回复
您的问题已经帮您同步至问答, 链接: https://ask.csdn.net/questions/7943540, 请您保持关注, 如果回答有帮助解决此问题, 麻烦您动动小手给相关的回答点赞, Ada会在评论区为您更新结题状态
CN_YaKei 2023-05-14
  • 打赏
  • 举报
回复

处理了负数的版本

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    string str;
    cin>>str;
    vector<int> a;
    for(int i=0;i<(int)str.size();) {
        int f=1;
        for(;i<(int)str.size()&&!isdigit(str[i]);++i) {
            if(str[i]=='-') f=-1;
        }
        if(i<(int)str.size()) {
            int x=0;
            for(;isdigit(str[i]);++i) {
                x=x*10+str[i]-'0';
            }
            a.push_back(x*f);
        }
    }
    sort(a.begin(),a.end());
    int mx=0;
    for(int i=1;i<(int)a.size();++i) {
        mx=max(mx,a[i]-a[i-1]);
    }
    cout<<mx<<'\n';
    return 0;
}
ooolinux 2023-05-15
  • 举报
回复
@CN_YaKei 可以
CN_YaKei 2023-05-13
  • 打赏
  • 举报
回复

这样也不难

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    string str;
    cin>>str;
    vector<int> a;
    for(int i=0;i<(int)str.size();) {
        for(;i<(int)str.size()&&!isdigit(str[i]);++i);
        if(i<(int)str.size()) {
            int x=0;
            for(;isdigit(str[i]);++i) {
                x=x*10+str[i]-'0';
            }
            a.push_back(x);
        }
    }
    sort(a.begin(),a.end());
    int mx=0;
    for(int i=1;i<(int)a.size();++i) {
        mx=max(mx,a[i]-a[i-1]);
    }
    cout<<mx<<'\n';
    return 0;
}
  • 举报
回复
@CN_YaKei 没有处理负数
ooolinux 2023-05-14
  • 举报
回复
@CN_YaKei 思路是判断每个字符
  • 打赏
  • 举报
回复

老鸟都爱用C库I/O,而不用iostream那套东西~

#include <cstdio>
#include <algorithm>
#include <vector>

int main()
{
  std::vector<int> v;

  for (int c; (c = getchar()) != ']';)
    c == '[' || c == ',' ? scanf("%d", &c), v.insert(v.end(), c) : v.end();
  sort(v.begin(), v.end());
  for (int i = 1, n = 0; i < v.size(); ++i == v.size() ? printf("%d\n", n) : 0)
    v[i] - v[i - 1] > n ? n = v[i] - v[i - 1] : 0;
  
  return 0;
}
ooolinux 2023-05-13
  • 举报
回复
@日立奔腾浪潮微软松下联想 用getchar结合scanf,好像也没有更简单的方法

13,869

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧