请教一个输入问题

拜倒在数据输入上面已久
请问像1,3 2,5 8,10 100,541这样不定长的数据,应该怎么输入呢,C++
#C++工程师#
全部评论
struct Interval {     int start;     int end;     Interval() : start(0), end(0) {}     Interval(int s, int e) : start(s), end(e) {} }; int main() {     vector <Interval> vec;     Interval i;     char c=' ';     while (c!='\n')     {         scanf("%d,%d%c",&i.start,&i.end,&c);         vec.push_back(i);     }     Solution sol;     vec = sol.merge(vec);     for (vector<Interval>::iterator i = vec.begin(); i != vec.end(); i++)         cout << i->start << ','<<i->end<<' ';     cout<<endl;     system("pause");     return 0; }
点赞 回复 分享
发布于 2017-09-19 08:40
do {cin >> num;}while(cin.getchar() != '\n')
点赞 回复 分享
发布于 2017-09-19 00:18
do {}
点赞 回复 分享
发布于 2017-09-19 00:16
感谢各位大佬,发现7L@西方玄学,神经网络~ 的方法可以使用
点赞 回复 分享
发布于 2017-09-18 21:14
用的笨办法。读进字符串然后解析出来,A了。 #include <vector> #include <iostream> #include <string> #include <sstream> #include <map> using namespace std;  struct Person {     int vote;      int count;  }; void fill(vector<int>& v, string s) {     string sub;     for (int i = 0; i < s.size(); )     {         int j = i;          if (s[i] >= '0' && s[i] <= '9')         {             while (s[j] >= '0' && s[j] <= '9') j++;             sub = s.substr(i, j - i);              stringstream str(sub);             int num;             str >> num;             v.push_back(num);         }         i = j + 1;      } } int main() {     string vstr;      string wstr;     getline(cin, vstr);     getline(cin, wstr);     vector<int> v;     fill(v, vstr);     vector<int> w;     fill(w, wstr);     map<int, Person> votes;      for (int i = 0; i < v.size(); i++)     {         votes[v[i]].count++;          votes[v[i]].vote += w[i];      }     map<int, Person>::iterator person = votes.begin();     for(map<int, Person>::iterator it = votes.begin(); it != votes.end(); it++)     {         if (person->second.vote < it->second.vote)             person = it;          if (person->second.vote == it->second.vote && person->second.count < it->second.count)             person = it;      }     cout << person->first << endl; }
点赞 回复 分享
发布于 2017-09-18 21:07
第一种(用c++): while(cin>>first) {     char a;cin>>a;     cin>>end } 第二种用C: while (1) {         scanf("%d", &num1);         char c = getchar();  //过滤逗号         scanf("%d", &num2);         c = getchar();   //过滤空格         if (c == '\n'){             break;         }     }
点赞 回复 分享
发布于 2017-09-18 21:01
你是说迅雷的笔试题吗? #include <bits/stdc++.h> using namespace std; struct Bound{ unsigned int x; unsigned int y; Bound(unsigned int _x=0,unsigned int _y=0) :x(_x),y(_y){} }; int main() { list<Bound> lst; unsigned int x,y; while(scanf("%u,%u",&x,&y)){ lst.emplace_back(x,y); if(getchar()==10) break; } lst.sort([](const Bound& a,const Bound& b){ return a.x<b.x;} ); for(auto it=++lst.begin();it!=lst.end();++it){ auto cur_it = it;//保存当前迭代器 if(cur_it->x <= (--it)->y){ cur_it->x = it->x; lst.erase(it); } it = cur_it; } for(const auto& ele:lst){ printf("%u,%u ",ele.x,ele.y); } return 0; }
点赞 回复 分享
发布于 2017-09-18 21:00
用 vector 存 while(scanf("%d,%d", &x, &y) != EOF) {     ....... }
点赞 回复 分享
发布于 2017-09-18 20:44
不定长数组如果确定输入个数的话可以用关键字new来分配动态内存,注意用完后用delete回收;也可以用vector 如果不确定输入个数的话就直接用vector吧,然后判断‘\n’跳出输入 我平常都是这么做,不知道还有没有更好的方法
点赞 回复 分享
发布于 2017-09-18 20:42
windows控制台ctrl+z表示结束
点赞 回复 分享
发布于 2017-09-18 20:41
我用比较蠢的方法,先全部读进来,然后找空格然后用sscanf.. 突然感觉好像用sstream更简单些..
点赞 回复 分享
发布于 2017-09-18 20:39
这跟定不定长有啥关系
点赞 回复 分享
发布于 2017-09-18 20:39

相关推荐

HR_丸山彩同学:你的项目描述里,系统设计讲了很多:MemCube是什么、三级存储架构怎么设计、四种遗忘策略分别是什么。这些面试的时候讲没问题,但简历上不需要这么细。 简历要突出的是影响力,不是实现细节。面试官看简历的时候想知道的是「这个项目有多大价值」,不是「这个项目具体怎么实现的」。实现细节是面试时候聊的 怎么改:技术细节可以精简为一句「采用三级存储架构+四种遗忘策略」,把省出来的篇幅用来写影响力。比如:项目有没有开源?有没有写成技术博客?有没有被别人使用过? 校园经历没有任何信息量,任何人都可以写这句话,写了等于没写。更关键的是,你投的是技术岗,校园活动经历本来就不是加分项。如果非要写,必须写出具体的数字和成果。如果你没有这些数字,那就老老实实删掉 「端到端耗时缩减30-40%」要给出确切数字和绝对值。从1000ms降到600ms是降了40%,从100ms降到60ms也是降了40%,但这两个含义完全不一样。其他也是,涉及到数据,准备好证据,口径统一,面试会问 「熟练」「熟悉」「了解」混在一起用,读起来很乱。而且「了解前端需求」最好改成「具备前后端协作经验」
点赞 评论 收藏
分享
01-30 22:03
门头沟学院 Java
用微笑面对困难:我滴妈,【俩月】【实习】【主管】仨debuff吃满了,独立设计开发的项目写了绝大占比的运营板块,你独立开发,那维护、问题复盘、日志更新、bug、策划书全是自己整的? 不建议写那么大,可以从小出发更容易
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务