UVa725 - Division

本文介绍了一段使用C++编写的代码,该代码旨在找出特定条件下的五位数,这些数字通过乘以某个整数n得到的结果包含十个不同的数字。文章提供了完整的程序代码,并解释了其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
set<int> num[7];
int n,cnt,x,y[7];
bool z;
void judge()
{
    x = n*y[1];
    if(x>98765){
        z = true;
        return;
    }
    num[1].insert(x/10000);
    num[1].insert(x%10000/1000);
    num[1].insert(x%1000/100);
    num[1].insert(x%100/10);
    num[1].insert(x%10);
    if(num[1].size() == 10){
        printf("%05d / %05d = %d\n",x,y[1],n);
        cnt++;
    }
    num[1].clear();
    return;
}
void find(int k){
    for(int i = 0; i < 10; i++){
        y[k] = y[k+1];
        num[k] = num[k+1];
        while(num[k].find(i)!=num[k].end()&&i<9) i++;
        if(num[k].find(i) == num[k].end()){
            y[k]*=10;
            y[k]+=i;
            num[k].insert(i);
            if(k-1) find(k-1);
            else judge();
            if(z) return;
        }
    }
    return;
}
int main()
{
    int t = 0;
    while(cin >> n && n){
        cnt = 0;
        z = false;
        if(t++) cout << endl;
        find(5);
        if(!cnt) cout << "There are no solutions for " << n << "."<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值