整理内容是这周ojDDL的题。包括字符串,函数和结构体。
字符串
- 字符串string类的基本运用:
- 本质是char数组。在读入string类后最后都有’\0’。
- 输入。cin>>不考虑空格、回车。getline(cin,stringvar)可以读入一行字符串,包括空格,以回车为停止读入标志。cin.get(char),每次读入单个字符,不适用于不知道长度的读入。可以读入回车。
- 输出:cout<<
- 几个题目中常用到函数:reverse(开始的位置,结束的位置的后面一个位置),可以让字符串反转(回文类问题)。stringvar.length(),得到字符串的长度(不计’\0’)。一般在遍历每一个string元素的时候可以用在for循环退出条件上。transform(开始,结束,存放结果的var容器迭代器(改变原string就可以和第一个参数一样就行),::tolower/::toupper)将stringvar的选定区间转换大小写(第四个参数也可以自己定义函数写变化规则)。截取子串stringvar.substr()方法。stringvar.substr(开始截取的位置,需要的长度)。返回值是一个string,需要用一个string接收。如果不给第二个参数,那么会从开始到末尾。 若开始位置超过了string的大小,则substr函数会抛出一个out_of_range异常;若截取长度的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾。
- 具体题目:
- 子字符串排序(有两种做法,第一个不是我做的。第二个我写的就是用了sort快排和substr方法)
第一种
- 子字符串排序(有两种做法,第一个不是我做的。第二个我写的就是用了sort快排和substr方法)
#include <bits/stdc++.h>
using namespace std;
#define N 20
int cmp(const void* a, const void* b);
int main()
{
char line[N];
char all[10][N];
int len;
while(cin>>line){
len=strlen(line);
for(int i=0;i<len;i++){
strcpy(all[i],line+i);
}
qsort(all,len,N,cmp);
for(int i=0;i<len;i++)
{
cout<<all[i]<<endl;
}
}
return 0;
}
int cmp(const void* a, const void* b)
{
return strcmp((char*)a, (char*)b);
}
第二种
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(cin>>s){
string*res=new string[s.length()];
for(int i=0;i<s.length();i++){
res[i]=s.substr(i);
}
sort(res,res+s.length());
for(int i=0;i<s.length();i++){
cout<<res[i]<<endl;
}
}
return 0;
}
2.字符串对比。主要用到了transform()
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s0,s1;
cin>>s0>>s1;
if(s0.length()!=s1.length()){
cout<<1<<endl;
}
else{
if(s0==s1){cout<<2<<endl;}
else{
transform(s0.begin(),s0.end(),s0.begin(),::toupper);
transform(s1.begin(),s1.end(),s1.begin(),::toupper);
if(s1==s0) {cout<<3<<endl;}
else cout<<4<<endl;
}
}
return 0;
}
函数
- 主要就是要让你封装,细节没什么难的。这儿就记录一下sort的自定义排序吧。(主要用在结构体上)
// sort自定义排序
#include <bits/stdc++.h>
using namespace std;
typedef struct _purchase{
string good;
int price;
int storage;
}purchase;
//要求你用这么个结构体,而且需要用pirce或者storage来排序。
bool cmp(const purchase a,const purchase b){
if(a.price!=b.price)
return a.price<b.price;//如果你要前一个小于后一个,那么就让这种条件返回true就可以了。
//价格一样根据库存排序
else return a.storage>b.storage;
}
int main()
{
int n;cin>>n;
purchase* a=new purchase[sizeof(purchase)*n];
for(int i=0;i<n;i++){
cin>>a[i].good>>a[i].price>>a[i].storage;
}
sort(a,a+n,cmp);//cmp是自定义的函数名。两个参数左闭右开。
for(int i=0;i<n;i++){
cout<<a[i].good<<' '<<a[i].price<<' '<<a[i].storage<<endl;
}
return 0;
}
结构体
额,结构体其实简单,中等题大多数都在考分级排序。就上面的就行。有一道题需要用到STL标准库set的自定义排序,有两种实现方式。我暂时自己也还没完全搞懂第一种,就先放一种的实现吧(重载<)。
长方形排序
#include <bits/stdc++.h>
using namespace std;
//8.2
struct rectangle
{
int id;
int length;
int width;
};
bool operator<(const rectangle& r1,const rectangle& r2)
{
if(r1.id!=r2.id) return r1.id<r2.id;
else{
if(r1.length!=r2.length) return r1.length<r2.length;
else return r1.width<r2.width;
}
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++){
set<rectangle> rs;
rectangle r;
int m;
cin>>m;
for(int j=0;j<m;j++){
int a,b;
cin>>r.id>>a>>b;
r.length=max(a,b);
r.width=min(a,b);
rs.insert(r);
}
for(auto x:rs){
cout<<x.id<<' '<<x.length<<' '<<x.width<<endl;
}
cout<<endl;
}
return 0;
}
再附上标准答案的代码吧,用了假去重(自称为。
#include <iostream>
#include <algorithm>
using namespace std;
struct ak
{
int num,lenth,width;
}a[1000];
bool cmp(ak a,ak b)
{
if(a.num<b.num)
return 1;
else if(a.num==b.num&&a.lenth<b.lenth)
return 1;
else if(a.num==b.num&&a.lenth==b.lenth&&a.width<b.width)
return 1;
else return 0;
}
int main()
{
int t,n,i,b,c;
cin >> t;
while(t--)
{
cin >> n;
for(i=0;i<n;i++)
{
cin >> a[i].num >> b >> c;
if(b>=c)
{
a[i].lenth=b;
a[i].width=c;
}
else
{
a[i].lenth=c;
a[i].width=b;
}
}
sort(a,a+n,cmp);
for(i=0;i<n;i++)
{
if(a[i].num==a[i+1].num&&a[i].lenth==a[i+1].lenth&&a[i].width==a[i+1].width)
continue;
cout << a[i].num << ' ' << a[i].lenth << ' '<< a[i].width << endl;
}
cout << endl;
}
return 0;
}
//剩下的坑以后有时间再补。