c++
基本框架
#include<bits/stdc++.h>
using namespace std;
int main(){
return 0;
}
栈
函数
#include<bits/stdc++.h>
using namespace std;
int main(){
//void
push()//入栈
pop()//删除栈顶元素
//int
size()//栈的长度
//bool
empty()//栈是否为空
//
top()//获取栈顶元素
return 0;
数组模拟栈
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int Stack[N];
int top=-1;
void push(int n){//入栈
if(top>=N){
cout << "[Error]栈满了\n";
return ;
}
Stack[++top]=n;
}
int gettop(){//获取栈顶元素
if(top<0){
cout << "[Error]栈空\n";
}
return Stack[top];
}
void pop(){//删除栈顶元素
if(top<0){
cout << "[Error]栈空\n";
return ;
}
top--;
}
int size(){//返回栈的长度
return top+1;
}
bool empty(){//栈是否为空
return size()==0;
}
void clear(){//清空栈
top = -1;
}
int main(){
return 0;
}
数组模拟队列
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int Queue[N];
int topt=0,topw=-1;
int size(){//返回队列的长度
return topw-topt+1;
}
bool empty(){//队列是否为空
return size()==0;
}
void push(int n){//入队
if(topw==N){
cout << "[Error]队列满了\n";
return ;
}
Queue[++topw]=n;
}
int getfront(){//获取队头元素
if(empty()){
cout << "[Error]队列空\n";
}
return Queue[topt];
}
int getback(){//获取队尾元素
if(empty()){
cout << "[Error]队列空\n";
}
return Queue[topw];
}
void pop(){//出队元素
if(empty()){
cout << "[Error]队列空\n";
return ;
}
topt++;
}
void clear(){//清空队列
topt = 0;
topw = -1;
}
int main(){
return 0;
}
读、写入文件
#include<bits/stdc++.h>
using namespace std;
int main(){
//写入
freopen("/*文件名加后缀*/","w",stdout);
cout << "/*写入的内容*/";
fclose(stdout);//停止写入
return 0;
}
常用函数
#include<bits/stdc++.h>
using namespace std;
int main(){
abs();//绝对值
return 0;
}
deBUG
#include<bits/stdc++.h>
#define deBUG(var) cout << "[deBUG]" << #var << "=" << var << endl;
using namespace std;
int main(){
int n;
cin >> n;
deBUG(n);
return 0;
}
三目运算符
#include<bits/stdc++.h>
using namespace std;
int main(){
/*式子*/ ? /*结果为真(不为0)执行(返回)这个式子*/ : /*结果为假(为0)执行(返回)这个式子*/;
return 0;
}
排序
字符串内部排序(降序,不写cmp是升序)
#include<bits/stdc++.h>
using namespace std;
bool cmp(char x,char y){
return x>y;
}
int main(){
string s;
cin >> s;
sort(s.begin(),s.end(),cmp);
cout << s;
return 0;
}
算法
高精度
加法
数组模拟
#include<bits/stdc++.h>
using namespace std;
int a[100000],b[100000],c[1000000];
int main(){
string a1,b1;
cin >> a1 >> b1 ;
for(int i=a1.size()-1;i>=0;i--){
a[a1.size()-i-1]=a1[i]-'0';
}
for(int i=b1.size()-1;i>=0;i--){
b[b1.size()-i-1]=b1[i]-'0';
}
int maxsize=max(a1.size(),b1.size())-1;
c[0] = (a[0] + b[0])%10;
for(int i=1;i<=maxsize;i++){
c[i] = (a[i] + b[i])%10 + (a[i-1] + b[i-1])/10;
}
c[maxsize+1] = (a[maxsize] + b[maxsize])/10;
if(c[maxsize+1]==1){
cout << 1;
}
for(int i=maxsize;i>=0;i--){
cout << c[i];
}
}
struct模拟
#include<bits/stdc++.h>
using namespace std;
struct bigint{
int a[1010]={0};
int len=0;
void normal(){
while(a[len]==0&&len>1){
len--;
}
}
void in(string s){
for(int i=s.size()-1;i>=0;i--){
a[++len]=s[i]-'0';
}
normal();
}
void out(){
for(int i=len;i>0;i--){
cout << a[i];
}
}
bigint operator+(bigint b){
bigint sum;
sum.len=max(len,b.len)+1;
for(int i=1;i<=sum.len;i++){
sum.a[i]+=a[i]+b.a[i];
sum.a[i+1]+=sum.a[i]/10;
sum.a[i]%=10;
}
sum.normal();
return sum;
}
}a,b,c;
int main(){
string A,B;
cin >> A >> B;
a.in(A);
b.in(B);
c=a+b;
c.out();
return 0;
}
栈模拟
#include<bits/stdc++.h>
using namespace std;
int main(){
stack<int> a,b,c;
string A,B;
cin >> A >> B;
for(int i=0;i<A.size();i++){
a.push(A[i]-'0');
}
for(int i=0;i<B.size();i++){
b.push(B[i]-'0');
}
int Size = max(A.size(),B.size()),j=0;
for(int i=1;i<=Size;i++){
if(a.empty()){
c.push((0+b.top()+j)%10);
j = (0+b.top()+j)/10;
b.pop();
}
else if(b.empty()){
c.push((0+a.top()+j)%10);
j = (0+a.top()+j)/10;
a.pop();
}
else {
c.push((a.top()+b.top()+j)%10);
j = (a.top()+b.top()+j)/10;
b.pop();
a.pop();
}
}
if(j==1){
c.push(1);
}
while(!c.empty()){
cout << c.top();
c.pop();
}
return 0;
}