[容易] UVa OJ 725 Division 暴力枚举

题目描述

思路:

本题可以通过简单枚举来实现,不过需要注意的是只需要枚举分子或分母即可,无需全部枚举。

具体代码:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int N;
    vector<int> digits;
    bool first=true;
    for(cin>>N;N!=0;cin>>N)
    {
        if(first)
            first=false;
        else
            cout<<endl;
        bool haveAns=false;
        for(int i=0;i<=9;++i)
            for(int j=0;j<=9;++j)
            {
                if(i==j) continue;
                for(int k=0;k<=9;++k)
                {
                    if(k==i||k==j) continue;
                    for(int l=0;l<=9;++l)
                    {
                        if(l==i||l==j||l==k) continue;
                        for(int m=0;m<=9;++m)
                        {
                            if(m==i||m==j||m==k||m==l) continue;
                            else
                            {
                                int t=N*(i*10000+j*1000+k*100+l*10+m);
                                digits.clear();
                                while(t>0)
                                {
                                    digits.push_back(t%10);
                                    t/=10;
                                }
                                if(digits.size()>5)
                                    continue;
                                else if(digits.size()<5)
                                    digits.push_back(0);
                                digits.push_back(i);
                                digits.push_back(j);
                                digits.push_back(k);
                                digits.push_back(l);
                                digits.push_back(m);
                                int occur[10];
                                fill(occur,occur+10,0);
                                bool ok=true;
                                for(int i=0;i<digits.size();++i)
                                {
                                    ++occur[digits[i]];
                                    if(occur[digits[i]]>1)
                                    {
                                        ok=false;
                                        break;
                                    }
                                }
                                if(ok)
                                {
                                    haveAns=true;
                                    for(int i=4;i>=0;--i)
                                    {
                                        cout<<digits[i];
                                    }
                                    cout<<" / ";
                                    cout<<i<<j<<k<<l<<m<<" = "<<N<<endl;
                                }
                            }
                        }
                    }
                }
            }
        if(!haveAns)
        {
            cout<<"There are no solutions for "<<N<<"."<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值