0828-C03STL数据结构2
2024-08-28 11:40:44
发布于:江苏
10阅读
0回复
0点赞
//P2084进制转换
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main(){
cin >> n >> s;
int len = s.size();
for (int i=0; i<len; i++){
if (s[i] != '0') {
if(i!=0) printf("+");
printf("%c*%d^%d", s[i], n, len-i-1);
}
}
return 0;
}
//P3613寄包柜
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int main(){
int n, q;
cin >> n >> q;
vector<vector<int> > v(n+1); //定义了一个二维的动态数组
while (q--){
int op, i, j, k;
cin>>op;
if(1 == op) {
cin >> i >> j >> k;
if (v[i].size() < j)
v[i].resize(j+1); //根据输入来进行分配空间
v[i][j] = k;
}
else if (2 == op){
cin >> i >> j;
cout << v[i][j] << endl;
}
}
return 0;
}
set
#include <bits/stdc++.h>
#include <set>
using namespace std;
int n, t;
set<int> s;
int main(){
cin >> n;
for (int i=1; i<=n; i++){
cin >>t;
s.insert(t);
}
cout << s.size() << endl;
for (auto i:s){ //auto:自动获取数据类型
cout<<i<<" ";
}
#if 0
//迭代器 iterator, 指针
set<int>::iterator it; //定义了一个set的迭代器
for (it=s.begin(); it!=s.end(); it++){
cout << *it << " "; //取值符号
}
#endif
return 0;
}
pair
#include <bits/stdc++.h>
#include <map>
using namespace std;
typedef pair<string, int> PSI;
int main(){
PSI a, b, c[105];
c[0].first = "zhangfei";
c[0].second = 29;
c[1].first = "liubei";
c[1].second = 36;
c[2].first = "guanyu";
c[2].second = 31;
sort(c, c+3); //默认按照first从小到大排序
for (int i=0; i<3; i++){
cout<<c[i].first << ' ' <<c[i].second << endl;
}
return 0;
}
auto
/*
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
char a = 3;
int b = 4;
auto c = a+b;
cout << c << endl;
cout << typeid(c).name() << endl;
return 0;
}
map
/*
*/
#include <bits/stdc++.h>
using namespace std;
map<int, string> a;
//输出方式一: auto
void print(){
printf("--------------------------\n");
for (auto i: a){
cout << i.first << ' ' << i.second << endl;
}
}
//输出方式二: 迭代器
void print_iterator(){
map<int, string>::iterator it = a.begin();
for (; it!=a.end(); it++){
cout << it->first <<' '<< it->second << endl;
}
cout << endl;
}
int main(){
a[1000] = "xmw";
a[1002] = "zhangsan";
a[1004] = "lisi";
a[1006] = "wangwu";
a[1001] = "zhangfei";
a[1003] = "guanyu";
a[1005] = "liubei";
print();
a[1006] = "machao"; //覆盖赋值
print();
a.insert({1004, "黄忠"}); //若key已经存在则不能插入
print();
a.erase(1004); //删除key为1004
print();
cout << a.count(1006) << endl; //1
print_iterator();
return 0;
}
订单结算
/*
stack
queue
priority_queue
vector
set
pair
map
*/
#include <bits/stdc++.h>
using namespace std;
int n;
map<string, double> a;
double p;
string name;
int main(){
cin >> n;
for(int i=1; i<=n; i++){
cin >> name >> p;
if(a.count(name) == 0) p*=0.75;
a[name] += p;
}
for (auto i:a){
cout << i.first <<' ';
//C++方式: 设置精度,控制到小数点后1位
//include <iomanip>
// cout << fixed << setprecision(1) << i.second << endl;
//C语言:
printf("%.1lf", i.second);
}
return 0;
}
这里空空如也
有帮助,赞一个