c++学习笔记

0.前言

- 此随笔为作者学习c++过程中所记录的笔记;

- 学习原因,不定时更新;

 

1.笔记

单链表

结构、建立、输出;

 1 #include <cstdio>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 struct Node
 7 {
 8     int data;
 9     Node *next;
10 };
11 
12 Node *head, *p, *r;
13 int x;
14 
15 int main()
16 {
17     cin >> x;
18     head = new Node;
19     r = head;
20     while (x != -1)
21     {
22         p = new Node;
23         p->data = x;
24         p->next = NULL;
25         r->next = p;
26         r = p;
27         cin >> x;
28     }
29     
30     p = head->next;
31     while (p->next != NULL)
32     {
33         cout << p->data << " ";
34         p = p->next;
35     }
36     cout << p->data << endl;
37     
38     return 0;
39 }
View Code

 

位运算

和 » 运算

一个数 x & 1可以取得末尾,因此可以将 x 1 判断奇偶;

>>> x & 1 == 0(偶数);

>>> x 1 != 0(奇数);

一个数 x »= 1 可以去掉二进制下的末位,因此可以将 x >>= 1 看作 x /= 2;

>>> x = 10;

>>> x »= 1;

>>> x = 5;

一个数 x « n ,相当于 x 2^n;

>>> 2 « 2 = 8;

>>> 3 « 2 = 12;

一个数 x » n ,相当于 x 2^n;

>>> 20 » 2 = 5;

>>> 40 » 2 = 10;

比较两个数是否相等,取两数异或值,若为0,则相等;

1 bool IsEqual(int a,int b)
2 {
3     return !(a^b);
4 }
View Code

不使用第三方变量交换 a,b

1 int a,int b;
2 a=a^b;
3 b=a^b;
4 a=a^b;
View Code

求一个数 x 的绝对值;

1 (n^(n>>31))-(n>>31);
View Code

判断两个数是否同号;

1 !((a^b)>>31);
View Code

提取 lowbit();

1 int lowbit(int x)
2 {
3     return x&-x;
4 }
View Code

计算两个整数的平均值(防止溢出);

1 int average(int x, int y) //返回X,Y 的平均值 
2 { 
3   return (x&y)+((x^y)>>1); 
4 }
View Code

判断一个整数是否为 2 的幂 2^n;

1 ((x&(x-1))==0)&&(x!=0);
View Code

 

快速乘模板

1 LL mul(LL a, LL b, LL P){
2     LL L = a * (b >> 25LL) % P * (1LL << 25) % P;
3     LL R = a * (b & ((1LL << 25) - 1)) % P;
4     return (L + R) % P;
5 }
View Code

 

快速幂模板(与上连用)

1 LL pow(LL a,LL b) {
2     LL res=1,base=a;
3     while(b) {
4         if(b&1) res=mul(res,base,mod);
5         base=mul(base,base,mod);
6         b>>=1;
7     }
8     return res;
9 }
View Code

 

快读快写模板

 1 template <typename T>
 2 inline void read(T &x){
 3     x=0;T f=0;
 4     char ch=0;
 5     while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
 6     while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
 7     x=f?-x:x;
 8 }
 9 
10 template <typename T>
11 inline int write(T x){
12     if(x<0) putchar('-'),x=-x;
13     if(x<10) putchar(x+'0');
14     else write(x/10),putchar(x%10+'0');
15 }
View Code

 

快排模板

 1 #include <cstdio>
 2 using namespace std;
 3 int a[1000001];
 4 void swap(int &a,int &b){
 5     int temp=a;
 6     a=b;
 7     b=temp;
 8 }
 9 
10 void cmpswap(int &a,int &b){
11     if(a>b) swap(a,b);
12 }
13 
14 void qsort(int a[],int l,int r){
15     if(l>=r) return;
16     int s=l+(r-l)/2;
17     cmpswap(a[s],a[l]),cmpswap(a[l],a[r]);
18     int p=a[l];
19     int i=l+1,j=r;
20     while(true){
21         while(a[j]>p) j--;
22         while(a[i]<p&&i<r) i++;
23         if(i<j) swap(a[i++],a[j--]);
24         else{
25             swap(a[l],a[j]);
26             break;
27         }
28     }
29     qsort(a,l,j-1);
30     qsort(a,j+1,r);
31 }
32 
33 int main(){
34     int n;
35     scanf("%d",&n);
36     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
37     qsort(a,1,n);
38     for(int i=1;i<=n;i++){
39         if(i!=n) printf("%d ",a[i]);
40         else printf("%d\n",a[i]);
41     }
42     return 0;
43 }
View Code

 

高精加法模板

 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 int c;
 6 char a[510],b[510];
 7 int ia[510],ib[510],ic[510];
 8 
 9 int max(int a,int b) {
10     return a>b?a:b;
11 }
12 
13 int main() {
14     scanf("%s %s",a,b);
15     int lena=strlen(a);
16     int lenb=strlen(b);
17     int lenc=max(lena,lenb);
18     for(int i=0;i<lena;i++) ia[lena-i-1]=a[i]-'0';
19     for(int i=0;i<lenb;i++) ib[lenb-i-1]=b[i]-'0';
20     for(int i=0;i<lenc;i++) {
21         ic[i]=ia[i]+ib[i]+c;
22         c=ic[i]/10;
23         ic[i]=ic[i]%10;
24         if(c>0) {
25             ic[lenc]=c;
26             lenc++;
27         }
28     }
29     while(ic[lenc]==0&&lenc>=1) lenc--;
30     for(int i=lenc;i>=0;i--) {
31         printf("%d",ic[i]);
32     }
33     return 0;
34 }
View Code

 

高精减法模板

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int MAXN=1e5+4;
 7 char a[MAXN],b[MAXN],n[MAXN];
 8 int ia[MAXN],ib[MAXN],ic[MAXN];
 9 
10 int max(int a,int b) {
11     return a>b?a:b;
12 }
13 
14 int main() {
15     scanf("%s %s",a,b);
16     if(strlen(a)<strlen(b)||strlen(a)==strlen(b)&&strcmp(a,b)<0) {
17         strcpy(n,a);
18         strcpy(a,b);
19         strcpy(b,n);
20         printf("-");
21     }
22     int lena=strlen(a);
23     int lenb=strlen(b);
24     int lenc=max(lena,lenb);
25     for(int i=0;i<lena;i++) { ia[lena-i-1]=a[i]-'0'; }
26     for(int i=0;i<lenb;i++) { ib[lenb-i-1]=b[i]-'0'; }
27     for(int i=0;i<lenc;i++) {
28         if(ia[i]<ib[i]) {
29             ia[i+1]--;
30             ia[i]+=10;
31         }
32         ic[i]=ia[i]-ib[i];
33     }
34     while(ic[lenc]==0&&lenc>=1) { lenc--; }
35     for(int i=lenc;i>=0;i--) {
36         printf("%d",ic[i]); 
37     }
38     return 0;
39 }
View Code

 

posted @ 2020-06-14 20:47  Rnin  阅读(161)  评论(0)    收藏  举报