蓝桥杯---神奇算式

Description

由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。

比如:

6 x 210 = 1260
8 x 473 = 3784
27 x 81 = 2187

都符合要求。

如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,请输出剩余的满足要求的算式且按照第一个因数从小到大排列,第一个因数相等,按照第二个因数从小到大排列

Input

Output

6 x 210 = 1260
8 x 473 = 3784
27 x 81 = 2187

分析:注意看清楚题,题上的一些数式不输出,我已经wrong了很多次,才过去,注意输出的乘号和空格的问题,乘号是小写的x而不是*,这次得长记性啦哭

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
using namespace std;
int a[10],b[10];
int count1,count2;
bool zhuanhua(int a[],int b[])
{
    int flag=1;
    sort(a,a+count1);
    sort(b,b+count2);
    for(int i=0;i<count2;i++)
    {
        if(a[i]==a[i+1])
           {
              for(int j=i;j<count2;j++)
                {
                    a[j]=a[j+1];
                    i--;
                    count2--;
                }
           }
    }
    if(count1!=count2)
        return false;
    for(int i=0;i<count1;i++)
    {
        if(a[i]!=b[i])
        {
            flag=0;
        }
    }
    if(flag)
       {
            return true;
       }
    else
       return false;

}
int main()
{

    int sum;
   for(int i=1;i<10;i++)
   {
       for(int j=100;j<=999;j++)
       {
            sum=i*j;
             count1=0;
             count2=0;
        if(sum<1000||sum>9999||sum==1260||sum==3784)
        {
            continue;
        }
            a[count1++]=i%10;
            a[count1++]=j%10;
            a[count1++]=j/10%10;
            a[count1++]=j/100;

            b[count2++]=sum%10;
            b[count2++]=sum/10%10;
            b[count2++]=sum/100%10;
            b[count2++]=sum/1000;
           if(count1!=count2&&count2!=4)
                continue;
                if(zhuanhua(a,b))
                    printf("%d x %d = %d\n",i,j,sum);
        }
   }
        for(int i=10;i<100;i++)
        {
            for(int j=i;j<=100;j++)
            {
                count1=0;count2=0;
                sum=i*j;
                if(sum<1000|sum>9999||sum==2187)
                    continue;
                     a[count1++]=i%10;
                     a[count1++]=i/10;
                     a[count1++]=j%10;
                     a[count1++]=j/10;
                     b[count2++]=sum%10;
                     b[count2++]=sum/10%10;
                     b[count2++]=sum/100%10;
                     b[count2++]=sum/1000;
                    if(count1!=count2&&count2!=4)
                       continue;
                if(zhuanhua(a,b))
                    printf("%d x %d = %d\n",i,j,sum);
            }
        }
   return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值