头像

范特西西




离线:16小时前


最近来访(15)
用户头像
小哈皮
用户头像
nmj
用户头像
嗨呀不是我
用户头像
KANO_菓
用户头像
无人圣地
用户头像
zjyyc07

活动打卡代码 AcWing 787. 归并排序

归并排序

#include <iostream>

using namespace std;

const int N = 1e6 + 10;

int n;
int q[N],tmp[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 k = 0,i = l, j = mid + 1;

    while (i <= mid && j <= r)
    {
        if (q[i] <= q[j]) tmp[k ++] = q[i ++];
        else tmp[k ++] = q[j ++];
    }
    while (i <= mid) tmp[k ++] = q[i ++];
    while (j <= r) tmp[k ++] = q[j ++];

    for (i = l,j = 0;i <= r; i ++, j ++) q[i] = tmp[j];

}
int main()
{
    scanf("%d", &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;
}


活动打卡代码 AcWing 786. 第k个数

#include <iostream>

using namespace std;

const int N = 1e6 + 10;

int n,k;
int q[N];

void quick_sort(int q[], int l, int r)
{
    if (l >= r)return ;
    int x = q[l + r >> 1], i = l - 1, j = 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 %d",&n, &k);
    for (int i = 0; i < n; i ++)scanf("%d",&q[i]);

    quick_sort(q, 0, n - 1);

    printf("%d", q[k - 1]);

    return 0;
}


活动打卡代码 AcWing 785. 快速排序

快排算法板子

#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 x = q[l + r >> 1], i = l - 1,j = 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]);
    }

    return 0;
}


活动打卡代码 AcWing 823. 排列

究极函数递归

#include <iostream>

using namespace std;

const int N = 10;
int n;
int q[N];
bool st[N];

void f(int u)
{
    if(u == n)
    {
        for (int i = 0; i < n; i ++)
        {
            cout << q[i] << " ";
        }

        cout  << endl;
        return;
    }
    else
    {
        for (int i = 1;i <= n; i ++)
        {
            if(!st[i])
            {
                q[u] = i;
                st[i] = true;
                f(u + 1);
                st[i] = false;
            }
        }
        return;
    }
}
int main ()
{
    cin >> n;
    f(0);

    return 0;
}


活动打卡代码 AcWing 822. 走方格

十一月九日第五题函数递归

#include <iostream>

using namespace std;

int ans;
int n,m;
void f(int x,int y)
{
    if(x == n && y == m)
    {
        ans ++;
        return;
    }
    if(x < n) f(x + 1,y);
    if(y < m) f(x, y + 1);
}
int main ()
{
    cin >> n >> m;
    f(0,0);
    cout  << ans << endl;

    return 0;
}


活动打卡代码 AcWing 821. 跳台阶

十一月九日第四题函数的递归

#include <iostream>

using namespace std;

int ans;
int k;
int n;
void f(int k)
{
    if(k == n)ans ++;
    else if (k < n)
    {
        f(k + 1);
        f(k + 2);
    }
}

int main ()
{
    cin >> n;
    f(0);
    cout << ans << endl;

    return 0;
}


活动打卡代码 AcWing 818. 数组排序

十一月九日第三题

#include <iostream>

using namespace std;

void sort(int a[],int l,int r)
{
    for (int i = l;i <= r;i ++)
    {
        for (int j = i + 1;j <= r; j ++)
        {
            if(a[j] < a[i])
            swap(a[i], a[j]);
        }
    }
}

int main ()
{
    int n,l,r;
    int a[1000];
    cin >> n >> l >> r;
    for (int i = 0;i < n; i ++)
    {
        cin >> a[i];
    }
    sort(a,l,r);
    for (int i = 0; i < n; i ++)
    {
        cout << a[i] << " ";
    }

    return 0;
}


活动打卡代码 AcWing 817. 数组去重

十一月九日第二题

include [HTML_REMOVED]

using namespace std;

int get_unique_count(int a[],int n)
{
int cnt = 0;
for (int i = 0; i < n; i )
{
bool is_same = false;
for (int j = i - 1;j >= 0;j –)
{
if(a[i] == a[j])
{
is_same = true;
break;
}
}
if(!is_same) cnt
;
}
return cnt;
}
int main ()
{
int n;
int a[1010];
cin >> n;
for (int i = 0;i < n; i ++)
{
cin >> a[i];
}
cout << get_unique_count(a,n) << endl;

return 0;

}



活动打卡代码 AcWing 816. 数组翻转

十一月九日第一题

#include <iostream>

using namespace std;

void reverse(int a[],int size)
{
    for (int i = 0,j = size - 1;i < j;i ++, j --)
    swap(a[i], a[j]);
}

int main ()
{
    int n,size;
    int a[1000];
    cin >> n >> size;
    for (int i = 0; i < n; i ++)
    {
        cin >> a[i];
    }
    reverse(a,size);

    for (int i = 0;i < n; i ++)
    {
        cout << a[i] << " ";
    }

    return 0;
}


活动打卡代码 AcWing 815. 打印字符串

十一月八日第四题

#include <iostream>

using namespace std;

void print(char str[])
{
    for (int i = 0;str[i]!= '\0'; i ++)
    {
        cout << str[i];
    }
}
int main ()
{
    char str[101];
    fgets(str,101,stdin);
    print(str);

    return 0;

}