STL最后的三道题太复杂了,放弃之。 这本书定位实在太高。 还是水水后面的小练习吧( 达到最低要求(九道题) 1.
#include<iostream> #include<cstdio> #include<vector> #include<string> #include<map> #include<sstream> #include<set> #include<cmath> #include<algorithm> using namespace std; const int maxn = 1e3; vector<string> ans[maxn]; int count = 0,r = 1 ; int row[180 + 5] = { 0 };//统计每行最长单词长度 void print() { for (int i = 1; i <= r; i++) { for (int g = 1; g <= ans[i].size(); g++) { cout << ans[i][g]; for (int k = 0; k < (row[g]-(int)ans[i][g].size()); k++) cout << " "; } cout << endl; } } int main() { string s,line; int column=1;//列是column,英语苦手 while (getline(cin, line)) { stringstream ss(line); while (ss >> s) { row[column] = max(row[column],(int)s.size()); ans[r].push_back(s); column++; } r++; column = 1; } print(); return 0; }2.emmm,VS不能用了,先学CSAPP去了
#include<iostream> #include<cstdio> #include<vector> #include<string> #include<map> #include<sstream> #include<set> #include<cmath> #include<algorithm> #include<iterator> using namespace std; vector<int> test; int main() { int m,n,tmp; cin >> m; while (m--) { int flag = 1; test.clear(); int n; cin >> n; while (n--) { cin >> tmp; test.push_back(tmp); } int k = 1001; while (k--) { tmp = *test.begin();//保存第一个值 //迭代器遍历;注意是要直接引用,也就是要进行解引用操作 vector<int>::iterator it = test.begin(); for (; it != test.end(); it++) *it = abs(*it - *(it + 1)); *it = abs(*it - tmp); } for (vector<int>::iterator it = test.begin(); it != test.end(); it++) if (!*it) flag = 0; if (!flag) cout << "LOOP"; else cout << "ZERO"; } return 0; }3忘了保存了,不过很水,无所谓 4. 本以为是用map没想到是考sort 而且两个vector是可以直接比较的,学习了。
#include<iostream> #include <string> #include <algorithm> #include <set> #include <sstream> #include<stack> #include<queue> #include<vector> using namespace std; vector<int> a, b; int main() { int n,tmp1,tmp2; while (cin >> n&& n) { if (n % 2) cout << "NO" << endl; else { a.clear(); b.clear(); while (n--) { cin >> tmp1 >> tmp2; a.push_back(tmp1); b.push_back(tmp2); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (a == b) cout << "YES" << endl; else cout << "NO"<<endl; } } }这道题比较好,摘录一下
You are to nd all the two-word compound words in a dictionary. A two-word compound word is aword in the dictionary that is the concatenation of exactly two other words in the dictionary.InputStandard input consists of a number of lowercase words, one per line, in alphabetical order. There willbe no more than 120,000 words.OutputYour output should contain all the compound words, one per line, in alphabetical order.Sample InputaalienbornlesslienneverneverthelessnewnewbornthezebraSample Outputaliennewborn
注意,set自建红黑树,查找很高效
#include<iostream> #include <string> #include <algorithm> #include <set> #include <sstream> #include<stack> #include<queue> #include<vector> using namespace std; set<string> Q; /* * Q.begin(); 返回指向容器最开始位置数据的指针 * Q.end(); 返回指向容器最后一个数据单元+1的指针 * set<string> 不是命名空间名 */ int main() { string tmp,left,right; while (cin >> tmp) Q.insert(tmp); set<string>::iterator loop; for (loop = Q.begin; loop != Q.end(); ++loop) {//第一个不用检验 tmp = *loop; for (int j = 0; j < tmp.size(); ++j) {//0不用拆分 left.assign(tmp, 0, j); if (Q.count(left)) { right.assign(tmp, j, tmp.size() - j); if (Q.count(right))//查找很快!count返回right个数 { cout << tmp << endl; break; } } } } }6.找对称轴 首先可以用图实现,但是空间复杂度太高 然后可以用两个循环实现,刚学了union想用这个做,发现不对,用struct实现吧 还是很简单
#include<iostream> #include <string> #include <algorithm> #include <set> #include <sstream> #include<stack> #include<queue> #include<vector> #define INT_MAX 0x7fffffff #define INT_MIN 0x80000000//用不到,有范围 using namespace std; struct data { int x; int y; }; int main() { struct data a[1000 + 5]; float n,m; cin >> n; while (n--) { cin >> m; int tmp = 0; float max= -1e4-5,min= 1e4+5; while (m--) { cin>>a[tmp].x >> a[tmp].y; if (a[tmp].x > max) max = a[tmp].x; if (a[tmp].x < min) min = a[tmp].x; tmp++; } float cen = (max + min) / 2; int flag; for (int i = 0; i < tmp; i++) { flag = 0; if (a[i].x == cen) flag=1; else for (int j = tmp-1; j >= 0; j--) { if (a[j].x == (2*cen-a[i].x) && a[j].y == a[i].y) flag = 1; } if (!flag)//出现了一个不存在的 { cout << "NO" << endl; break; } } if (flag) { cout << "YES" << endl; } } }7.一看就是优先队列问题 我开始以为 时间就等于队列中比focus大的加上focus之后入队的和它一样大的。 结果发现没那么简单 还是老老实实模拟吧 这个题也不错,摘录
The only printer in the computer science students’union is experiencing an extremely heavy workload.Sometimes there are a hundred jobs in the printerqueue and you may have to wait for hours to get asingle page of output.Because some jobs are more important than others,the Hacker General has invented and implemented asimple priority system for the print job queue. Now,each job is assigned a priority between 1 and 9 (with 9being the highest priority, and 1 being the lowest), andthe printer operates as follows.The rst jobJin queue is taken from the queue.If there is some job in the queue with a higher priority than jobJ, then moveJto the end of thequeue without printing it.Otherwise, print jobJ(and do not put it back in the queue).In this way, all those important muffin recipes that the Hacker General is printing get printed veryquickly. Of course, those annoying term papers that others are printing may have to wait for quitesome time to get printed, but that’s life.Your problem with the new policy is that it has become quite tricky to determine when your printjob will actually be completed. You decide to write a program to gure this out. The program willbe given the current queue (as a list of priorities) as well as the position of your job in the queue, andmust then calculate how long it will take until your job is printed, assuming that no additional jobswill be added to the queue. To simplify matters, we assume that printing a job always takes exactlyone minute, and that adding and removing jobs from the queue is instantaneous.InputOne line with a positive integer: the number of test cases (at most 100). Then for each test case:One line with two integersnandm, wherenis the number of jobs in the queue (1n100)andmis the position of your job (0mn1). The rst position in the queue is number 0,the second is number 1, and so on.One line withnintegers in the range 1 to 9, giving the priorities of the jobs in the queue. The rst integer gives the priority of the rst job, the second integer the priority of the second job,and so on.OutputFor each test case, print one line with a single integer; the number of minutes until your job is completelyprinted, assuming that no additional print jobs will arrive. Sample Input31 054 21 2 3 46 01 1 9 1 1 1 Sample Output125 在网上搜了一下,两个队列的帖子给了我思路:
#include <iostream> #include <queue> #include <vector> using namespace std; struct cmp { bool operator () (const int a, const int b) { return a < b; } }; priority_queue<int, vector<int>, cmp> tot; queue<int> tot0; int main() { int n; cin >> n; vector<int> ans; while (n--) { tot0 = queue<int>(); tot = priority_queue<int, vector<int>, cmp>(); int a, b; cin >> a >> b; int m = 0; for (int i = 0; i < a; ++i) { int t; cin >> t; if (i == b) m = t; tot.push(t); tot0.push(t); } int time = 0; while (tot.top() != tot0.front() || b != 0) { if (tot.top() != tot0.front()) { int c = tot0.front(); tot0.pop(); tot0.push(c); } else { tot.pop(); tot0.pop(); ++time; --a; } if (b == 0)b = a - 1; else --b; } ++time; ans.push_back(time); } for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << endl; } }优先队列和题目中所述行为不符,那就用一个普通队列模拟优先队列同时满足题意。 8. 9.第九题确实难一点,但是幸亏不用迭代,要不这题太复杂可能我就处理不了了
#include<iostream> #include <string> #include <algorithm> #include <set> #include <sstream> #include<stack> #include<queue> #include<map> #include<vector> using namespace std; map<string, int> _array; map<string, bool> M1;//一个string是否被赋过值 map<string, int> M2;//被赋过值的话检查有没有溢出 //提取字符串中的数组名和数组值 string Array_name(string s) { string name; for (int i = 0; i < s.size(); i++) { if (s[i] == '[') break; else name += s[i]; } return name; } string Array_num(string s) { string num; int i; for (i = 0; i < s.size(); i++) { if (s[i] == '[') break; } for (++i; i < s.size(); i++) { if (s[i] == ']') break; else num += s[i]; } return num; } //产生新数组 void make_Array(string s) { string name, num; name = Array_name(s); num = Array_num(s); //映射数组名与数组大小 _array[name]=stoi(num); } //检查是否越界 int if_crossed(string s) { string name, num; name = Array_name(s); num = Array_num(s); if (stoi(num) < 0 || stoi(num) > _array[name]) return 1; } //处理等式 int parse(string s) { string left, right,tmp,left_num; int i = 0,flag=0; //提取左式 for (; i < s.size(); i++) { if (s[i] == '=') { left = tmp; tmp.clear(); break; } else tmp += s[i]; } //检查左边是否越界 if (if_crossed(left)) return 1; //提取右式并且检查右边是否是数组 for (++i; i < s.size(); i++) { right += s[i]; if (s[i] == '[') flag = 1; } //如果是数组的话 if (flag) { string name, num; name = Array_name(right); num = Array_num(right); if (num[1] >= '0' && num[1] <= '9') {//右边数组里面是数字,检查是否越界,是否已赋值 if (if_crossed(right)) return 1; if (M1[num] == false) return 1; else { M1[left] = true; M2[left] = M2[right]; } } else {//右边数组里还是数组,检查是否赋过值,检查是否越界 if (M1[num] == false) return 1; if (M2[num] > _array[name]) return 1; else { M1[left] = true; M2[left] = M2[name+'['+ to_string(M2[num])+']']; } } } //如果不是数组,那么赋值 else { int num = stoi(left);//stoi函数用于转换字符串变成数字 M1[left] = true; M2[left] = num; } } int main() { string tmp; int bug = 0,row=0,flag=0; while (cin >> tmp) { row++; int if_Ass = 0;//是不是赋值语句 if (tmp == ".") { bug = 0; row = 0; flag = 0; } else if(!bug){ stringstream ss(tmp); char s; while (ss >> s) if (s == '=') if_Ass = 1; if (if_Ass) { bug=parse(tmp); if (bug) cout << row<<endl; } else make_Array(tmp); } } return 0; }