算法分析与设计练习-1

算法分析与设计练习-1

7-1 汉诺塔问题

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
stack<int>a,b,c;
void hanoi(int num,char from,char to,char end)
{
    if(num == 1)
    cout<<"1: "<<from<<" -> "<<to<<'\n';
    else
    {
        hanoi(num-1,from,end,to);
        cout<<num<<": "<<from<<" -> "<<to<<'\n';
        hanoi(num-1,end,to,from);
    }
}
int main()
{
    cin>>n;
    char a,c,b;
    cin>>a>>c>>b;
    hanoi(n,a,c,b);
}

7-2 逆序对

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int a[N], n;
ll cnt = 0;
int c[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]) c[k++] = q[i++];
        else
        {
            cnt += mid - i + 1; 
            c[k++] = q[j++];
        }
    }
    while (i <= mid) c[k++] = q[i++];
    while (j <= r) c[k++] = q[j++];
    for (int i = l, j = 0; i <= r; i++, j++) q[i] = c[j];
}
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    merge_sort(a, 0, n - 1);
    cout << cnt << endl;
}

7-3 前t个组合结果

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 50;
int a[N];
int n, r, t;
void dfs(int x, int num)
{
    if (!t || r - num > x) return;
    if (x == 0)
    {
        for (int i = n; i >= 1; i--)
            if (a[i]) cout << " " << i;
        cout << endl;
        t--;
        return;
    }
    if (num < r)
    {
        a[x] = 1;
        dfs(x - 1, num + 1);
    }
    a[x] = 0;
    dfs(x - 1, num);
}
int main()
{
    cin >> n >> r >> t;
    dfs(n, 0);
}

7-4 跳马问题

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1010;
int dx[] = {2, 1, -1, -2, -2, -1, 1, 2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
int m, n, k, vis[N][N], a[N][N];
int cnt;
void DFS(int x, int y, int num)
{
    vis[x][y] = num;
    if (num == n * m)
    {
        cnt++;
        if (cnt == k)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= m; j++)
                {
                    cout << vis[i][j];
                    if (j == m)
                        cout << endl;
                    else
                        cout << " ";
                }
            }
            exit(0);
        }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                a[i][j] = vis[i][j];
    }
    for (int i = 0; i < 8; i++)
    {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx <= 0 || nx > n || ny <= 0 || ny > m || vis[nx][ny])
            continue;
        DFS(nx, ny, num + 1);
    }
    vis[x][y] = 0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m >> k;
    DFS(1,2,1);
    if(cnt == k) return 0;
    if (!cnt)
        cout << "impossible";
    else
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (j != 1)
                    cout << ' ';
                cout << a[i][j];
            }
            cout << endl;
        }
    }
}

7-5 加油站之最小加油次数

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1010;
struct gasStation
{
    int len,p;
}a[N];
int n,L,P;
int cmp(gasStation x,gasStation y)
{
    return x.len < y.len;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++) cin>>a[i].len>>a[i].p;
    cin>>L>>P;
    for(int i=0;i<n;i++) a[i].len = L - a[i].len;
    sort(a,a+n,cmp);
    int ans = 0,now = 0;
    queue<int>q;
    for(int i=0;i<n;i++)
    {
        while (P < a[i].len - now)
        {
            if(!q.size())
            {
                cout<<-1<<endl;
                return 0;
            }
            else
            {
                P += q.front();
                q.pop();
                ans ++;
            }
        }
        P -= a[i].len - now;
        now = a[i].len;
        q.push(a[i].p);
    }
    cout<<ans<<endl;
}

7-6 删数问题

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 300;
int n, k;
int a[N];
string s;
int main()
{
    cin >> s >> k;
    n = s.length();
    for (int i = 0; i < n; i++) a[i] = s[i] - '0';
    while (k--)
    {
        int f = 0;
        for (int i = 0; i < n; i++)
        {
            if (a[i] > a[i + 1])
            {
                f = 1;
                for (int j = i; j < n; j++)
                    a[j] = a[j + 1];
            }
            if (f) break;
        }
        n--;
    }
    int flag = 1;
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '0' && i < n - 1 && flag)
            continue;
        else
        {
            cout << a[i];
            flag = 0;
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值