样例都过不去,找不到bug,大佬求调(悬关!!!))

P1618 三连击(升级版)

TripFriend1130 @ 2024-10-06 20:09:37

程序:

#include <iostream>
using namespace std;
int open_nums(int n,int m)
{
    int i = 0,res;
    while(i < m)
    {
        i++;
        res=n%10;
        n/=10;
    }
    return res;
}
int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    if(a==0||b==0||c==0)
    {
        cout << "No!!!";
        return 0;
    }
    bool flag = false;
    bool nums[10] = {true,false,false,false,false,false,false,false,false,false};
    for(int i = 123;i<=987/c*a;i++)
    {
        for(int j = 1;j<4;j++)
        {
            nums[open_nums(i,j)] = true;
        }
        int bi = i/a*b;
        bool f = true;
        for(int j = 1;j<4;j++)
        {
            if(nums[open_nums(bi,j)]==true)
            {
                f = false;
                break;
            }
            nums[open_nums(bi,j)] = true;
        }
        if(f==false)
        {
            continue;
        }
        int ci = i/a*c;
        f = true;
        for(int j = 1;j<4;j++)
        {
            if(nums[open_nums(ci,j)])
            {
                f = false;
                break;
            }
        }
        if(!f)
        {
            continue;
        }
        flag = true;
        cout << i << " " << bi << " " << ci << endl;
        for(int j = 1;j<10;j++)
        {
            nums[j] = false;
        }
    }
    if(!flag)
    {
        cout << "No!!!";
    }
    return 0;
}

帮忙看一下哪里有问题可以吗?谢谢!有答必关!


by LTTXiaochuan @ 2024-10-06 20:33:41

可以参考我的:

#include <bits/stdc++.h>
using namespace std;
int ar[10],book[10],a,b,c,flag;
bool pd(int x,int y,int z)
{
    if(x*1.0/y==a*1.0/b && y*1.0/z==b*1.0/c) return 1;
    else return 0;
}
void dfs(int step)
{
    if(step==10)
    {
        int x,y,z;
        x=ar[1]*100+ar[2]*10+ar[3];
        y=ar[4]*100+ar[5]*10+ar[6];
        z=ar[7]*100+ar[8]*10+ar[9];
        if(pd(x,y,z)==1)
        {
            cout<<x<<" "<<y<<" "<<z<<endl;
            flag=1;
        }
        return;
    }
    for(int i=1; i<=9; i++)
    {
        if(book[i]==0)
        {
            book[i]=1;
            ar[step]=i;
            dfs(step+1);
            book[i]=0;
        }
    }
    return;
}
int main()
{
    cin>>a>>b>>c;
    dfs(1);
    if(flag==0) cout<<"No!!!";
    return 0;
}

应该更易于理解。


|