create table o ( code char(1) not null ) insert into o values('+') insert into o values('-') insert into o values('*') insert into o values('/') gocreate table t ( id int not null ) insert into t values(1) insert into t values(2) insert into t values(3) insert into t values(4) insert into t values(5) insert into t values(6) insert into t values(7) insert into t values(8) insert into t values(9) insert into t values(10) go--sql解法 select * from t a, o o1, t b, o o2, t c, o o3, t d where (case o3.code when '+' then (case o2.code when '+' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)+c.id when '-' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)-c.id when '*' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)*c.id when '/' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)/c.id end)+d.id when '-' then (case o2.code when '+' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)+c.id when '-' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)-c.id when '*' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)*c.id when '/' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)/c.id end)-d.id when '*' then (case o2.code when '+' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)+c.id when '-' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)-c.id when '*' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)*c.id when '/' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)/c.id end)*d.id when '/' then (case o2.code when '+' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)+c.id when '-' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)-c.id when '*' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)*c.id when '/' then (case o1.code when '+' then a.id+b.id when '-' then a.id-b.id when '*' then a.id*b.id when '/' then a.id/b.id end)/c.id end)/d.id end)=24
float* o(q1,q2)
{ float r1=q1+q2;
float r2=q1-q2;
float r3=q1*q2;
float r4=q1/q2;
float r[4]={r1,r2,r3,r4}
return r;
}
work()
{ float q1,q2,q3,q4;
for(q1=1; q1<=10;q1++)
{ for(q2=1; q2<=10;q2++)
{ for(q3=1; q3<=10;q3++)
{ for(q4=1; q4<=10;q4++)
{ float *p=o(o(o(q1,q2),q3),q4);
for(int i=0; i<3; i++)
{ if(*(p+i)==24)
{printf("%f,%f,%f,%f",&q1,&q2,&q3,&q4);
}
}
}
}
}
}
}
#include <stdlib.h>
#include <time.h>using namespace std;bool Result(int x1,int x2,int x3,int x4)
{
return ((((x1 + x2 - x3) * x4) == 24)? true : false);
}void main()
{
srand((unsigned)time( NULL ));
int x[4];
for(int i=0; i<sizeof(x)/sizeof(int); i++)
{
int y = rand()%11;
cout<<y<<'\t';;
x[i] = y<1 ? 1:y;
}
if(Result(x[0], x[1], x[2],x[3]))
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
看看这个。
#include <stdlib.h>
#include <time.h>using namespace std;
int work(char op, int x1,int x2)
{
switch(op)
{
case '+':
return x1 + x2;
case '-':
return x1 - x2;
case '*':
return x1 * x2;
case '/':
return x1 / x2;
default:
break;
}
}bool Result(int para[])
{
char sign[4] = {'+','-','*','/'};
char t[3];
char sig[6][3];
for(int i=0; i<4; i++)
{
for(int j=0; j<3; j++)
{
t[j] = sign[(j+i)%4];
//cout<<t[j]<<" ";
}
for(int x=0; x<6; x++)
{
int z=x;
for(int y=0; y<3;y++)
{
sig[x][y] = t[z%3];
x<=2 ? ++z : --z;
//cout<<sig[x][y];
}
}
for(int a=0; a<6; a++)
{
int result = 0;
for(int b=0; b<3; b++)
{
result += work(sig[a][b],para[b],para[b+1]);
cout<<(char)sig[a][b]<<' ';
}
cout<<"r = "<<result<<'\t';
if(result == 24)
return true;
}
}
}
void main()
{
srand((unsigned)time( NULL ));
int x[4];
for(int i=0; i<sizeof(x)/sizeof(int); i++)
{
int y = rand()%11;
cout<<y<<'\t';;
x[i] = y<1 ? 1:y;
} if(Result(x))
{
cout<<"yes"<<endl;
for(int i=0;i<sizeof(x);i++)
cout<<x[i]<<'\t';
}
else
{
cout<<"no"<<endl;
}
}
{
char sign[4] = {'+','-','*','/'};
char t[3];
char sig[6][3];
for(int i=0; i<4; i++)
{
for(int j=0; j<3; j++)
{
t[j] = sign[(j+i)%4];
//cout<<t[j]<<" ";
}
for(int x=0; x<6; x++)
{
int z=x;
for(int y=0; y<3;y++)
{
sig[x][y] = t[z%3];
x<=2 ? ++z : --z;
//cout<<sig[x][y];
}
}
for(int a=0; a<6; a++)
{
int result = 0;
for(int b=0; b<3; b++)
{
result += work(sig[a][b],para[b],para[b+1]);
cout<<(char)sig[a][b]<<' ';
}
cout<<"r = "<<result<<'\t';
if(result == 24)
return true;
}
}
return false; //必须加上这句
}刚才比较仓促,少了一条语句,现加上。
function getRandom(){
return parseInt(Math.random()*10)+1;
}
function getTempResult(v1,v2,oNum){
switch(oNum){
case 0:
return v1+v2;
case 1:
return v1-v2;
case 2:
return v1*v2;
case 3:
if(v1%v2==0) return v1/v2;
default:
break;
}
}
function work(){
var MyArr=new Array(4);
for(var i=0;i<4;i++)
MyArr[i]=getRandom();
for(var a=0;a<4;a++){
for(var b=0;b<4;b++){
if(b!=a){
for(var c=0;c<4;c++){
var tempValue1=getTempResult(MyArr[a],MyArr[b],c);
for(var d=0;d<4;d++){
if(d!=a&&d!=b){
for(var e=0;e<4;e++){
var tempValue2=getTempResult(tempValue1,MyArr[d],e);
for(var f=0;f<4;f++){
if(f!=a&&f!=b&&f!=d&&tempValue1!=tempValue2){
for(var g=0;g<4;g++){
var tempValue=getTempResult(tempValue2,MyArr[f],g);
if(tempValue==24)
return true
}
}
}
}
}
}
}
}
}
}
}
</Script>
[推荐]http://www.csdn.net/develop/Read_Article.asp?Id=20719里面的代码是由 csdn数据算法版牛人 胡海星 所写,非常值得一看 :)
(
code char(1) not null
)
insert into o values('+')
insert into o values('-')
insert into o values('*')
insert into o values('/')
gocreate table t
(
id int not null
)
insert into t values(1)
insert into t values(2)
insert into t values(3)
insert into t values(4)
insert into t values(5)
insert into t values(6)
insert into t values(7)
insert into t values(8)
insert into t values(9)
insert into t values(10)
go--sql解法
select *
from t a, o o1, t b, o o2, t c, o o3, t d
where
(case o3.code
when '+' then
(case o2.code
when '+' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)+c.id
when '-' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)-c.id
when '*' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)*c.id
when '/' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)/c.id
end)+d.id
when '-' then
(case o2.code
when '+' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)+c.id
when '-' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)-c.id
when '*' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)*c.id
when '/' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)/c.id
end)-d.id
when '*' then
(case o2.code
when '+' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)+c.id
when '-' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)-c.id
when '*' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)*c.id
when '/' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)/c.id
end)*d.id
when '/' then
(case o2.code
when '+' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)+c.id
when '-' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)-c.id
when '*' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)*c.id
when '/' then
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)/c.id
end)/d.id
end)=24
1.有重复的,如:
10 + 9 + 1 + 4
9 + 10 + 1 + 4
2.有漏掉的,如:
10/5 * 10 + 4;
10/5 * 9 + 6;
9/3 * 5 + 9;
7*8 / 2 - 4;
............
就是你至少落了*或/与+,与-的组合。
http://expert.csdn.net/Expert/TopicView1.asp?id=2672395
揭帖了
谢谢
呵呵,不知道分析的对不对,请高手们多指教哦~~~,没时间编代码了,下次补上吧~~。