全排列

P1618 三连击(升级版)

YJLphy @ 2024-03-10 21:38:49

大佬们~有一个样例超时了,咋整
其他样例正确,加了注释
(本人六年级,勿喷)

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

int main()
{
    int A, B, C;
    bool flag = false; // 判断有没有输出过
    cin >> A >> B >> C;
    int a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // 用来组成数的各个数字
    do
    {
        // 位值原理组数
        int num1 = a[0] * 100 + a[1] * 10 + a[2] * 1;
        int num2 = a[3] * 100 + a[4] * 10 + a[5] * 1;
        int num3 = a[6] * 100 + a[7] * 10 + a[8] * 1;

        // 判断是否满足题意,是则输出
        if ((num1 * B / A == num2) && (num1 * C / A == num3) && (num2 * C / B == num3))
        {
            for (int j = 0; j < 3; j++) cout << a[j * 3] << a[j * 3 + 1] << a[j * 3 + 2] << " ";
            cout << endl;
            flag = true;
        }
    }
    while (next_permutation(a, a + 9)); // 全排列
    if (flag == false) cout << "No!!!" << endl;
    return 0;
}

by Zichen123 @ 2024-03-14 21:22:21

我试了一下,是subtask1#1


by Zichen123 @ 2024-03-14 21:22:50

那应该是除0的问题


by Zichen123 @ 2024-03-14 21:23:42

不是TLE啊


by Zichen123 @ 2024-03-14 21:24:13

我试是RE啊


by Zichen123 @ 2024-03-14 21:25:03

对,应该是除以0的问题


by Zichen123 @ 2024-03-14 21:26:16

本人五年级


by Zichen123 @ 2024-03-14 21:26:57

if ((num1 B / A == num2) && (num1 C / A == num3) && (num2 * C / B == num3))

好像错这里了


by Zichen123 @ 2024-03-14 21:27:32

先判定是否是零


by Zichen123 @ 2024-03-14 21:31:25

if (A!=0 && (num1 B / A == num2) && (num1 C / A == num3) && (num2 * C / B == num3))

改成这样


by YJLphy @ 2024-03-15 22:02:14

@Zichen123 噢噢好的,谢谢啦


|