请教一个输入问题

拜倒在数据输入上面已久
请问像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

相关推荐

04-10 18:55
门头沟学院 Java
点赞 评论 收藏
分享
个人背景:学院二本计科专业&nbsp;大二开始实习个人经历:安克创新&nbsp;、理想汽车、字节跳动碎碎念:我做事只有三分钟热度。看到进了大厂的同学,我会羡慕,也会跟着努力上进;但遇到好看的小说,我又会放下手头的事沉迷其中,之前的坚持也就中断了。我有些自卑,总觉得自己学历和外貌都不够好。之前偶然在网上受到关注,我就喜欢上了上网,因为这里有很多人认可我。但我也很在意别人的评价,偶尔看到嘲讽的言论,会触发我的自卑情绪,让我感到愤怒。有时候我会强硬地回怼,有时候又会懦弱地选择无视。我也有虚荣心。不管是拿到安克、理想还是字节的机会,我在分享的时候都会带着这份心思。我会特意强调自己学历不好,是为了衬托出过程的艰难,以此显得自己更厉害。我知道,人往往会炫耀自己缺少的东西,来掩盖内心的空洞。我总想着走捷径,不太喜欢踏踏实实地做事。找实习的时候,我花了更多时间在研究面试技巧上,而不是提升专业能力。我会反复听面试录音分析技巧,看面试教程学习怎么和不同的面试官沟通,还会每天自言自语练习语言表达,同学都觉得我有点奇怪。我的实习生涯里,侥幸和运气占了很大一部分。我总在想,如果有一天我失去了这份幸运,这些特质可能会让我一蹶不振。ps:&nbsp;很多人会问我学习路线和经验&nbsp;但是就像我上面说的&nbsp;我的实习过程靠的很多是关键节点的运气&nbsp;技术上面我可能不如很多人&nbsp;&nbsp;所以请大家理性求助和理性参考我的回答&nbsp;附上我的投递记录
我的offer在哪里...:从去年看到现在,飞升哥就是榜样
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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