请教一个输入问题

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

相关推荐

2025-12-02 02:15
门头沟学院
最近菊厂陆续开了,极力劝退那些拿13级的985硕士,就13级那么点儿薪资,一线城市每个月到手1.8/7/6w,租房2k还是破烂,吃饭2k还是预制菜,买个1k衣服都是聚酯纤维破塑料,稍微出去浪一浪,能留1w就是万岁,要是再有个啥都想买的对象,一线工作一年难存10w。隔壁工地混泥土,钳工,焊工一天800+,还包吃包住。读书18年到985硕士出来就为了进厂螺丝工?还不如从8岁童工开始干活,别人读书完了你工龄18+,混不上领导也是个小头头了。当然专科进来正式工,od都行,一般本科进来13级也OK,毕竟22岁年纪摆在那个地方还不需要太花钱。读硕博的基本26岁,工作两年就要结婚的,兜里没几个崽,连彩礼都要信用贷。菊厂离职的不少,毕竟正常没人受得了9116(梗:再来一次911刷6)。为啥这时候劝?因为刚下班,因为国考刚完,省考下周,就是可惜选调只有当年应届能报。现在回想能拍断大腿。应届生真实好身份,错过这一次,选调,考公,考编,当老师,进医院,研究所,高校,央国企,基本都无缘了,就连报名资格都被剥夺了,可谓是被党和国家遗弃的废材,统称“社会上的”,扔到社会去流浪,被用坏了就扔医院,长期超负载使用,零件修不好基本可以扔火里回炉重造了。体制内奉行找体制内的,都是党和国家选的人才,智力不差,样貌不丑,身材端正,收入稳定,安居房政策福利待遇也OK。因公出行都是报销,周末顺带“游山玩水“,这种体制内单身资源但凡想找对象,去社会上随便吆喝一声都排队。观察一下,基本没什么公务员在相亲,因为早就被邻里邻居抢光了。
哈哈哈,你是老六:就这不去的人大把人干呢,现在不缺人干活,你不干大把干呢,还有那个说农民工赚钱的,那个800+我估计肯定也就那一段时间,哪有这么赚钱,还是一句话,要想存下钱必须花销极低,能省的就不花钱,工资要高点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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