Tsinsen A1109 三角形

暴力破解算法题解析
本文通过一个具体的算法题目,展示了如何使用C++实现暴力破解的方法。通过对特定条件的检查,找到符合条件的数字排列组合。

http://oj.tsinsen.com/A1109

分析:呃..不知道有没有好的数学搞法,反正直接暴力破解也就那么算了~

代码:

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

int p, a[9], OK, res[9];

int main() {
    scanf("%d", &p);
    for (int i = 0; i < 9; ++i)
        a[i] = i + 1;
    do {
        if (a[0] > a[5] || a[0] > a[8] || a[5] > a[8]) continue;
        if (a[1] > a[3] || a[6] > a[7] || a[2] > a[4]) continue;
        if (a[0] + a[1] + a[3] + a[5] != p) continue;
        if (a[5] + a[6] + a[7] + a[8] != p) continue;
        if (a[8] + a[4] + a[2] + a[0] != p) continue;
        OK += 1;
        if (OK <= 1) for (int i = 0; i < 9; ++i)
            res[i] = a[i];
    } while (next_permutation(a, a + 9));
    if (OK <= 0) puts("NO");
    else printf("%d\n%d\n%d %d\n%d %d\n%d %d %d %d\n",
        OK, res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7], res[8]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值