6.1 vector
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
vector<int> a;//一维数组
vector< vector<int> > b;//二维数组
for(int i=1; i<=5; i++) /*在vector后面添加一个元素,O(1) */
a.push_back(i);
a.pop_back(); /*删除vector的尾元素,O(1) */
a.size(); /*获取vector中元素的个数,O(1) */
a.clear(); /*清空vector中的所有元素,O(n) */
a.insert(a.begin(), -1); /*将-1插入a[0]的位置,O(n)*/
a.erase(a.begin()); /*删除a[0]处的元素*/
for(int i=1; i<=5; i++)
a.push_back(i);
a.erase(a.begin()+1, a.begin()+3); /* 删除a[1], a[2]左闭右开 */
vector<int>::iterator it = a.begin();
for(it = a.begin(); it!=a.end(); it++)//左闭右开
printf("%d ",*it);
return 0;
}
/*
1 4 5
*/
6.2 set
#include<stdio.h>
#include<set>
using namespace std;
int main()
{
/*
set 内部自动有序且不含重复元素
insert(); 插入且自动递增排序和去重
find(); 返回set中对应值value得迭代器
*/
set<int> st;
for(int i=1; i<=3; i++)
st.insert(i);
set<int>::iterator it = st.find(2);
printf("%d\n",*it);
st.erase(st.find(1)); //删除元素
st.erase(2);//删除值为2的元素
st.erase(st.begin(), st.end());//删除一个区间的元素
return 0;
}
6.3 string
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int main()
{
/*
1. 通过下标访问,
如果要读入和输出整个字符串,则只能用cin和cout.
或者用c_str()将string类型转换为字符数组进行输出
2. 通过迭代器访问.
3. += 将两个string直接拼接起来。
4. ==, !=, <, <=, >, >=比较大小, 比较规则是字典序.
5. length()/size(), insert()
insert(2, string),在str[2]号位置插入字符串string
insert(2, s1.begin(), s1.end());
6. erase(str.begin()+4);
str.erase(str.begin()+2, str.end()-1);
str.erase(3,2) 删除从3号位开始的2个字符
substr(0, 5) 返回从str[0]开始,长度为len的子串
7. string::npos == -1 或者 string::npos==4294967295
8. str.find(str1); 返回str1在str中第一次出现的位置,若没有则返回string::npos
str.find(str1, pos); 从str的pos号位开始匹配str1,返回值同上.
9. getline(cin, s); 输入一行并赋值给s
*/
string str = "abcd";
for(int i=0; i<str.length(); i++)
{
printf("%c", str[i]);//输出abcd
}
printf("%d\n", str.c_str());
for(string::iterator it = str.begin(); it!=str.end(); it++)
{
printf("%c",*it);
}
if(string::npos==-1)
cout<<"-1 is true."<<endl;
if(string::npos==4294967295)
cout<<"4294967295 is also true."<<endl;
return 0;
}
1060 Are They Equal (25 分)
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×105 with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.
Input Specification:
Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10100, and that its total digit number is less than 100.
Output Specification:
For each test case, print in a line YES
if the two numbers are treated equal, and then the number in the standard form 0.d[1]...d[N]*10^k
(d[1]
>0 unless the number is 0); or NO
if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.
Note: Simple chopping is assumed without rounding.
Sample Input 1:
3 12300 12358.9
Sample Output 1:
YES 0.123*10^5
Sample Input 2:
3 120 128
Sample Output 2:
NO 0.120*10^3 0.128*10^3
题意:将a, b转换为科学计数法,保留精度为n,比较两个结果是否相同。
输入的数有两种形式:
- 0.xxxxxxx
- xxx.xxxx
#include<stdio.h> #include<string> #include<iostream> using namespace std; int n;//精度 string deal(string s, int &e) { int k = 0; while(s.length()>0&&s[0]=='0')//去掉前导0 s.erase(s.begin()); if(s[0]=='.')//.xxxxxx { s.erase(s.begin()); while(s.length()>0&&s[0]=='0') { s.erase(s.begin()); e--; } } else { while(k<s.length()&&s[k]!='.'){ e++; k++; } if(k<s.length()) s.erase(s.begin()+k);//删除小数点 if(s.length()==0) e=0; } int num=0; k=0; string ans=""; while(num<n) { if(k<s.length()) ans+=s[k++]; else ans+='0'; num++; } return ans; } int main() { string s1, s2, ans1, ans2; while(cin>>n>>s1>>s2) { int e1=0, e2=0; ans1 = deal(s1, e1); ans2 = deal(s2, e2); if(ans1==ans2) { cout<<"YES 0."<<ans1<<"*10^"<<e1<<endl; } else { cout<<"NO 0."<<ans1<<"*10^"<<e1<<" 0."<<ans2<<"*10^"<<e2<<endl; } } }
6.4 map
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<map>
#include<set>
using namespace std;
int main()
{
/*
如果是字符串到整型的映射,必须使用string而不能用char[]数组。
it->first是当前映射的键,it->second是当前映射的值。
map会以键从小到大的顺序自动排序。
map,set内部是使用红黑树实现的。在建立映射的过程中会自动实现从小到大的排序功能。
size();映射的对数
clear();清空所有元素
map的键和值是唯一的,而如果要一个键对应多个值,就只能用multimap
unordered_map:以散列代替map内部的红黑树,使其可以用来处理只映射而不按key排序的需求,速度比map要快的多。
*/
map<string , int> m1;
map<set<int>, string> m2;
m1["c"] = 20;
m1["m"] = 30;
m1["a"] = 40;
cout<<m1["c"]<<endl;
map<string, int>::iterator it = m1.find("m");
cout<<it->first<<" "<<it->second<<endl;
m1.erase(it, m1.end());//删除 m 30
m1.erase("c");//删除 c 20
for(map<string, int>::iterator it = m1.begin(); it!=m1.end();it++)
cout<<it->first<<" "<<it->second<<endl;
}
6.5 queue
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int main()
{
queue<int> q;
for(int i=1; i<=5; i++)
q.push(i);
printf("%d %d\n",q.front(), q.back());
q.pop();//队首元素出队
printf("%d %d\n",q.front(), q.back());
//q.empty()==0,为非空, q.empty()==1为空
printf("%d\n", q.empty());
printf("%d\n", q.size());
/*
使用front()和pop()函数前,必须用empty()判断队列是否为空
双端队列 deque
优先队列 prior_queue
*/
}
6.6 priority_queue
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
struct fruit{
string name;
int price;//重载
friend bool operator<(const fruit &f1, const fruit &f2)
{
return f1.price > f2.price;//价格高的为优先级高
} // > 价格低的为优先级高
}f1, f2, f3;
int main()
{
/*
priority_queue 优先队列,底层是堆实现的,大顶堆,
队首元素一定是当前队列中优先级最高的那一个。
*/
priority_queue<int, vector<int>, greater<int> > q;
q.push(3);
q.push(4);
q.push(1);
printf("%d\n",q.top());//没有front()和back函数,q.top()获得堆顶元素,使用之前必须用empty()判断优先队列是否为空
q.pop();//删除堆顶元素
printf("%d\n", q.top());
printf("%d\n", q.empty());
printf("%d\n", q.size());
priority_queue<fruit> q1;
/*
vector<int> 填写的是来承载底层数据堆的容器
less<int> 数字大的优先级大
greater<int>数字小的优先级大
*/
f1.name = "桃子";
f1.price = 3;
f2.name = "梨子";
f2.price = 4;
f3.name = "苹果";
f3.price = 1;
q1.push(f1);
q1.push(f2);
q1.push(f3);
cout<<q1.top().name<<" "<<q1.top().price;
}