timus#1155. Troubleduons

C语言

#include<stdio.h>

int a[8] = { 0 };

void Move(int x, int y, int t)
{
    while (a[x])
    {
        if (a[t] == 0)
        {
            a[t]++;
            a[y]++;
            printf("%c%c+\n", t + 'A', y + 'A');
        }
        a[t]--;
        a[x]--;
        printf("%c%c-\n", t + 'A', x + 'A');
    }
}

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

    if (a[0] + a[2] + a[5] + a[7] != a[1] + a[3] + a[4] + a[6])   printf("IMPOSSIBLE\n");
    else
    {
        Move(1, 4, 0);
        Move(2, 0, 1);
        Move(3, 4, 0);
        Move(5, 0, 4);
        Move(6, 4, 5);
        Move(7, 0, 4);
        while (a[0]--)  printf("AE-\n");
    }
    return 0;
}

C++

#include<iostream>
#include<vector>

using namespace std;

const int n = 8;
vector<int> a(8);

void Move(int x, int y, int t)
{
    while (a[x])
    {
        if (a[t] == 0)
        {
            a[t]++;
            a[y]++;
            cout << (char)(t + 'A') << (char)(y + 'A') << '+' << endl;
        }
        a[t]--;
        a[x]--;
        cout << (char)(t + 'A') << (char)(x + 'A') << '-' << endl;
    }
}

int main()
{
    int s1 = 0, s2 = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (i == 0 || i == 2 || i == 5 || i == 7)
            s1 += a[i];
        else 
            s2 += a[i];
    }

    if (s1 != s2)   cout <<"IMPOSSIBLE"<< endl; 
    else
    {
        Move(1, 4, 0);
        Move(2, 0, 1);
        Move(3, 4, 0);
        Move(5, 0, 4);
        Move(6, 4, 5);
        Move(7, 0, 4);
        while (a[0]--)  cout << "AE-" << endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叶孤程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值