头像

一只菜鸡


访客:16205

离线:1天前


活动打卡代码 AcWing 568. 奇妙的数列

一只菜鸡
1个月前
#include<iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    while (n--) {
        int l, r;
        cin>>l>>r;
        int k = (r - l + 1) / 2;
        int res = 0;
        if (l % 2 == 0) {
            res = -k;
        } else {
            res = k;
        }
        if ((r - l + 1) % 2 == 1) {
            if (r % 2 == 0) res += r;
            else res += -r;
        }
        cout<<res<<endl;
    }
    return 0;
}


活动打卡代码 AcWing 567. 硬币

一只菜鸡
1个月前
#include <iostream>
#include <string>
#include <vector> 
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;
#define all(arr) arr.begin(),arr.end()
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#ifdef ONLINE_JUDGE
#define cerr if (false) cerr
#endif 
#define unique(a)       sort((a).begin(), a.end()), (a).erase(unique((a).begin(), (a).end()),(a).end())
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>void __f(const char* name, Arg1&& arg1){cout<<name<<" : "<<arg1<<"\n";}
template <typename Arg1, typename... Args>void __f(const char* names, Arg1&& arg1, Args&&... args){const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" , ";__f(comma+1, args...);}
const int MOD = 1000000007;
using PII = pair<int, int>;
using LL = long long int;

const int N = 105;
void solve(int n) {

}

// 向上取整
int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin>>n>>m;
    int ans = (m + n - 1) / n;
    cout<<ans<<endl;
}


新鲜事 原文

一只菜鸡
2个月前
cf又挂了,呜呜呜


新鲜事 原文

一只菜鸡
2个月前
@y总,赶紧把@feesf这个毒瘤和他的帖子都封掉吧, 你看他都发了些啥,这种sb是怎么知道acwing的



活动打卡代码 LeetCode 29. 两数相除

一只菜鸡
2个月前
int HALF_INT_MIN = -1073741824;

int divide(int dividend, int divisor) {

    // Special cases: overflow.
    if (dividend == INT_MIN && divisor == -1) {
        return INT_MAX;
    }
    if (dividend == INT_MIN && divisor == 1) {
        return INT_MIN;
    }

    /* We need to convert both numbers to negatives.
     * Also, we count the number of negatives signs. */
    int negatives = 2;
    if (dividend > 0) {
        negatives--;
        dividend = -dividend;
    }
    if (divisor > 0) {
        negatives--;
        divisor = -divisor;
    }

    /* We want to find the largest doubling of the divisor in the negative 32-bit
     * integer range that could fit into the dividend.
     * Note if it would cause an overflow by being less than HALF_INT_MIN,
     * then we just stop as we know double it would not fit into INT_MIN anyway. */
    int maxBit = 0;
    while (divisor >= HALF_INT_MIN && divisor + divisor >= dividend) {
        maxBit += 1;
        divisor += divisor;
    }

    int quotient = 0;
    /* We start from the biggest bit and shift our divisor to the right
     * until we can't shift it any further */
    for (int bit = maxBit; bit >= 0; bit--) {
        /* If the divisor fits into the dividend, then we should set the current
         * bit to 1. We can do this by subtracting a 1 shifted by the appropriate
         * number of bits. */
        if (divisor >= dividend) {
            quotient -= (1 << bit);
            /* Remove the current divisor from the dividend, as we've now
             * considered this part. */
            dividend -= divisor;
        }
        /* Shift the divisor to the right so that it's in the right place
         * for the next positon we're checking at. */
        divisor = (divisor + 1) >> 1;
    }

    /* If there was originally one negative sign, then
     * the quotient remains negative. Otherwise, switch
     * it to positive. */
    if (negatives != 1) {
        quotient = -quotient;
    }
    return quotient;
}


活动打卡代码 LeetCode 28. 实现 strStr()

一只菜鸡
2个月前
class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle == "") return  0;
        for (int i = 0; i < haystack.size(); i++) {
            for (int j = 0;; j++) {
                if (j == needle.size()) {
                    return i;
                }
                if (i + j == haystack.size()) {
                    return -1;
                } 
                if (haystack[i + j] != needle[j]) {
                    break;
                }
            }
        }
        return -1;
    }
};



一只菜鸡
2个月前
/**
 * 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* reverseKGroup(ListNode* head, int k) {
        auto *dummy = new ListNode(-1), *ans = dummy;
        dummy->next = head;
        bool hasK = true;
        auto *prev = dummy;
        while (hasK && dummy->next) {
            for (int i = 0; i < k;  i++) {
                if (!dummy->next) {
                    hasK = false; 
                    break;
                } else {
                    dummy = dummy->next;
                }
            }
            //-1    1   2   3    4 
            //prev  t   f dummy ->  (swap from prev to dummy)
            if (!hasK) {break;}
            auto *temp = prev->next;
            auto *tail = prev->next;
            auto *forward = prev->next->next;
            for (int i = 1; i < k; i++) {
               auto *forwardNext = forward->next;
               prev->next = forward; 
               forward->next = temp;
               forward = forwardNext;
               temp = prev->next;
               tail->next = forwardNext;
            }
            prev = tail;
            dummy = tail;
        } 
        return ans->next; 
    }

};


活动打卡代码 LeetCode 27. 移除元素

一只菜鸡
2个月前
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        if (nums.empty()) return 0;
        int i = 0, j = 0;
        for (; j < nums.size(); j++) {
           if (nums[j] != val) {
               nums[i] = nums[j];
               i++;
           } 
        }
        return i;
    }
};



一只菜鸡
2个月前
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if (nums.empty()) {
            return 0;
        }
        int i = 0, j = 0;
        for (; j < nums.size(); j++) {
            if (nums[j] != nums[i]) {
                i++;
                nums[i] = nums[j];
            }
        }
        nums.resize(i+1);
        return i+1;
    }
};



一只菜鸡
2个月前
/**
 * 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* swapPairs(ListNode* head) {
        auto *dummy = new ListNode(-1), *ans = dummy;
        dummy->next = head;
        //d->1->2 ->3->4
        while (dummy->next && dummy->next->next) {
           auto *temp = dummy->next; 
           dummy->next = dummy->next->next;
           auto * next = dummy->next->next;
           dummy->next->next = temp;
           temp->next = next;
           dummy = temp;
        }
        return ans->next;
    }
};