小学数学老师想用编程来做这道题不会做
我想做这样一道数学题一个三位数乘一个两位数,结果是一个四位数,上面用到的9个数字是各不相同的,但必须是1~9这9个数,请问这个三位数是几?两位数是几,我想用编的方法来解答这个题.
本人是一名小学教师,水平有限,还望高手写出源代码.
138 * 42 = 5796
157 * 28 = 4396
159 * 48 = 7632
186 * 39 = 7254
198 * 27 = 5346
297 * 18 = 5346
483 * 12 = 5796
#include <vector>
#include <iostream>
#include <algorithm>
int main(int argc, _TCHAR* argv[])
{
std::vector<int> numbers;
numbers.push_back(1);
numbers.push_back(2);
numbers.push_back(3);
numbers.push_back(4);
numbers.push_back(5);
numbers.push_back(6);
numbers.push_back(7);
numbers.push_back(8);
numbers.push_back(9);
do
{
int muler3 = numbers[0] * 100 + numbers[1] * 10 + numbers[2];
int muler2 = numbers[3] * 10 + numbers[4];
int amass = numbers[5] * 1000 + numbers[6] * 100 + numbers[7] * 10 + numbers[8];
if(muler3 * muler2 == amass)
{
std::cout<<muler3<<" * "<<muler2<<" = "<<amass<<std::endl;
}
} while(std::next_permutation(numbers.begin(), numbers.end()));
return 0;
}
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(int argc,int argv[])
{
for(int x=100;x<1000;x++)
for(int y=10;y<100;y++){
int z = x*y;
if( (z>10000)|(z<1000) )
continue;
char sz[16]={0};
snprintf(sz,15,"%d%d%d",x,y,z);
int len = strlen(sz);
if( sz+len != std::find(sz,sz+len,'0'))
continue;
std::sort(sz,sz+len);
if( sz+len != std::adjacent_find(sz,sz+len))
continue;
printf("%d*%d=%d\n",x,y,z);
}
return 0;
}
简单写了一个:
#include <stdio.h>
int a[9]; /* 保存9个位置的数字 */
int count; /* 解的个数统计 */
int hasNum(int num, int dep) /* 判断一个数字是否在前面生成的过程中已经使用过 */
{
int i;
if (num == 0) /* 不允许含0,视为已使用过 */
return 1;
for (i = 0; i < dep && a[i] != num; ++i)
;
return (i < dep);
}
void check() /* 判断生成的被乘数与乘数算出来的解是否满足不重复要求 */
{
int i = 5;
int x = a[0] * 100 + a[1] * 10 + a[2];
int y = a[3] * 10 + a[4];
int z, cz;
z = cz = x * y;
if (z >= 1000 && z < 10000)
{
while (cz)
{
if (hasNum(a[i] = cz % 10, i))
break;
++i;
cz /= 10;
}
if (!cz)
{
printf("%d * %d = %d\n", x, y, z);
++count;
}
}
}
void pl(int dep) /* 生成所有可能的被乘数与乘数的排列,沿有提前剪枝的优化余地 */
{
if (dep < 5)
{
int i;
for (i = 1; i < 10; ++i)
{
if (!hasNum(i, dep))
{
a[dep] = i;
pl(dep + 1);
}
}
}
else
{
check();
}
}
int main()
{
count = 0;
pl(0);
printf("Count: %d\n", count);
return 0;
}
---------------------------------------
运行结果:
138 * 42 = 5796
157 * 28 = 4396
159 * 48 = 7632
186 * 39 = 7254
198 * 27 = 5346
297 * 18 = 5346
483 * 12 = 5796
Count: 7
凑凑热闹...
int CompareNum(int three, int two, int four)
{
int f, t, th;
int ff[4], tt[2], thth[3];
for(int i1=0; i1<4; i1++)
{
f = (four%(int)pow(10, i1+1))/(int)pow(10, i1);
ff[i1] = f;
for(int i2=0; i2<3; i2++)
{
th = (three%(int)pow(10, i2+1))/(int)pow(10, i2);
thth[i2] = th;
for(int i3=0; i3<2; i3++)
{
t = (two%(int)pow(10, i3+1))/(int)pow(10, i3);
tt[i3]=t;
if(f==th || f ==t || th==t)
return 0;
}
}
}
for(int i=0; i<3; i++)
{
for(int j=i+1; j<4; j++)
{
if(ff[i]==ff[j])
return 0;
}
}
for(int i=0; i<2; i++)
{
for(int j=i+1; j<3; j++)
{
if(thth[i]==thth[j])
return 0;
}
}
if(tt[0] == tt[1])
return 0;
return 1;
}
void Test()
{
int three, two, four;
for(int i=102; i<987; i++)
{
three = i;
for(int j=10; j<98; j++)
{
two = j;
four = three*two;
if((four/10000)>0)
break;
else
{
if(CompareNum(three, two, four)==1)
cout<<four<<" "<<three<<" "<<two<<endl;
}
}
}
}
.....................
不好意思,看错题目,,把0也算进去了..
int CompareNum(int three, int two, int four)
{
int f, t, th;
int ff[4], tt[2], thth[3];
for(int i1=0; i1<4; i1++)
{
f = (four%(int)pow(10, i1+1))/(int)pow(10, i1);
if(f==0)
return 0;
ff[i1] = f;
for(int i2=0; i2<3; i2++)
{
th = (three%(int)pow(10, i2+1))/(int)pow(10, i2);
if(th==0)
return 0;
thth[i2] = th;
for(int i3=0; i3<2; i3++)
{
t = (two%(int)pow(10, i3+1))/(int)pow(10, i3);
if(t==0)
return 0;
tt[i3]=t;
if(f==th || f ==t || th==t)
return 0;
}
}
}
for(int i=0; i<3; i++)
{
for(int j=i+1; j<4; j++)
{
if(ff[i]==ff[j])
return 0;
}
}
for(int i=0; i<2; i++)
{
for(int j=i+1; j<3; j++)
{
if(thth[i]==thth[j])
return 0;
}
}
if(tt[0] == tt[1])
return 0;
return 1;
}
void Test()
{
int three, two, four;
for(int i=100; i<1000; i++)
{
three = i;
for(int j=10; j<100; j++)
{
two = j;
four = three*two;
if((four/10000)>0)
break;
else
{
if(CompareNum(three, two, four)==1)
cout<<four<<" "<<three<<" "<<two<<endl;
}
}
}
}
#include <stdio.h>
#include <stdlib.h>
#define n 11
void main()
{
int a[n];
int num1,num2,product,i,j;
for(num1=100;num1<1000;num1++)
{
for(num2=10;num2<100;num2++)
{
a[1]=num1/100;
a[2]=num1/10%10;
a[3]=num1%10;
a[4]=num2/10;
a[5]=num2%10;
product=num1*num2;
a[6]=product/1000%10;
a[7]=product/100%10;
a[8]=product/10%10;
a[9]=product%10;
a[10]=product/10000;
for(j=1;j<n-1;j++)
{
for(i=2;i<=n-j;i++)
{
if(a[i]>a[i-1])
{
a[0]=a[i];
a[i]=a[i-1];
a[i-1]=a[0];
}
}
}
for(i=1;i<n-1;i++)
{
if(a[i+1]==a[i])
break;
}
a[0]=product/10000;
if(a[0]==0)
{
if(i==n-2)
printf("\n%d*%d=%d\n",num1,num2,product);
}
else
{
if(i==n-1)
printf("\n%d*%d=%d\n",num1,num2,product);
}
}
}
}
好想多了点
把积分为4位数或5位数了
#include<iostream>
#include<string>
using namespace std;
int judgebit(int n)
{
if(n<10) return 1;
else return judgebit(n/10)+1;
}
int buffer[10];
int judgediffer(int num)
{
int k=0;
for(int i=num;i;i/=10)
{
for(int j=0;j<k;j++)
if(buffer[j]==i%10)
return 0;
buffer[k++]=i%10;
}
return 1;
}
int arr1[2],arr2[3];int arr3[4];
int judgetwo(int a,int b)
{
if(judgebit(a*b)!=4) return 0;
if(!judgediffer(a)) return 0;
if(!judgediffer(b)) return 0;
if(!judgediffer(a*b)) return 0;
for(int i=0;i<2;i++,a/=10)
{
arr1[i]=a%10;
}
for(int j=0;j<3;j++,b/=10)
{
arr2[j]=b%10;
}
int s=a*b;
for(int t=0;t<4;t++,s/=10)
{
arr3[t]=s%10;
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
if(arr1[i]==arr2[j])
return 0;
for(t=0;t<4;t++)
{
if(arr2[j]==arr3[t])
return 0;
}
if(arr1[i]==arr3[t])
return 0;
}
}
return 1;
}
void main()
{
for(int i=10;i<=99;i++)
{
for(int j=100;j<=999;j++)
{
if(judgetwo(i,j))
{
cout<<i<<"*"<<j<<"="<<i*j;
cout<<endl;
}
}
}
}
忘了把0除去 再发一遍
#include<iostream>
#include<string>
using namespace std;
int judgebit(int n)//判断一个数的位数
{
if(n<10) return 1;
else return judgebit(n/10)+1;
}
int buffer[10];
int judgediffer(int num)//判断该数是否含0或相同数字
{
int k=0;
for(int i=num;i;i/=10)
{
for(int j=0;j<k;j++)
{
if(buffer[j]==i%10)
return 0;
}
if(i%10==0)
return 0;
buffer[k++]=i%10;
}
return 1;
}
int arr1[2];int arr2[3];int arr3[4];int arr[9];int s;
int judgetwo(int a,int b)//判断两个数与乘积中是否有相同的数
{
if(judgebit(a*b)!=4) return 0;
if(!judgediffer(a)) return 0;
if(!judgediffer(b)) return 0;
if(!judgediffer(a*b)) return 0;
s=a*b;
for(int i=0;i<2;i++,a/=10)
{
arr1[i]=a%10;
}
for(int j=0;j<3;j++,b/=10)
{
arr2[j]=b%10;
}
for(int t=0;t<4;t++,s/=10)
{
arr3[t]=s%10;
}
int m=0;
for(i=0;i<2;i++)
arr[m++]=arr1[i];
for(i=0;i<3;i++)
arr[m++]=arr2[i];
for(i=0;i<4;i++)
arr[m++]=arr3[i];
for(i=0;i<m;i++)
for(j=i+1;j<m;j++)
{
if(arr[i]==arr[j])
return 0;
}
return 1;
}
void main()
{
for(int i=10;i<=99;i++)
{
for(int j=100;j<=999;j++)
{
if(judgetwo(i,j))
{
cout<<i<<"*"<<j<<"="<<i*j;
cout<<endl;
}
}
}
}