请教一个输入问题

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

相关推荐

05-12 10:10
已编辑
门头沟学院 人工智能
写这篇之前我犹豫了挺久。一方面是怕被人骂,&quot;又一个收割焦虑的转行帖&quot;;另一方面是看了太多用&nbsp;GPT&nbsp;套娃出来的「学习路线」文章,AI&nbsp;味重得让人没法读完。所以这篇全是亲身踩过的坑,时间线、用过的项目、当时的心路全都尽量原样写出来。如果你是大学生在迷茫要不要转&nbsp;AI,或者已经在转的路上,希望能给点参考。&nbsp;一个反共识的开场:你以为进&nbsp;OpenAI&nbsp;的人都是博士?&nbsp;先讲个故事,跟我没关系,但跟所有想转&nbsp;AI&nbsp;的人都有关系。&nbsp;OpenAI&nbsp;的&nbsp;Sora&nbsp;团队(就是搞文生视频那个)一共&nbsp;13&nbsp;个人。这里面有两个人特别有意思:&nbsp;Will&nbsp;DePue,密歇根大学计算机系,直接辍学了。17...
_hengheng:我也本,也算是做ai相关,我最开始感觉做ai工程师有多么多么困难,后来发现懂了原理后整体训练完全可以看成一个流程化的内容,开源方案太多了,大多基本都是按着模子在自家业务上做各种操作,就算是大厂的小部门也没那么多资源去训基模,反而更多的是像怎么把技术往业务方向靠近了,不过当前时代如果本科学历没那么好加上自己执行力不是特别强还真不建议走ai工程师这条路,可以试试其他ai的偏业务方向,不然校招不太好杀出来
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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