头像

孤独与我




离线:1天前


最近来访(5)
用户头像
lixiaoqian
用户头像
皮卡Q
用户头像
只吃虾仁大雪菜
用户头像
1711619194
用户头像
alex2007

活动打卡代码 AcWing 823. 排列

#include <iostream>
#include <cstring>

using namespace std;

const int N = 10;
int n;

void dfs(int u, int nums[], bool st[]);

int main()
{
    cin >> n;

    bool st[N] = {0};
    int nums[N];

    dfs(1, nums, st);

    return 0;
}

void dfs(int u, int nums[], bool st[])
{
    if (u > n){
        for (int i = 1; i <= n; i++) printf("%d ", nums[i]);
        puts("");
    }

    for (int i = 1; i <= n; i ++){
        if (!st[i]){
            nums[u] = i;
            st[i] = true;
            dfs(u + 1, nums, st);
            st[i] = false;
        }
    }
}


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

#include <iostream>

using namespace std;

int settle(int n);

int main()
{
    int n;
    cin >> n;

    cout << settle(n) << endl;

    return 0;
}

int settle(int n)
{
    if (n == 1) return 1;
    if (n == 2) return 2;
    return settle(n - 1) + settle(n - 2);
}


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

#include <iostream>

using namespace std;

void sort(int a[], int l, int r);

int main()
{
    int n, l , r;
    cin >> n >> l >> r;

    int a[n];
    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;
}

void sort(int a[], int l, int r)
{
    for (int i = l; i <= r; i ++){
        int v, j;
        v = a[i];
        j = i - 1;
        while (j >= l && a[j] > v){
            a[j + 1] = a[j];
            j --;
        }
        a[j + 1] = v;
    }
}


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

#include <iostream>
#define N 1005

using namespace std;

int b[N];

int get_unique_count(int a[], int n);

int main()
{
    int n;
    cin >> n;

    int a[n];
    for (int i = 0; i < n; i ++) cin >> a[i];

    cout << get_unique_count(a, n) << endl;

    return 0;
}

int get_unique_count(int a[], int n)
{
    int sum = 0;

    for (int i = 0; i < n; i ++) b[a[i]] ++;

    for (int i = 0; i < N; i ++){
        if (b[i] > 0) sum ++;
    }

    return sum;
}


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

#include <cstdio>

void reverse(int a[], int size);

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

    int a[n];
    for (int i = 0; i < n; i ++) scanf("%d", &a[i]);

    reverse(a, size);

    for (int i = 0; i < n; i ++) printf("%d ", a[i]);

    return 0;
}

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


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

#include <cstdio>

void print(char str[]);

int main()
{
    char str[105];
    fgets(str, 105, stdin);

    print(str);

    return 0;
}

void print(char str[])
{
    printf("%s", str);
}


活动打卡代码 AcWing 814. 复制数组

#include <iostream>

using namespace std;

void copy(int a[], int b[], int size);

int main()
{
    int n, m, size;
    cin >> n >> m >> size;

    int a[n], b[m];
    for (int i = 0; i < n; i ++) scanf("%d", &a[i]);
    for (int i = 0; i < m; i ++) scanf("%d", &b[i]);

    copy(a, b, size);

    for (int i = 0; i < m; i ++) cout << b[i] << ' ';

    return 0;
}

void copy(int a[], int b[], int size)
{
    for (int i = 0; i < size; i ++){
        b[i] = a[i];
    }
}


活动打卡代码 AcWing 809. 最小公倍数

#include <iostream>

using namespace std;

int lcm(int a, int b);

int main()
{
    int a, b;
    cin >> a >> b;
    cout << lcm(a, b) << endl;

    return 0;
}

int lcm(int a, int b)
{
    if (a > b){
        int t = a;
        a = b;
        b = t;
    }

    for (int i = b, j = 2; ; i = b * j, j ++){
        if (i % a == 0) return i;
    }
}


活动打卡代码 AcWing 807. 区间求和

#include <iostream>

using namespace std;

int sum(int l, int r);

int main()
{
    int l, r;
    cin >> l >> r;
    cout << sum(l, r) << endl;

    return 0;
}

int sum(int l, int r)
{
    int sum = 0;
    for (int i = l; i <= r; i ++)
        sum += i;

    return sum;
}


活动打卡代码 AcWing 806. 两个数的和

#include <cstdio>

double add(double x, double y);

int main()
{
    double x, y;
    scanf("%lf%lf", &x, &y);
    printf("%.2lf", add(x, y));

    return 0;
}

double add(double x, double y)
{
    return x + y;
}