[STL] day 1~2 Problem Set
Q#1
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void print(int& t){ //for_each fnc: parameter accept elements T
cout << t << " ";
}
int main() {
vector<int> v;
int num, toPut;
cin >> num;
; i<num; i++) {
cin >> toPut;
v.push_back(toPut);
}
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print); //calling fnc in for_each
;
}
Q#2
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void print(int& i){
cout << i << " ";
}
int main() {
vector<int> v;
int size, num;
cin >> size;
; i<size; i++){
cin >> num;
v.push_back(num);
}
int toErase1, toErase2;
cin >> toErase1;
v.erase(v.begin()+toErase1-);
cin >> toErase1 >>toErase2;
toErase1--; toErase2--;
v.erase(v.begin()+toErase1, v.begin()+toErase2); //last one exclusive
cout << v.size() <<endl;
for_each(v.begin(), v.end(), print);
;
}
Q#3: time-out exception
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
vector<int> v;
int size, num, time, target;
cin >> size;
; i<size; i++){
cin >> num;
v.push_back(num);
}
cin >> time;
; t<time; t++){ //start testing each case
cin >> target;
;
for(; i<v.size(); i++){
if(v[i]!=target) continue;
<<endl; break;
}
if(i == v.size()){
; j<v.size(); j++){
<<endl; break;} //should not forget the {} in one-line code
}
}
}
}
Q#4
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main() {
set<int> s;
int num, enquiryType, item;
cin >> num;
while(num){
cin>>enquiryType;
cin >> item;
switch(enquiryType){
: s.insert(item); break;
: s.erase(item); break;
: {
set<int>::const_iterator p = s.find(item);
if(p!=s.end()) cout << "Yes" <<endl; //cannot use if(p!=NULL); Cz even p == v.end(), v.end()!=NULL;
else cout << "No" <<endl;
break;
}
}
num--;
}
}
Q#5
#include <cstdio>
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
int main() {
map<string, int> students;
int num, marks, choice;
string name;
cin >> num;
while(num){
cin >> choice >> name;
switch(choice){
: {
cin >> marks;
map<string, int>::iterator p = students.find(name); //stl algorithm needed be called by a STL CONTAINER! NOT STANDALONE!
if(p!=students.end()){ //rmb: after 'find()' algorithm, found if p!=container.end();
&& marks!= && marks!=) students[name] += marks; //container name with key represents the value it contains!
break;
}
&& marks!= && marks!=) students.insert(pair<string, int>(name, marks));
));
break;
}
: {
students.erase(name); break;
}
:{
cout << students[name] <<endl;
}
}
num--;
}
}
[STL] day 1~2 Problem Set的更多相关文章
- STL UVA 11991 Easy Problem from Rujia Liu?
题目传送门 题意:训练指南P187 分析:用vector存id下标,可以用map,也可以离散化用数组存(发现不用离散化也可以) map #include <bits/stdc++.h> u ...
- STL之父Stepanov谈泛型编程的发展史
这是一篇Dr. Dobb's Journal对STL之父stepanov的采访.文中数次提到STL的基本思想.语言的特性.编程的一些根本问题等,非常精彩.这篇文章让我想去拜读下stepanov的大作& ...
- codevs http://www.codevs.cn/problem/?problemset_id=1 循环、递归、stl复习题
12.10高一练习题 1.要求: 这周回顾复习的内容是循环.递归.stl. 不要因为题目简单就放弃不做,现在就是练习基础. 2.练习题: (1)循环 题目解析与代码见随笔分类 NOI题库 htt ...
- UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
- [UVA] 11991 - Easy Problem from Rujia Liu? [STL应用]
11991 - Easy Problem from Rujia Liu? Time limit: 1.000 seconds Problem E Easy Problem from Rujia Liu ...
- Problem I: STL——多重集的插入和删除
Problem I: STL--多重集的插入和删除 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1729 Solved: 1258[Submit][ ...
- HDU - 1022 Train Problem I STL 压栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1022 Train Problem I(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- uva 11991 - Easy Problem from Rujia Liu?(STL)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142" target="_blank ...
随机推荐
- php中查询mysql如何在IN array中用
假如有一个数组 $arr = array(1,3,5,7,9)那么我在如何在php中使用mysqlWHERE id IN (1,3,5,7,9.......)$arr_string = join(', ...
- 用window的onload事件,窗体加载完毕的时候
<script type="text/javascript"> //用window的onload事件,窗体加载完毕的时候 window.onload=function( ...
- 一张图搞定Java设计模式——工厂模式! 就问你要不要学!
小编今天分享的内容是Java设计模式之工厂模式. 收藏之前,务必点个赞,这对小编能否在头条继续给大家分享Java的知识很重要,谢谢!文末有投票,你想了解Java的哪一部分内容,请反馈给我. 获取学习资 ...
- 矩阵的f范数及其求偏导法则
转载自: http://blog.csdn.net/txwh0820/article/details/46392293 矩阵的迹求导法则 1. 复杂矩阵问题求导方法:可以从小到大,从scalar到 ...
- [Android FrameWork 6.0源码学习] Window窗口类分析
了解这一章节,需要先了解LayoutInflater这个工具类,我以前分析过:http://www.cnblogs.com/kezhuang/p/6978783.html Window是Activit ...
- Java 容器在实际项目开发中应用
前言:在java开发中我们离不开集合数组等,在java中有个专有名词:"容器" ,下面会结合Thinking in Java的知识和实际开发中业务场景讲述一下容器在Web项目中的用 ...
- lvs讲解
作者写的就是好呀,简单明了,安逸的我也不知道早点看看,非得等到找工作了,在这里临时抱佛脚,以后再过来添加我自己的总结 http://www.linuxvirtualserver.org/zh/
- Spring Security4实例(Java config版)——ajax登录,自定义验证
本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...
- Android之圆点导航的两个案例(ViewPager)
案例一效果: 布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- 移动端布局最佳实践(viewport+rem)
通过前几天写的两篇博客(浅谈移动端三大viewport和移动端em和rem区别),我们现在来总结一下如何实现一个最佳方案. 之前在第二篇博客中提到过我们可以使用媒体查询来针对不同设备及做适配,如下图 ...