/* XXX
* XX
XXXX
XXXX
XXXXX
18个X均是素数 */
以下为方案五的简化写法
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
int Isprime(int s) //判断s的位数是否满足条件,且其各位数是否为素数
{
int count=0, flag=0;
while (s>10)
{
int t=s%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{
s=s/10;
count++;
}
else
break;
}
if ((s==2) || (s==3) || (s==5) ||(s==7))
count++;
if ((count==4) || (count==5))
flag=1;
return flag;
}
void pp()
{ int i, j, k, m, h, x, n, s, s1, s2, s3, s4, y;
int a[4]={2, 3, 5, 7};
for (i=3; i<=7; i=i+2) //分析知,三位被乘数的最高位不能是2
for (j=0; j<4; j++) // 三位被乘数的中间位可以取2,3,5,7中的任意一个
{
x=a[j];
for (k=3; k<=7; k=k+2) //分析知,三位被乘数的最低位不能是2,不然最终乘积为偶数
{
n=i*100+x*10+k; //找满足条件的三位被乘数n
for (m=3; m<=7; m=m+2) //找满足条件的两位乘数的最低位,不能为2。为2,乘积是偶数
{ s=n*m;
s1=s;
if (Isprime(s1)==1)
{
for (h=3; h<=7; h=h+2) //找满足条件的两位乘数的最高位,不能为2。为2,则最终乘积的最高位上的数为1
{ s2=n*h;
s3=s2;
if (Isprime(s3)==1)
{ y=h*10+m;
s3=s+s2*10;
s4=s3;
if (Isprime(s4)==1)
printf("%3d * %d = %5d\n", n, y, s3);
}
}
}
}
}
}
}
int main( )
{
pp();
system ("pause");
return 0;
}
运行结果:
775*33=25575
#include <stdio.h>
#include <stdlib.h>
方法一:
int main( )
{
int i, j, k, s, s1, s2, s3, t, m, n, x, y, h, count, count1;
int a[4]={2, 3, 5, 7};
int b[20]={0}, c[20]={0};
h=0;
for (i=3; i<=7; i=i+2)
for (j=0; j<4; j++)
{
x=a[j];
for (k=3; k<=7; k=k+2)
{
n=i*100+x*10+k;
for (m=3; m<=7; m=m+2)
{ s=n*m;
s1=s;
count=0;
while (s1>10)
{
t=s1%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{
s1=s1/10;
count++;
}
else
break;
}
if (count==3 && s1<10 && (s1==2) || (s1==3) || (s1==5) ||(s1==7))
{ //printf("%3d %d %4d\n", n, m, s);
b[h]=s; c[h]=m;
printf("Please output b[%d]=%4d c[%d]=%d n=%3d\n", h, b[h], h, c[h], n);
h++;
}
}
}
}
printf("h=%d\n", h);
for (int i=0; i<h; i++)
{
for (int j=0; j<h; j++)
if ((b[i]/c[i])==(b[j]/c[j])) //如果在这里不加语句 if ((b[i]/c[i])==(b[j]/c[j])),就需要在下面求 y 后,加上语句 if (s2%y==0) %%%加在这里比较省时间,可以提高效率
{
s2=b[i]+b[j]*10;
s3=s2;
count1=0;
while (s3>10)
{
t=s3%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{s3=s3/10; count1++;}
else
break;
}
if (count1==4 && s3<10 && (s3==2) || (s3==3) || (s3==5) ||(s3==7))
{
y=c[j]*10+c[i];
//if (s2%y==0) //不加这句会出错,因为b[i]/c[i]不一定等于b[j]/c[j],而我们要保证被乘数是一样的
printf("%4d %4d %2d %5d %3d\n", b[i], b[j], y, s2, s2/y);
}
}
}
system ("pause");
return 0;
}
方法二:
#include <stdio.h>
#include <stdlib.h>
void f(int a1[2])
{
int t;
while (a1[1]>10)
{
t=a1[1]%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{
a1[1]=a1[1]/10;
a1[0]++;
}
else
break;
}
}
int main( )
{
int i, j, x, k, s, s2, m, n, y, h;
int a[4]={2, 3, 5, 7};
int b[20]={0}, c[20]={0};
h=0;
for (i=3; i<=7; i=i+2)
for (j=0; j<4; j++)
{
x=a[j];
for (k=3; k<=7; k=k+2)
{
n=i*100+x*10+k;
for (m=3; m<=7; m=m+2)
{ s=n*m;
int a1[2]={0, s};
f(a1);
if (a1[0]==3 && a1[1]<10 && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
{
b[h]=s; c[h]=m;
h++;
}
}
}
}
for (int i=0; i<h; i++)
{
for (int j=0; j<h; j++)
if ((b[i]/c[i])==(b[j]/c[j]))
{
s2=b[i]+b[j]*10;
int a1[2]={0, s2};
f(a1);
if (a1[0]==4 && a1[1]<10 && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
{
y=c[j]*10+c[i];
printf("%4d %4d %2d %5d %3d %3d %3d\n", b[i], b[j], y, s2, s2/y, b[i]/c[i], b[j]/c[j]);
}
}
}
system ("pause");
return 0;
}
方法三:
#include <stdio.h>
#include <stdlib.h>
void f(int a1[2])
{
int t;
while (a1[1]>10)
{
t=a1[1]%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{
a1[1]=a1[1]/10;
a1[0]++;
}
else
break;
}
}
int main( )
{
int i, j, k, s, s2, s3, m, n, x, y, h;
int a[4]={2, 3, 5, 7};
for (i=3; i<=7; i=i+2)
for (j=0; j<4; j++)
{
x=a[j];
for (k=3; k<=7; k=k+2)
{
n=i*100+x*10+k;
for (m=3; m<=7; m=m+2)
{ s=n*m;
int a1[2]={0, s};
f(a1);
if (a1[0]==3 && a1[1]<10 && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
{ printf("%3d %d %4d\n", n, m, s);
for (h=3; h<=7; h=h+2)
{ s2=n*h;
int a1[2]={0, s2};
f(a1);
if (a1[0]==3 && a1[1]<10 && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
{
y=h*10+m;
s3=s+s2*10;
int a1[2]={0, s3};
f(a1);
if (a1[0]==4 && a1[1]<10 && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
printf("%3d %d %d %2d %4d %4d %5d\n", n, m, h, y, s, s2, s3);
}
}
}
}
}
}
system ("pause");
return 0;
}
方法四:
#include <stdio.h>
#include <stdlib.h>
/* 函数 f 用于判断一个给定的正整数是否是 N 位数,并且是否是素数。若是,则返回1 */
bool f(int a1[2], int N)
{
int t;
while (a1[1]>10)
{
t=a1[1]%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{
a1[1]=a1[1]/10;
a1[0]++;
}
else
break;
}
//a1[0]==N-1 说明初始的 a1[0]是个 N 位数
if ((a1[0]==N-1) && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
return 1;
else
return 0;
}
int main( )
{
int i, j, x, k, s, s2, m, n, y, h;
int a[4]={2, 3, 5, 7}, b[20]={0}, c[20]={0};
/* 用数组 b 记录满足各位数字均是素数的四位数;用 c[i] 记录的是与一个各位都是素数的三位数相乘得到 b[i] 的一位素数 */
bool b1;
h=0;
for (i=3; i<=7; i=i+2)
for (j=0; j<4; j++)
{
x=a[j];
for (k=3; k<=7; k=k+2)
{
n=i*100+x*10+k;
for (m=3; m<=7; m=m+2)
{ s=n*m; //这句后也可以再加一个判断 s 是否是四位数的 语句 if(s>1000)
int a1[2]={0, s};
b1=f(a1, 4);
if (b1)
{
b[h]=s; c[h]=m;
h++;
}
}
}
}
for (int i=0; i<h; i++)
{
for (int j=0; j<h; j++)
if ((b[i]/c[i])==(b[j]/c[j]))
{
s2=b[i]+b[j]*10; //这句后不需要加判断 s2 是否是五位数的语句,鉴于前面对数组 b 中元素的位数的限制,s2必定是五位数
int a1[2]={0, s2};
b1=f(a1, 5);
if (b1)
{
y=c[j]*10+c[i];
printf("%3d %2d %4d %4d %5d\n", b[i]/c[i], y, b[i], b[j], s2);
}
}
}
system ("pause");
return 0;
}
方法五:
#include <stdio.h>
#include <stdlib.h>
int main( )
{
int i, j, k, s, s1, s2, s3, s4, n1, t, m, n, x, y, h, count, count1, count2;
int a[4]={2, 3, 5, 7};
for (i=3; i<=7; i=i+2)
for (j=0; j<4; j++)
{
x=a[j];
for (k=3; k<=7; k=k+2)
{
n=i*100+x*10+k;
for (m=3; m<=7; m=m+2)
{ s=n*m;
s1=s;
count=0;
while (s1>10)
{
t=s1%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{s1=s1/10; count++;}
else
break;
}
if (count==3 && s1<10 && (s1==2) || (s1==3) || (s1==5) ||(s1==7))
{ printf("%3d %d %4d\n", n, m, s);
for (h=3; h<=7; h=h+2)
{ s2=n*h;
s3=s2;
count1=0;
while (s3>10)
{
t=s3%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{s3=s3/10; count1++;}
else
break;
}
if (count1==3 && s3<10 && (s3==2) || (s3==3) || (s3==5) ||(s3==7))
{
y=h*10+m;
s3=s+s2*10;
s4=s3;
count2=0;
while (s4>10)
{
t=s4%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{s4=s4/10; count2++;}
else
break;
}
if (count2==4 && s4<10 && (s4==2) || (s4==3) || (s4==5) ||(s4==7))
printf("%3d %d %d %2d %4d %4d %5d\n", n, m, h, y, s, s2, s3);
}
}
}
}
}
}
system ("pause");
return 0;
}
方法六:
#include <stdio.h>
#include <stdlib.h>
/* 函数 f 用于判断一个给定的 N 位正整数是否是素数。若是,则返回1 */
bool f(int a1[2], int N)
{
int t;
while (a1[1]>10)
{
t=a1[1]%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{
a1[1]=a1[1]/10;
a1[0]++;
}
else
break;
}
//if ((a1[0]==N-1) && a1[1]<10 && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
if ((a1[0]==N-1) && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
return 1;
else
return 0;
}
int main( )
{
int i, j, s, s1, s2;
bool b, b1;
for (i=222; i<=777; i++)
{ int a1[2]={0, i};
b=f(a1, 3);
if(b)
for (j=22; j<=77; j++)
{ int a1[2]={0, j};
b=f(a1, 2);
if(b)
{
s1=i*(j%10);
s2=i*(j/10);
if (s1>1000 && s2>1000) //这个语句也可以不要;要的话,对不满足条件的正整数可以提前跳出循环,减少两次调用操作S1,S2.
{
int a1[2]={0, s1}, a2[2]={0, s2};//
b=f(a1, 4); // S1
b1=f(a2, 4); // S2
if (b && b1)
{
s=i*j;
int a1[2]={0, s};
b=f(a1, 5);
if(b)
printf("%3d %2d %4d %4d %5d\n", i, j, s1, s2, s);
}
}
}
}
}
system ("pause");
return 0;
}
/* XXX*X=XXXX, 8个X均是素数 */
#include <stdio.h>
#include <stdlib.h>
void f(int a1[2])
{
int t;
while (a1[1]>10)
{
t=a1[1]%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{
a1[1]=a1[1]/10;
a1[0]++;
}
else
break;
}
}
int main( )
{
int i, j, x, k, s, m, n;
int a[4]={2, 3, 5, 7};
for (i=3; i<=7; i=i+2)
for (j=0; j<4; j++)
{
x=a[j];
for (k=3; k<=7; k=k+2)
{
n=i*100+x*10+k;
for (m=3; m<=7; m=m+2)
{ s=n*m;
int a1[2]={0, s};
f(a1);
if (a1[0]==3 && a1[1]<10 && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
printf("%3d %d %4d\n", n, m, s);
}
}
}
system ("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
//#define n1=
/* 函数 f 用于判断一个给定的 N 位正整数是否是素数。若是,则返回1 */
bool f(int a1[2], int N)
{
int t;
while (a1[1]>10)
{
t=a1[1]%10;
if ( (t==2) || (t==3) || (t==5) ||(t==7))
{
a1[1]=a1[1]/10;
a1[0]++;
}
else
break;
}
//a1[0]==N-1 说明初始的 a1[0]是个 N 位数
if ((a1[0]==N-1) && (a1[1]==2) || (a1[1]==3) || (a1[1]==5) ||(a1[1]==7))
return 1;
else
return 0;
}
int main( )
{
int i, j, x, k, s, m, n;
int a[4]={2, 3, 5, 7};
bool b;
for (i=3; i<=7; i=i+2)
for (j=0; j<4; j++)
{
x=a[j];
for (k=3; k<=7; k=k+2)
{
n=i*100+x*10+k;
for (m=3; m<=7; m=m+2)
{ s=n*m;
int a1[2]={0, s};
b=f(a1, 4);
if (b)
printf("%3d %d %4d\n", n, m, s);
}
}
}
system ("pause");
return 0;
}
#include <stdlib.h>