1.
#include<bits/stdc++.h>
常见stl库函数:见链接(72条消息) STL 中的常见实用库函数(适合初学算法者阅读)(持续更新中)_stl库函数_H-rosy的博客-CSDN博客
2. 而老师用的
#include<stdafx.h>//这是自己写的,
会有一个stdfax.cpp来生成预编译头
但一般只用于vscode,不同编译器之间是不兼容的。
(72条消息) #include“stdafx.h”详解_"#include \"stdafx.h"_qingkong8832的博客-CSDN博客
3.积分赛K题,只用看第一个和最后一个是否相同就行了,因为有上就必有下
取string对象的长度,C语言中使用strlen()来获取字符串长度,C++中使用str.size()
或str.length()
.
string str("hello!");
int len1 = str.size();
int len2 = str.length();
读入了一个int n之后,要把后面的这个换行符“吃掉”,否则后续读入字符串会有问题。
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int main() {
int n;
cin>>n;
cin.ignore();
for(int i=1;i<=n;++i){
string s;
getline(cin, s);
int t=s.size();
if(s.at(0)==s.at(t-1)){
cout<<s<<endl;
}
else if(s.at(0)<s.at(t-1)){
s[t-1]='a';
cout<<s<<endl;
}
else {
s[t-1]='b';
cout<<s<<endl;
}}
return 0;
}
关于常用string的函数(72条消息) C++-string常用函数整理(建议收藏)_c++string类函数_翟天保Steven的博客-CSDN博客
3.最大分组问题:
若运行框抛出terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::insert:
多半是容器访问越界
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int t;
int n,r,b;//r为A队赢得次数,b为B队赢得,A强于B队
cin>>t;
for(int i=1;i<=t;++i){
cin>>n>>r>>b;
int xu=0;
string a(r,'R');
//分成了b+1组
int yu=r%(b+1);
int q=r/(b+1);
if(yu==0){
for(int j=1;j<=b;++j){
xu+=q;
a.insert(xu,1,'B');
xu+=1;
}}
else{
for(int j=1;j<=yu-1;++j){
xu+=q+1;
a.insert(xu,1,'B');
xu+=1;
}
for(int j=1;j<=(b+1)-(yu-1)-1;++j){
xu+=q;
a.insert(xu,1,'B');
xu+=1;
}
}
cout<<a<<endl;
}
return 0;
}
是一个非常妙的数学思路: