头像

苍茫得胖帅




离线:15天前


最近来访(0)


C++ 代码

#include <iostream>

using namespace std;
const int N = 1e5 + 10;
int a[N], b[N];

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)scanf("%d", &a[i]);
    for(int i = 0; i < m; i++)scanf("%d", &b[i]);
    int i = 0, j = 0;
    while(i < n && j < m)
    {
        if(a[i] == b[j])i++;
        j++;
    }
    if(i == n)printf("Yes\n");//如果i走到了最后
    else printf("No\n");
    return 0;
}



C++ 代码

#include <iostream>

using namespace std;

const int N = 1e6+10;
int q[N], temp[N];

void merge_sort(int q[], int l, int r)
{
    if(l >= r)return;//如果数组只含有一个或者没有元素直接返回
    int mid = l + r >> 1;
    merge_sort(q, l, mid);merge_sort(q, mid + 1, r);//递归对左右两半数组进行排序
    /*从左右两个排好序的数组头开始,
    同时往后遍历,
    取较小的合并到临时数组中*/
    int i = l, j = mid + 1, k = 0;
    while(i <= mid && j <= r)
    {
        if(q[i] <= q[j])temp[k++] = q[i++];
        else temp[k++] = q[j++];
    }
    //处理左右数组可能有一边有剩余的情况
    while(i <= mid)temp[k++] = q[i++];
    while(j <= r)temp[k++] =q[j++];
    //将临时数组中的元素转移回q数组中
    for(i = 0, j = l; j <= r;)
    {
        q[j++] = temp[i++];
    }
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &q[i]);
    }
    merge_sort(q, 0, n - 1);
    for(int i = 0; i < n; i++)
    {
        printf("%d ", q[i]);
    }
    return 0;
}



C++ 代码

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    double n;
    cin >> n;
    double l = -100, r = 1e5;//r不能选n,如果n为负数,三次方根将不在,[l,r]区间内
    while(r - l > 1e-7)
    {
        double mid = (r + l) / 2 ;
        if(pow(mid, 3) >= n)r = mid;
        else l = mid;
    }
    printf("%.6lf", l);
    return 0;
}



C++ 代码

#include <iostream>

using namespace std;

const int N = 1e6+10;
int q[N], temp[N];

void merge_sort(int q[], int l, int r)
{
    if(l >= r)return;//如果数组只含有一个或者没有元素直接返回
    int mid = l + r >> 1;
    merge_sort(q, l, mid);merge_sort(q, mid + 1, r);//递归对左右两半数组进行排序
    /*从左右两个排好序的数组头开始,
    同时往后遍历,
    取较小的合并到临时数组中*/
    int i = l, j = mid + 1, k = 0;
    while(i <= mid && j <= r)
    {
        if(q[i] <= q[j])temp[k++] = q[i++];
        else temp[k++] = q[j++];
    }
    //处理左右数组可能有一边有剩余的情况
    while(i <= mid)temp[k++] = q[i++];
    while(j <= r)temp[k++] =q[j++];
    //将临时数组中的元素转移回q数组中
    for(i = 0, j = l; j <= r;)
    {
        q[j++] = temp[i++];
    }
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &q[i]);
    }
    merge_sort(q, 0, n - 1);
    for(int i = 0; i < n; i++)
    {
        printf("%d ", q[i]);
    }
    return 0;
}



C++ 代码

#include <iostream>

using namespace std;
const int N = 1e6+10;
int temp[N], q[N];
void merge_sort(int q[], int l, int r)
{
    if(l >= r)return;
    int mid = l + r >> 1;
    merge_sort(q, l, mid);
    merge_sort(q, mid + 1, r);
    int i = l, j = mid + 1, k = 0;
    while(i <= mid && j <= r)
    {
        if(q[i] <= q[j])temp[k++] = q[i++];
        else temp[k++] = q[j++];
    }
    while(i <= mid)temp[k++] = q[i++];
    while(j <= r)temp[k++] = q[j++];
    for(i = 0, j = l; i < k; i++, j++)
    {
        q[j] = temp[i];
    }
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&q[i]);
    }
    merge_sort(q, 0, n - 1);
    for(int i = 0; i < n; i++)printf("%d ",q[i]);
    return 0;
}



C++ 代码

#include <iostream>

using namespace std;
const int N = 1e6+10;
int q[N];

quick_sort(int q[], int l, int r)
{
    if(l >= r)return;#如果数组为空或者,只有一个元素直接返回

    int i = l - 1, j = r + 1, x = q[l + r >> 1];
    #初始化左右指针,i、j各超出边界一步,方便后面先do
    #取中间对比元素x为中间的元素
    while(i < j)
    {
        do i++;while(q[i] < x);
        do j--;while(q[j] > x);
        #一直找到左边比x大(或等于)的数,右边区间比x小(或等于)的数
        if(i < j)swap(q[i], q[j]);#如果此时i,j指针还没有相遇,就交换两个元素
    }

    quick_sort(q, l, j);
    quick_sort(q, j+1, r);
    #再递归对左右排序
}

int main()
{
    int n, k;
    scanf("%d %d",&n, &k);
    quick_sort(q, 0, n - 1);
    printf("%d", q[k - 1]);
    return 0;
}



C++ 代码

#include <iostream>

using namespace std;

const int N = 1e6+10;
int n;
int q[N];

void quick_sort(int q[], int l, int r)
{
    if(l >= r)return;

    int i = l - 1, j = r + 1, x = q[l + r >> 1];//位移运算优先级低于加减
    while(i < j)
    {
        do i++; while(q[i] < x);
        do j--; while(q[j] > x);
        if(i < j)swap(q[i], q[j]);
    }

    quick_sort(q, l, j);
    quick_sort(q, j + 1, r);
}

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    scanf("%d", &q[i]);
    quick_sort(q, 0, n - 1);
    for(int i = 0; i < n; i++)
    printf("%d ", q[i]);
}



#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[n];//存每个数(其实存两个数就行了)
    int max = 0;//存最大差
    for(int i = 0;i < n;i++)
    {
        cin >> a[i];
        int t;
        if(i == 0){
            t = 0;
        }
        else t = abs(a[i] - a[i-1]);
        if(t > max)max = t;
    }
    cout << max << endl;

    return 0;
}



重要思想 利用一个二维数组记录是否图上了颜色

C++ 代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 110;
bool map[N][N];
int main()
{
    int n,x1,x2,y1,y2;
    cin >> n;
    while(n--)//循环n次
    {
        cin >> x1 >> y1 >> x2 >> y2;
        for(int i = x1;i < x2;i++)
        {
            for(int j = y1;j < y2;j++)
            {
                map[i][j] = true;//c++中true小写
            }
        }
    }
    int res = 0;
    for(int i = 0;i < N;i++)
    {
        for(int j = 0;j < N;j++)
        {
            res += (map[i][j] == true);//c++中true小写
        }
    }
    cout << res << endl;
    return 0;
}



Ck n-1 (k-1) (n-k-1)
先分成(0,1),(2,3)两组
第一个数必为2,后面n-1位选k个用来放(0,1),剩下n-k-1个放2,3
k取值2到n-2

C++ 代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;
const int N = 1010, MOD = 1e9 + 7;
int C[N][N];
int main()
{
    int n;
    cin >> n;
    for(int i = 0;i <= n;i++)
    {
        for(int j = 0;j <= i;j++)
        {
            if(!j){
                C[i][j] = 1;
            }
            else {
                C[i][j] = (C[i-1][j] + C[i-1][j-1]) % MOD;
                //Cnm=Cn−1m+Cn−1m−1运用了组合数公式,%MOD为了避免超
            }
        }

    }
    int res = 0;
    for(int k = 2;k <= n-2;k++)
    {
       res =(res + (ll)C[n-1][k] * (k-1) % MOD * (n-k-1)) % MOD;
//       res = res % MOD;
    }
    cout << res <<endl;
    return 0;
}