头像

微笑丶是我仅剩的骄傲




离线:7小时前


活动打卡代码 AcWing 61. 旋转链表

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
         if(k == 0 || head == nullptr || head->next == nullptr){
            return head;
        }//判断如果只有一个头节点或者只有一个数据就不用翻转,或者k=0也不用翻转
        ListNode *iter = head;
        int cnt = 1;//将cnt = 1是为了下面iter->next != nullptr做准备的
        while(iter->next != nullptr){
            iter = iter->next;
            cnt++;
        }//计算链表中的数据元素
        int i = cnt - k % cnt;//判断是否是链表中元素个数的整数倍
        if(i == cnt){
            return head;
        }
        iter->next = head;//将链表环化
        while(i--){
            iter = iter->next;
        }
        ListNode *ret = iter->next;
        iter->next = nullptr;
        return ret;
    }
};


活动打卡代码 AcWing 797. 差分

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>

using namespace std;

const int N = 100010;
int a[N], b[N];

void insert(int l, int r, int c)
{
    b[l] += c;
    b[r + 1] -= c;
}

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) insert(i, i, a[i]);
    //求差分数组
    while(m--)
    {
        int l, r, c;
        scanf("%d %d %d", &l, &r, &c);//利用差分简化运算
        insert(l, r, c);
    }

    for(int i = 1; i <= n; i++) b[i] += b[i - 1];//将差分数组求和,得到一段数据值
    for(int i = 1; i <= n; i++) cout << b[i] << ' ';
    return 0;
}


活动打卡代码 AcWing 796. 子矩阵的和

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>

using namespace std;

const int N = 1010;
int a[N][N], s[N][N];

int main()
{
    int n, m, q;
    scanf("%d %d %d", &n, &m, &q);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            scanf("%d", &a[i][j]);
        }
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
        }
    }

    while(q--)
    {
        int x1, y1, x2, y2;
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        printf("%d\n", s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]);
    }
    return 0;
}


活动打卡代码 AcWing 795. 前缀和

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>

using namespace std;

const int N = 100010;
int n, m;
int a[N], s[N];

int main()
{
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
    while(m--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        printf("%d\n", s[r]- s[l - 1]);
    }
    return 0;
}


活动打卡代码 AcWing 794. 高精度除法

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> div(vector<int> &A, int b, int &r)
{
    r = 0;
    vector<int> C;
    for(int i = A.size() - 1; i >= 0; i--){
        r = r * 10 + A[i];
        C.push_back(r / b);
        r %= b;
    }
    reverse(C.begin(), C.end());
    while(C.size() > 1 && C.back() == 0) C.pop_back();
    return C;
}

int main()
{
    string a;
    int b, r;
    cin >> a >> b;

    vector<int> A;
    for(int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
    auto C = div(A, b, r);
    for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
    cout << endl << r << endl;
    return 0;
}


活动打卡代码 AcWing 793. 高精度乘法

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
#include <vector>

using namespace std;

vector<int> mul(vector<int> &A, int b)
{
    int t = 0;
    vector<int> C;
    for(int i = 0; i < A.size(); i++)
    {
        t += A[i] * b;
        C.push_back(t % 10);
        t /= 10;
    }
    if(t) C.push_back(t);
    while(C.size() > 1 && C.back() == 0) C.pop_back();
    return C;
}

int main()
{
    int b;
    string a;
    cin >> a >> b;
    vector<int> A;
    for(int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
    auto C = mul(A, b);
    for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
    return 0;
}


活动打卡代码 AcWing 792. 高精度减法

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
#include <vector>

using namespace std;

bool cmp(vector<int> &A, vector<int> &B)
{
    if(A.size() != B.size()) return A.size() > B.size();
    else{
        for(int i = A.size() - 1; i >= 0; i--)
        if(A[i] != B[i]) return A[i] > B[i];
    }
    return true;
}

vector<int> sub(vector<int> &A, vector<int> &B)
{
    vector<int> C;
    for(int i = 0, t = 0; i < A.size(); i++){
        t = A[i] - t;
        if(i < B.size()) t -= B[i];
        C.push_back((t + 10) % 10);
        if(t < 0) t = 1;
        else t = 0;
    }
    while(C.size() > 1 && C.back() == 0) C.pop_back();//去掉前导0
    return C;
}

int main()
{
    vector<int> A, B;
    string a, b;
    cin >> a >> b;
    for(int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
    for(int j = b.size() - 1; j >= 0; j--) B.push_back(b[j] - '0');

    if(cmp(A, B)){
        auto C = sub(A, B);
        for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
    }else{
        auto C = sub(B, A);
        printf("-");
        for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
    }
    return 0;
}


活动打卡代码 AcWing 791. 高精度加法

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
#include <vector>

using namespace std;

vector<int> add(vector<int> &A, vector<int> &B)
{
    vector<int> C;
    int t = 0;
    for(int i = 0; i < A.size() || i < B.size(); i++)
    {
        if(i < A.size()) t += A[i];
        if(i < B.size()) t += B[i];
        C.push_back(t % 10);
        t = t / 10;
    }
    if(t) C.push_back(1);
    return C;
}

int main()
{
    string a, b;
    cin >> a >> b;
    vector<int> A, B;
    for(int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
    for(int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
    auto C = add(A, B);
    for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
    return 0;
}


活动打卡代码 AcWing 790. 数的三次方根

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int flag = 0;
    double n;
    scanf("%lf", &n);
    double l = -10000, r = 10000;
    while(r - l > 1e-8)
    {
        double mid = (l + r) / 2;
        if(mid * mid * mid >= n) r = mid;
        else l = mid;
    }
    printf("%lf", l);
    return 0;
}
//cbrt 开三次方


活动打卡代码 AcWing 789. 数的范围

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
#include <iostream>

using namespace std;

int n, q;
const int N = 100000;
int a[N];

int main()
{
    scanf("%d %d", &n, &q);
    for(int i = 0; i < n; i++) cin >> a[i];
    while(q--)
    {
        int x;
        scanf("%d", &x);

        int l = 0, r = n - 1;
        while(l < r)
        {
            int mid = (l + r) >> 1;
            if(a[mid] >= x) r = mid;
            else l = mid + 1;
        }
        if(a[l] != x) cout << "-1 -1" << endl;
        else
        {
            cout << l << " ";
            int l = 0, r = n - 1;
            while(l < r)
            {
                int mid = (l + r + 1) >> 1;
                if(a[mid] <= x) l = mid;
                else r = mid - 1;
            }
            cout << l << endl;
        }
    }
    return 0;
}