C++中的函数——习题2

816. 数组翻转

https://www.acwing.com/problem/content/818/

#include <bits/stdc++.h>
using namespace std;

void reverse(int a[], int size,int n)
{
  for(int i = size; i >= 1; i--)
    cout << a[i] << " ";
  for(int i = size+1; i <= n; i++)
    cout<< a[i] << " ";
}

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

 817. 数组去重

https://www.acwing.com/problem/content/819/

#include <iostream>
using namespace std;
const int N = 1010;
int st[N];

int get_unique_count(int st[], int n)
{
    int ans = 0;
    for (int i = 0; i < N; i ++ ){
        if(st[i] != 0) ans++;
    }
    return ans;
}

int main()
{
    int n,a;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a;
        st[a]++;
    }
    cout << get_unique_count(st,n);
    
    return 0;
}

818. 数组排序

https://www.acwing.com/problem/content/820/ 

#include <bits/stdc++.h>
using namespace std;

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

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;
}

821. 跳台阶

https://www.acwing.com/problem/content/823/

#include <iostream>
using namespace std;

int dfs(int n)
{
    if(n <= 2) return n;
    else return dfs(n-1) + dfs(n-2);
}

int main()
{
    int n;
    cin >> n;
    cout << dfs(n);
    
    return 0;
}

AcWing 822. 走方格

https://www.acwing.com/activity/content/problem/content/2014/ 

#include <iostream>
using namespace std;
int n,m;
int ans;

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

 823. 排列

https://www.acwing.com/problem/content/description/825/

#include <iostream>
#include <cstdio>
using namespace std;

const int N = 10;

int n;
int st[N]; //挖坑,存储数据的坑
int zt[N]; //ture代表这个数已经放了,false代表还没放进去

void dfs(int u)
{
    while(u > n) // 到了边界,输出
    {
        for(int i = 1; i <= n; i++) 
        printf("%d ",st[i]);
        puts("");
        return;
    }
    for (int i = 1; i <= n; i ++ ) // 依次枚举判断是否已经放了这个数
    {
        if(!zt[i]) // 如果为零(false)即没放进去过
        {
            st[u] = i; // 就把这个数存进坑位
            zt[i] = 1; // 更改这个数的状态
            dfs(u + 1); // 然后进行下一个坑位的填补
            zt[i] = 0;  //回溯时归零
        }
    }
}

int main()
{
    cin >> n;
    dfs(1);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永夜天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值