小学期C++实践

发布时间 2023-07-06 14:54:57作者: 雪之下,树之旁

一、链表

1、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

struct node *reverseList(struct node *head){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    vector <int> G; 
    while(p != NULL){
        G.push_back(p->val); 
        p = p->next; 
    }
    for(int i = G.size() - 1; i >= 0; i--){
        struct node *tmp = new node; 
        tmp->val = G[i]; 
        tmp->next = NULL; 
        if(head1 == NULL){
            head1 = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = now->next; 
        }
    }
    return head1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(scanf("%d", &x) != EOF){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head1 = reverseList(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

2、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *deleteDuplicates(struct node *head){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    int num = head->val; 
    insert(head1, now, num); 
    p = p->next; 
    while(p != NULL){
        if(p->val == num){
            p = p->next; 
            continue; 
        }
        num = p->val; 
        insert(head1, now, num); 
        p = p->next; 
    }
    return head1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head1 = deleteDuplicates(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

3、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *deleteDuplicates(struct node *head){
    struct node* p = head; 
    map<int, int> g; 
    while(p != NULL){
        g[p->val]++; 
        p = p->next; 
    }
    struct node *now = NULL; 
    p = head; 
    head = NULL; 
    while(p != NULL){
        if(g[p->val] == 1){
            insert(head, now, p->val); 
        }
        p = p->next; 
    }
    return head; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head1 = deleteDuplicates(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

4、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head1 = NULL;  
struct node *head2 = NULL; 

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *addTwoNumbers(struct node *head1, struct node *head2){
    struct node *head = NULL; 
    struct node *now = NULL; 
    
    struct node *p1 = head1, *p2 = head2; 
    int last = 0; 
    while(p1 != NULL && p2 != NULL){
        int x = p1->val + p2->val + last; 
        last = 0; 
        if(x >= 10){
            x -= 10; 
            last += 1; 
        }
        insert(head, now, x); 
        p1 = p1->next; 
        p2 = p2->next; 
    }
    while(p1 != NULL){
        int x = p1->val + last; 
        last = 0; 
        if(x >= 10){
            last = 1; 
            x -= 10; 
        }
        insert(head, now, x); 
        p1 = p1->next; 
    }
    while(p2 != NULL){
        int x = p2->val + last; 
        last = 0; 
        if(x >= 10){
            last = 1; 
            x -= 10; 
        }
        insert(head, now, x); 
        p2 = p2->next; 
    }
    if(last){
        insert(head, now, 1); 
    }

    return head; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head1; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head1 == NULL){
            head1 = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    now = head2; 
    while(cin >> x && x != -1){
        insert(head2, now, x); 
    }

    struct node *head =  addTwoNumbers(head1, head2); 
    struct node *p = head; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

5、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *oddEvenList(struct node *head){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    
    int cnt = 0; 
    while(p != NULL){
        cnt++; 
        if(cnt % 2 == 1){
            insert(head1, now, p->val); 
        }
        p = p->next; 
    }
    cnt = 0; 
    p = head; 
    while(p != NULL){
        cnt++; 
        if(cnt % 2 == 0){
            insert(head1, now, p->val); 
        }
        p = p->next; 
    }

    return head1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head1 = oddEvenList(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

6、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *partition(struct node *head, int k){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    while(p != NULL){
        if(p->val < k){
            insert(head1, now, p->val); 
        }
        p = p->next; 
    }
    p = head;
    while (p != NULL){
        if(p->val >= k) insert(head1, now, p->val); 
        p = p->next; 
    }
    
    return head1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }
    cin >> x; 

    struct node *head1 = partition(head, x); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

7、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    int sum; // 求前缀和,前缀和相等证明中间部分为 0
    struct node *next; 
    struct node *pre; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *DeleteZero(struct node *head){
    struct node *head1 = NULL;
    struct node *now = NULL; 
    struct node* p = head; 
    struct node *q = NULL; 
    
    head->sum = head->val; 
    while(233){
        p = head; 
        while(p != NULL){
            if(p->next != NULL){
              p->next->sum = p->next->val + p->sum; 
            } 
            p = p->next; 
        } 
        bool flag = 0; 
        p = head, q = head->next; 
        while(p != NULL){
            q = p->next; 
            while(q != NULL){
                if(p->sum == q->sum || q->sum == 0){
                    flag = 1; 
                    break; 
                }
                q = q->next; 
            }
            if(flag) break; 
            p = p->next; 
        }
        if(!flag) break; 
        p = p->next; 
        if(p == head || q->sum == 0) head = q->next; 
        else p->pre->next = q->next; 
    }

    return head; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        tmp->pre = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            tmp->pre = now; 
            now = tmp; 
        }
    }

    struct node *head1 = DeleteZero(head); 
    struct node *p = head1; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}

8、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
} ;
struct node *head1 = NULL;  
struct node *head2 = NULL; 

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}

struct node *mergeTwoLists(struct node *head1, struct node *head2){
    struct node *head = NULL;
    struct node *now = NULL; 
    struct node* p1 = head1, *p2 = head2; 
    
    while(p1 != NULL && p2 != NULL){
        if(p1->val < p2->val){
            insert(head, now, p1->val); 
            p1 = p1->next; 
        }
        else{
            insert(head, now, p2->val); 
            p2 = p2->next; 
        }
    }
    while(p1 != NULL){
        insert(head, now, p1->val); 
        p1 = p1->next; 
    }
    while(p2 != NULL){
        insert(head, now, p2->val); 
        p2 = p2->next; 
    }

    return head; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head1; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head1 == NULL){
            head1 = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        if(head2 == NULL){
            head2 = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            now = tmp; 
        }
    }

    struct node *head = mergeTwoLists(head1, head2); 
    struct node *p = head; 
    while(p != NULL){
        cout << p->val << " "; 
        p = p->next; 
    }
    return 0; 
}


## 9、

```cpp
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int val; 
    struct node *next; 
    struct node *pre; 
} ;
struct node *head = NULL;  

int x; 

void insert(struct node * &head, struct node * &now, int val){
    struct node *tmp = new node; 
    tmp->val = val;
    tmp->next = NULL; 
    if(head == NULL){
        head = tmp; 
        now = tmp; 
    }
    else{
        now->next = tmp; 
        now = tmp; 
    }
    return ; 
}


bool isPalindrome(struct node* head){
    struct node *p = head; 
    struct node *q = NULL; 
    while(p != NULL){
        if(p->next == NULL) q = p; 
        p = p->next; 
    }
    if(q == head) return 1; 
    p = head; 
    while(p != q && p->pre != q){
        // printf("val: %d %d\n", p->val, q->val); 
        if(p->val != q->val) return 0; 
        p = p->next; 
        q = q->pre; 
    }
    return 1; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
    struct node *now = head; 
    while(cin >> x && x != -1){
        struct node *tmp = new node; 
        tmp->val = x; 
        tmp->next = NULL; 
        tmp->pre = NULL; 
        if(head == NULL){
            head = tmp; 
            now = tmp; 
        }
        else{
            now->next = tmp; 
            tmp->pre = now; 
            now = tmp; 
        }
    }

    if(isPalindrome(head)) cout << "True" << endl; 
    else cout << "False" << endl; 



    return 0; 
}


二、位运算

1、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010


int main(){
//	freopen("hh.txt", "r", stdin); 
    int x = -23; 
    cout << bitset<64>(x) << endl; 
    x >>= 1; 
    cout << bitset<64>(x);      // 对于补码,Windows采取高位补1.
    cout << x << endl; 
    return 0; 
}

2、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010


int main(){
//	freopen("hh.txt", "r", stdin); 
    int a = 9; 
    int b = -2; 
    cout << (int)(a & b) << endl;   
    cout << bitset<65>(a) << endl; 
    cout << bitset<64> (b) << endl;     // 对负数高位补1,转成补码后 & 运算未消去原数字的高位
    return 0; 
}

3、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

string s; 
int a; 

string Int32toSixteen(ll a){
    if(a < 0){
        a = abs(a); 
        for(int i = 0; i < 32; i++)
            a ^= (1ll << i); 
        a += 1; // 取补码
    }
    
    
    string t = ""; 
    while(a){
        int tmp = (a >> 4) << 4; 
        int x = a ^ tmp; 
        // int num = 1 | (1 << 1) | (1 << 2) | (1 << 3); 
        // x ^= num; 
        if(x < 10) t.push_back(x + '0'); 
        else t.push_back(x + 'A' - 10); 
        a >>= 4;
    }
    reverse(t.begin(), t.end()); 
    return t; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
     cin >> a; 
     s = Int32toSixteen(a); 
     cout << s; 
    return 0; 
}

4、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

string s; 
int a; 

string Int32toTwo(ll a){
    if(a < 0){
        a = abs(a); 
        for(int i = 0; i < 32; i++)
            a ^= (1ll << i); 
        a += 1; // 取补码
    }
    
    string t = ""; 
    while(a){
        int tmp = (a >> 1) << 1; 
        int x = tmp ^ a; 
        t.push_back(x + '0'); 
        a >>= 1;
    }
    reverse(t.begin(), t.end()); 
    return t; 
}

int main(){
//	freopen("hh.txt", "r", stdin); 
     cin >> a; 
     s = Int32toTwo(a); 
     cout << s; 
    return 0; 
}

5、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

string s; 
int x = 0; 

int main(){
//	freopen("hh.txt", "r", stdin); 
    cin >> s; 
    if(s[0] == '0'){
        for(int i = 0; i < s.length(); i++){
            x = (x << 1) + (s[i] - '0'); 
        }
        printf("%x\n%d", x, x);  
    }
    else{
        for(int i = 1; i < s.length(); i++){
            x = (x << 1) + (s[i] - '0'); 
        }
        x -= 1; 
        for(int i = 0; i < 15; i++)
            x ^= (1 << i); 
        x *= -1; 
        printf("%x\n%d", x, x);
    }
    
    return 0; 
}

6、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

struct node{
    int a:3;
    int b:5; 
    int c:6; 
    int d:9; 
} t;

int n; 

string Int32toSixteen(ll a){
    if(a < 0){
        a = abs(a); 
        for(int i = 0; i < 32; i++)
            a ^= (1ll << i); 
        a += 1; // 取补码
    }
    
    
    string t = ""; 
    while(a){
        int tmp = (a >> 4) << 4; 
        int x = a ^ tmp; 
        // int num = 1 | (1 << 1) | (1 << 2) | (1 << 3); 
        // x ^= num; 
        if(x < 10) t.push_back(x + '0'); 
        else t.push_back(x + 'A' - 10); 
        a >>= 4;
    }
    reverse(t.begin(), t.end()); 
    return t; 
}

string Int32toTwo(ll a){
    if(a < 0){
        a = abs(a); 
        for(int i = 0; i < 32; i++)
            a ^= (1ll << i); 
        a += 1; // 取补码
    }
    
    string t = ""; 
    while(a){
        int tmp = (a >> 1) << 1; 
        int x = tmp ^ a; 
        t.push_back(x + '0'); 
        a >>= 1;
    }
    reverse(t.begin(), t.end()); 
    return t; 
}


int main(){
//	freopen("hh.txt", "r", stdin); 
    cin >> n; 
    t.a = n, t.b = n, t.c = n, t.d = n; 
    string s1 = Int32toSixteen(t.a);  
    string s2 = Int32toSixteen(t.b); 
    string s3 = Int32toSixteen(t.c);
    string s4 = Int32toSixteen(t.d); 
    cout << s1 << " " << s2 << " " << s3 << " " << s4 << endl; 
    s1 = Int32toTwo(t.a); 
    s2 = Int32toTwo(t.b); 
    s3 = Int32toTwo(t.c);  
    s4 = Int32toTwo(t.d); 
    cout << s1 << " " << s2 << " " << s3 << " " << s4; 
    return 0; 
}