除法等式(枚举)

这篇博客主要展示了如何使用C++进行数字枚举和分解操作。代码实现了一个功能,即查找两个五位数相乘的结果,使得乘积的每一位数字在原数中各不相同,且0可以重复。通过枚举和判断,找到满足条件的乘积并输出。这是一个涉及数值计算和数组操作的编程问题。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<map>
#include<algorithm>
using namespace std;
int n, t, abcde;
int fghij;
int vis[10],s[6],s1[6];
bool solve(int abcde, int fghij)
{
    memset(vis, 0, sizeof(vis));
    int cnt1 = 5, cnt2 = 5;
    memset(s, 0, sizeof(s));
    memset(s1, 0, sizeof(s1));
    while (abcde != 0)
    {
        s[cnt1] = abcde % 10;
        abcde /= 10;
        if (s[cnt1] == 0)
        {
            cnt1--;//0可以重复
            continue;
        }
        if (vis[s[cnt1]] == 0)
            vis[s[cnt1]]++;
        else//有重复说明不符合
            return false;
        cnt1--;
    }
    
    while (fghij!=0)
    {
        s1[cnt2] = fghij % 10;
        fghij /= 10;
        if (s1[cnt2] == 0)
        {
            cnt2--;
            continue;
        }
        if (vis[s1[cnt2]] == 0)
            vis[s1[cnt2]] ++;
        else
            return false;
        cnt2--;
    }//同理
    return true;
}
void load(int* s, int* s1)
{
    
    for (int i = 1; i <= 5; i++)
    {
        cout << s[i];
    }
    cout << '/';
    for (int i = 1; i <= 5; i++)
    {
        cout << s1[i];
    }
    cout << '=' << n;
}
int main()
{
    cin >> n;
    while (n != 0)
    {
        
        memset(vis, 0, sizeof(vis));
        
        for (fghij = 1; fghij <= 98765; fghij++)
        {
                abcde = fghij * n;
                if (abcde > 98765)
                    break;//超过
                if (solve(abcde, fghij))
                {
                    load(s, s1);
                   
                    cout << endl;
                }
            
           
        }
        
        cout << endl;//每做完一个n都要换行
        cin >> n;
    }

    return 0;
}

直接枚举abcde,然后分解abcde和fghij即可,注意0可以重复,满足就输出。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢林365

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值