1.使用str存储数字时,在运算时要把str转成数字,否则会出错
skew = skew + (str[i] - '0') * (pow(2, str.length()-i)-1);
2.强制类型转换
//int转string
int num = 123;
string str = to_string(num);
//string转int
string str = "123";
int num = stoi(str);
要注意的是转为string类型后,如果需要比较,要用if(str[i] == '3'),注意类型已经转为char了
3. 并查集
int root[100]; //用一个数组表示并查集
//初始化并查集
void Init(int root[]){
for(int i=0;i<100;i++)
root[i]=-1;
}
//Find “查”操作,找x所属集合(返回x所属根结点)
int Find(int a[],int x){
while(a[x]>=0) //循环寻找x的根
x=a[x];
return x;
}
//Union
void Union(int S[],int Root1,int Root2){
if(Root1==Root2) return;
//将根Root2连接到另一根Root1下面
S[Root2]=Root1;
}
//Union “并”操作,小树合并到大树
void Union(int S[],int Root1,int Root2){
x = Find(x);
y = Find(y);
if (root1 == root2){
return;
}
if(S[Root2]>S[Root1]) { //Root2结点数更少
S[Root1] += S[Root2]; //累加结点总数
S[Root2]=Root1; //小树合并到大树
} else {
S[Root2] += S[Root1]; //累加结点总数
S[Root1]=Root2; //小树合并到大树
}
}
//Find “查”操作优化,先找到根节点,再进行“压缩路径”
int Find(int S[],int x){
int root = x;
while(S[root]>=0)
root=S[root]; //循环找到根
while(x!=root){ //压缩路径
int t=S[x]; //t指向x的父节点
S[x]=root; //x直接挂到根节点下
x=t;
}
return root; //返回根节点编号
}
4.十进制和二进制的转换
#include <bitset>
// 十进制转二进制
string ToB(int n) {
string s;
while (n> 0) {
int t= n % 2;
s= to_string(t) + s;
n /= 2;
}
return s;
}
// 二进制转十进制
int ToD(string& s) {
bitset<32> binary(s);
int n= binary.to_ulong();
return n;
}
5.需要最大、最小整数值时
#include <climits>
int max = INT_MIN;
int min = INT_MAX;
6.getline用法
while (cin >> n){
cin.ignore(); // 清除换行符
getline(cin, str);//空格也输入
//要注意的是有可能会把前面的换行符也算上,从而出错
... ...
}
7.字符串逆序
char str[] = "Hello, World!";
//对str 进行逆序
strrev(str);
//或者
#include <algorithm>
reverse(str.begin(), str.end());
8.数组按列排序
for(int i=0 ; i<6 ; i++){
for(int j=0 ; j<4 ; j++){
for (int k=j+1 ; k<5 ; k++){
if(a[j][i] > a[k][i]){
swap(a[j][i], a[k][i]);
}
}
}
}
9.一个例题,注意str.erase的用法
#include <iostream>
using namespace std;
int main(){
string str;
char c;
cin >> str >> c;
for (int i=0 ; i<str.length() ; i++){
if (str[i] == c){
str.erase(i,1);
i--; //更新索引,不更新会出错
}
}
for (int i=0 ; i<str.length() ; i++){
cout << str[i];
}
return 0;
}
10.链表
//遍历链表统计和给定的已知变量相等的节点的个数
#include <iostream>
using namespace std;
struct Lnode{
int data;
Lnode *next;
};
int main(){
int n, x;
Lnode *l = new Lnode(); // 初始化链表头节点
Lnode *q = l ;
int count = 0;
cin >> n >> x;
for (int i=0 ; i<n ; i++){
Lnode *p = new Lnode();
cin >> p->data ;
q->next = p;
q = q->next;
}
Lnode *s = l->next;
while(s != nullptr){
if(s->data == x){
count ++;
}
s = s->next;
}
Lnode *t = l->next;
while (t != nullptr) {
Lnode *m = t;
t = t->next;
delete m;
}
cout << count;
return 0;
}
链表的详细用法:
struct Node {
int data;
Node* next;
};
Node* head = nullptr; // 初始化链表头指针为空
//头插法
Node* newNode = new Node;
newNode->data = value; // 设置节点数据
newNode->next = head; // 将新节点的next指针指向当前头节点
head = newNode; // 更新头指针指向新节点
//尾插法
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr; // 新节点的next指针指向null
Node* temp = head;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newNode; // 找到链表末尾,将新节点连接到末
//遍历
Node* temp = head;
while (temp != nullptr) {
// 处理节点数据
cout << temp->data << " ";
temp = temp->next; // 移动到下一个节点
}
//删除
if (head != nullptr) {
Node* temp = head;
head = head->next; // 更新头指针
delete temp; // 释放内存
}