请教一个输入问题

拜倒在数据输入上面已久
请问像1,3 2,5 8,10 100,541这样不定长的数据,应该怎么输入呢,C++
#C++工程师#
全部评论
这跟定不定长有啥关系
点赞 回复 分享
发布于 2017-09-18 20:39
我用比较蠢的方法,先全部读进来,然后找空格然后用sscanf.. 突然感觉好像用sstream更简单些..
点赞 回复 分享
发布于 2017-09-18 20:39
windows控制台ctrl+z表示结束
点赞 回复 分享
发布于 2017-09-18 20:41
不定长数组如果确定输入个数的话可以用关键字new来分配动态内存,注意用完后用delete回收;也可以用vector 如果不确定输入个数的话就直接用vector吧,然后判断‘\n’跳出输入 我平常都是这么做,不知道还有没有更好的方法
点赞 回复 分享
发布于 2017-09-18 20:42
用 vector 存 while(scanf("%d,%d", &x, &y) != EOF) {     ....... }
点赞 回复 分享
发布于 2017-09-18 20:44
你是说迅雷的笔试题吗? #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
第一种(用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
用的笨办法。读进字符串然后解析出来,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
感谢各位大佬,发现7L@西方玄学,神经网络~ 的方法可以使用
点赞 回复 分享
发布于 2017-09-18 21:14
do {}
点赞 回复 分享
发布于 2017-09-19 00:16
do {cin >> num;}while(cin.getchar() != '\n')
点赞 回复 分享
发布于 2017-09-19 00:18
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

相关推荐

01-17 08:34
门头沟学院 Java
点赞 评论 收藏
分享
02-05 08:49
已编辑
武汉大学 Java
野猪不是猪🐗:36k和36k之间亦有差距,ms的36k和pdd的36k不是一个概念
点赞 评论 收藏
分享
如题,鼠鼠快碎掉了。鼠鼠正在投暑期和日常的实习,可能是因为简历太差吧,好多初筛都没有过,所以其实格外珍惜每一次的约面。尤其鼠鼠是八股选手,但凡碰到喜欢问项目的面试官是直接速通鼠掉。那是一个万里无云的晚上,鼠鼠接到tx某子公司的约面,虽然没算法题但是问得我汗流浃背,面试官从我的八股批判到我的项目继而批判到我的实习,感觉基本上除了八股这种特定答案之外每一个问题都要质问我,尤其是询问到实习的时候我解释完之后直接来了一句“那你实习也啥也没做啊”,鼠鼠直接原地碎掉。之后的问题鼠鼠也不太记得了,大部分都是直接吟诵咒语,肌肉记忆直接不过脑子。因为接二连三的压力鼠鼠直接摆烂了,回答的时候也不太看屏幕直接开始搓...
机器人为什么是猫呀:楼主要自信。好的面试官是会照顾面试者情绪的,不会直接说那么伤人的话。面试表现其实很看自己的心态跟情绪,这些又和面试官的反馈很相关。而且有些面试官很高傲,不求甚解,自认为你的东西看一眼很简单,就不会听你说了,却没有从一个没有丰富工作经验的人的角度去思考。楼主不要因为这些影响心态,不要怀疑自己,只要遇到一个“合适”的面试官就会好很多的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务