本文共 30601 字,大约阅读时间需要 102 分钟。
#include <afxinet.h>
#define RECVPACK_SIZE 4096//下载文件并保存为新文件名
//url为网络下载地址,strSaveFile为本地保存文件地址
bool DownloadSaveFiles(TCHAR* url, TCHAR* strSaveFile) { bool ret=false; CInternetSession Sess(_T("lpload")); Sess.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 2000); //2秒的连接超时 Sess.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 2000); //2秒的发送超时 Sess.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 2000); //2秒的接收超时 Sess.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 2000); //2秒的发送超时 Sess.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 2000); //2秒的接收超时 DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD ; CHttpFile* cFile = NULL; char *pBuf = NULL ; int nBufLen = 0 ; do { try{ cFile = (CHttpFile*)Sess.OpenURL(url,1,dwFlag); DWORD dwStatusCode; cFile->QueryInfoStatusCode(dwStatusCode); if(dwStatusCode == HTTP_STATUS_OK) { //查询文件长度 DWORD nLen=0; cFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, nLen); //CString strFilename = GetFileName(url,TRUE); nBufLen=nLen; if(nLen<=0) break; //分配接收数据缓存 pBuf = (char*)malloc(nLen+8); ZeroMemory(pBuf,nLen+8); char *p=pBuf; while(nLen>0) { //每次下载8K int n = cFile->Read(p,(nLen<RECVPACK_SIZE)?nLen:RECVPACK_SIZE); //接收完成退出循环 if (n <= 0) break ; //接收缓存后移 p+= n ; //剩余长度递减 nLen -= n ; } //如果未接收完中断退出 if (nLen != 0) break; //接收成功保存到文件 CFile file(strSaveFile, CFile::modeCreate | CFile::modeWrite); file.Write(pBuf,nBufLen); file.Close(); ret = true; } } catch(...) { break; } }while(0); //释放缓存 if(pBuf) { free(pBuf); pBuf=NULL; nBufLen = 0 ; } //关闭下载连接 if(cFile) { cFile->Close(); Sess.Close(); delete cFile; } return ret; }//24点计算
//定义下面5种运算顺序:
// ((A @ B) @ C) @ D // (A @ B) @ (C @ D) // (A @ (B @ C)) @ D // A @ ((B @ C) @ D) // A @ (B @ (C @ D)) //23.99<计算结果<24.01 //其中: //1≤A、B、C、D≤13 //@为“+、-、*、/”之一 //用浮点数计算,循环遍历以上所有情况即可。 #include <stdio.h> int A,B,C,D,N; float r,r1,r2; char op1,op2,op3; char opc[4]={'+','-','*','/'}; void main() { N=0; for (A=1;A<=13;A++) { for (B=1;B<=13;B++) { for (C=1;C<=13;C++) { for (D=1;D<=13;D++) { for (op1=0;op1<4;op1++) { for (op2=0;op2<4;op2++) { for (op3=0;op3<4;op3++) { // ((A @ B) @ C) @ D r=(float)A; switch (op1) {case 0:r =r +B ;break;case 1:r =r -B ;break;case 2:r =r *B ;break;case 3:r =r /B ;break;} switch (op2) {case 0:r =r +C ;break;case 1:r =r -C ;break;case 2:r =r *C ;break;case 3:r =r /C ;break;} switch (op3) {case 0:r =r +D ;break;case 1:r =r -D ;break;case 2:r =r *D ;break;case 3:r =r /D ;break;} if (23.99f<r && r<24.01f) {N++;printf("%8d: ((%2d%c%2d)%c%2d)%c%2d=24\n",N,A,opc[op1],B,opc[op2],C,opc[op3],D);} // (A @ B) @ (C @ D) r1=(float)A; switch (op1) {case 0:r1=r1+B ;break;case 1:r1=r1-B ;break;case 2:r1=r1*B ;break;case 3:r1=r1/B ;break;} r2=(float)C; switch (op3) {case 0:r2=r2+D ;break;case 1:r2=r2-D ;break;case 2:r2=r2*D ;break;case 3:r2=r2/D ;break;} switch (op2) {case 0:r =r1+r2;break;case 1:r =r1-r2;break;case 2:r =r1*r2;break;case 3:if (-0.01f<r2 && r2<0.01f) goto STEP3; r=r1/r2;break;} if (23.99f<r && r<24.01f) {N++;printf("%8d: (%2d%c%2d)%c(%2d%c%2d)=24\n",N,A,opc[op1],B,opc[op2],C,opc[op3],D);} // (A @ (B @ C)) @ D STEP3: r=(float)B; switch (op2) {case 0:r =r +C ;break;case 1:r =r -C ;break;case 2:r =r *C ;break;case 3:r =r /C ;break;} switch (op1) {case 0:r =A +r ;break;case 1:r =A -r ;break;case 2:r =A *r ;break;case 3:if (-0.01f<r && r <0.01f) goto STEP4; r=A /r ;break;} switch (op3) {case 0:r =r +D ;break;case 1:r =r -D ;break;case 2:r =r *D ;break;case 3:r =r /D ;break;} if (23.99f<r && r<24.01f) {N++;printf("%8d: (%2d%c(%2d%c%2d))%c%2d=24\n",N,A,opc[op1],B,opc[op2],C,opc[op3],D);} // A @ ((B @ C) @ D) STEP4: r=(float)B; switch (op2) {case 0:r =r +C ;break;case 1:r =r -C ;break;case 2:r =r *C ;break;case 3:r =r /C ;break;} switch (op3) {case 0:r =r +D ;break;case 1:r =r -D ;break;case 2:r =r *D ;break;case 3:r =r /D ;break;} switch (op1) {case 0:r =A +r ;break;case 1:r =A -r ;break;case 2:r =A *r ;break;case 3:if (-0.01f<r && r <0.01f) goto STEP5; r=A /r ;break;} if (23.99f<r && r<24.01f) {N++;printf("%8d: %2d%c((%2d%c%2d)%c%2d)=24\n",N,A,opc[op1],B,opc[op2],C,opc[op3],D);} // A @ (B @ (C @ D)) STEP5: r=(float)C; switch (op3) {case 0:r =r +D ;break;case 1:r =r -D ;break;case 2:r =r *D ;break;case 3:r =r /D ;break;} switch (op2) {case 0:r =B +r ;break;case 1:r =B -r ;break;case 2:r =B *r ;break;case 3:if (-0.01f<r && r <0.01f) continue; r=B /r ;break;} switch (op1) {case 0:r =A +r ;break;case 1:r =A -r ;break;case 2:r =A *r ;break;case 3:if (-0.01f<r && r <0.01f) continue; r=A /r ;break;} if (23.99f<r && r<24.01f) {N++;printf("%8d: %2d%c(%2d%c(%2d%c%2d))=24\n",N,A,opc[op1],B,opc[op2],C,opc[op3],D);} } } } } } } } } // 1: (( 1+ 1)+ 1)* 8=24 // 2: ( 1+( 1+ 1))* 8=24 // 3: ( 1+ 1)*( 1+11)=24 // ... ... // 53280: (13+13)/(13/12)=24 // 53281: (13-(13/13))+12=24 // 53282: 13-((13/13)-12)=24//24点计算改良版
#include <stdio.h> float Number[4]; char Option[3]; char OptionList[4] ={ '+', '-', '*', '/'}; int N = 0; float FuncSwitch(float A, int Opt, float B) { float Ret; switch (Opt) { case '+' : Ret = A + B;break; case '-' : Ret = A - B;break; case '*' : Ret = A * B;break; default : Ret = (B > -0.01f && B < 0.01f) ? -12345678.9 : A / B;break; } return Ret; } void FuncCalcul() { float r; r = FuncSwitch(FuncSwitch(FuncSwitch(Number[0], Option[0], Number[1]), Option[1], Number[2]), Option[2], Number[3]); if (23.99f<r && r<24.01f) {N++;printf("%8d: ((%2d%c%2d)%c%2d)%c%2d=24\n",N,(int)Number[0],Option[0],(int)Number[1],Option[1],(int)Number[2],Option[2],(int)Number[3]);} r = FuncSwitch(FuncSwitch(Number[0], Option[0], Number[1]), Option[1], FuncSwitch(Number[2], Option[2], Number[3])); if (23.99f<r && r<24.01f) {N++;printf("%8d: (%2d%c%2d)%c(%2d%c%2d)=24\n",N,(int)Number[0],Option[0],(int)Number[1],Option[1],(int)Number[2],Option[2],(int)Number[3]);} r = FuncSwitch(FuncSwitch(Number[0], Option[0], FuncSwitch(Number[1], Option[1], Number[2])), Option[2], Number[3]); if (23.99f<r && r<24.01f) {N++;printf("%8d: (%2d%c(%2d%c%2d))%c%2d=24\n",N,(int)Number[0],Option[0],(int)Number[1],Option[1],(int)Number[2],Option[2],(int)Number[3]);} r = FuncSwitch(Number[0], Option[0], FuncSwitch(FuncSwitch(Number[1], Option[1], Number[2]), Option[2], Number[3])); if (23.99f<r && r<24.01f) {N++;printf("%8d: %2d%c((%2d%c%2d)%c%2d)=24\n",N,(int)Number[0],Option[0],(int)Number[1],Option[1],(int)Number[2],Option[2],(int)Number[3]);} r = FuncSwitch(Number[0], Option[0], FuncSwitch(Number[1], Option[1], FuncSwitch(Number[2], Option[2], Number[3]))); if (23.99f<r && r<24.01f) {N++;printf("%8d: %2d%c(%2d%c(%2d%c%2d))=24\n",N,(int)Number[0],Option[0],(int)Number[1],Option[1],(int)Number[2],Option[2],(int)Number[3]);} } void FuncOption(int OptionCount) { int i = 0; if(OptionCount == 3) { FuncCalcul(); } else { for (i = 0; i <= 3; i++) { Option[OptionCount] = OptionList[i]; FuncOption(OptionCount + 1); } } } void FuncNumber(int NumberCount) { int i = 0; if (NumberCount == 4) { FuncOption(0); } else { for (i = 1; i <= 13; i++) { Number[NumberCount] = (float)i; FuncNumber(NumberCount + 1); } } } int main() { FuncNumber(0); return 0; }//C++11版的
#include<stdio.h>
#include<algorithm> #include<Functional> using namespace std; typedef std::pair<function<float(float,float)>,char> Type; Type f[4] = { Type([](float x,float y ){return x+y;},'+'), Type([](float x,float y ){return x-y;},'-') ,Type([](float x,float y ){return x*y;},'*') , Type([](float x,float y ){return x/y;},'/') }; float Num[4] = { 3 , 3 , 8 , 8 };//答案为 8/(3-8/3) ; int main( int argc, char* argv[] ) { sort( Num , Num + 4 ); do { for_each( f , f+4 , [=](Type x) { for_each( f,f+4, [=](Type y) { for_each( f,f+4, [=](Type z) { float sum = z.first( y.first( x.first( Num[0], Num[1] ) , Num[2] ) , Num[3] ); if( ( sum > 23.9 ) && ( sum < 24.1 ) ) { printf( "((%d%c%d)%c%d)%c%d", ( int )Num[0], x.second , ( int )Num[1], y.second , ( int )Num[2], z.second, ( int )Num[3] ); exit(0); } sum = z.first( x.first( Num[0], Num[1] ) , y.first( Num[2] , Num[3] ) ); if( ( sum > 23.9 ) && ( sum < 24.1 ) ) { printf( "(%d%c%d)%c(%d%c%d)", ( int )Num[0], x.second , ( int )Num[1], z.second , ( int )Num[2], y.second, ( int )Num[3] ); exit(0); } sum = z.first( Num[0] , y.first( Num[1] , x.first( Num[2], Num[3] ) ) ); if( ( sum > 23.9 ) && ( sum < 24.1 ) ) { printf( "%d%c(%d%c(%d%c%d))", ( int )Num[0], z.second , ( int )Num[1], y.second , ( int )Num[2], x.second, ( int )Num[3] ); exit(0); } } ); } ); } ); }while( next_permutation( Num , Num + 4 ) ); return 0; }//
//isnan未定义
/************************************** * * 定义下面5种运算顺序: * ( ( A @ B ) @ C ) @ D * ( A @ B ) @ ( C @ D ) * ( A @ ( B @ C ) ) @ D * A @ ( ( B @ C ) @ D ) * A @ ( B @ ( C @ D ) ) * 其中: * A, B, C, D在区间[ 1, 13 ]上 * @为[ +, -, *, / ] * 23.99 < 计算结果 < 24.01 * **************************************/ #include <stdio.h> #include <stdlib.h> #include <stdint.h> //#include <stdbool.h> #include <string.h> #include <math.h> #define RESULT_MIN 23.99f #define RESULT_MAX 24.01f #define OP_NUM 3 #define VAR_NUM 4 #define VAR_MIN 1 #define VAR_MAX 13 enum Operator { op_add, op_sub, op_mul, op_div, op_system }; const char op_to_char[] = { '+', '-', '*', '/' }; inline bool between( float num, float max, float min ) { return ( isnan( num ) ? false : ( num > max ? false : ( num < min ? false : true ) ) ); } inline float compute( float num1, float num2, enum Operator op ) { switch ( op ) { case op_add: return num1 + num2; case op_sub: return num1 - num2; case op_mul: return num1 * num2; case op_div: return num1 / num2; // this is a bug default: exit( 1 ); break; } } inline bool number_system_inc( uint8_t *first, uint8_t *last, int system ) { uint8_t *iterator; if ( ++( *last ) >= system ) { iterator = last; do { *iterator = 0; if ( --iterator < first ) { return false; } } while ( ( *iterator += 1 ) >= system ); } return true; } int main( int argc, char **argv ) { uint8_t op_iterator_first[ OP_NUM ]; uint8_t *op_iterator_last = op_iterator_first + OP_NUM - 1; uint8_t var_iterator_first[ VAR_NUM ]; uint8_t *var_iterator_last = var_iterator_first + VAR_NUM - 1; int counter = 0; #ifdef FILE FILE *fp = fopen( "list.txt", "w" ); #endif // fill zero memset( op_iterator_first, 0, sizeof( op_iterator_first ) ); while ( true ) { // fill zero memset( var_iterator_first, 0, sizeof( var_iterator_first ) ); while ( true ) { float A = ( float )var_iterator_first[ 0 ] + 1.0f; float B = ( float )var_iterator_first[ 1 ] + 1.0f; float C = ( float )var_iterator_first[ 2 ] + 1.0f; float D = ( float )var_iterator_first[ 3 ] + 1.0f; float R = 0.0f; #define U( index ) op_to_char[ op_iterator_first[ index ] ] #ifdef FILE #define P( text ) fprintf( fp, text "=%.0f\n", ( int )A, U( 0 ), ( int )B, U( 1 ), ( int )C, U( 2 ), ( int )D, R ); counter++ #else #define P( text ) printf( text "=%.0f\n", ( int )A, U( 0 ), ( int )B, U( 1 ), ( int )C, U( 2 ), ( int )D, R ); counter++ #endif #define _( num1, num2, index ) compute( num1, num2, op_iterator_first[ index ] ) // ((A@B)@C)@D if ( between( R = _( _( _( A, B, 0 ), C, 1 ), D, 2 ) , RESULT_MAX, RESULT_MIN ) ) { P( "((%d%c%d)%c%d)%c%d" ); } // (A@B)@(C@D) if ( between( R = _( _( A, B, 0 ), _( C, D, 2 ), 1 ) , RESULT_MAX, RESULT_MIN ) ) { P( "(%d%c%d)%c(%d%c%d)" ); } // (A@(B@C))@D if ( between( R = _( _( A, _( B, C, 1 ), 0 ), D, 2 ) , RESULT_MAX, RESULT_MIN ) ) { P( "(%d%c(%d%c%d))%c%d" ); } // A@((B@C)@D) if ( between( R = _( A, _( _( B, C, 1 ), D, 2 ), 0 ) , RESULT_MAX, RESULT_MIN ) ) { P( "%d%c((%d%c%d)%c%d)" ); } // A@(B@(C@D)) if ( between( R = _( A, _( B, _( C, D, 2 ), 1 ), 0 ) , RESULT_MAX, RESULT_MIN ) ) { P( "%d%c(%d%c(%d%c%d))" ); } #undef U #undef _ if ( number_system_inc( var_iterator_first, var_iterator_last, VAR_MAX - VAR_MIN + 1 ) == false ) { break; } } if ( number_system_inc( op_iterator_first, op_iterator_last, op_system ) == false ) { break; } } printf( "solve total: %d\n", counter ); #ifdef FILE fclose( fp ); #endif return 0; }//C/C++ code
#include <cstdio> #include <algorithm> using namespace std; int main(int argc, char* argv[]) { using Ft = float; using Op = Ft(*)(Ft, Ft); pair<Op, char> f[] = { {[](Ft x, Ft y) {return x + y;}, '+'}, {[](Ft x, Ft y) {return x - y;}, '-'}, {[](Ft x, Ft y) {return x * y;}, '*'}, {[](Ft x, Ft y) {return x / y;}, '/'}, }; pair<Ft(*)(Ft, Ft, Ft, Ft, Op, Op, Op), const char*> g[] = { {[](Ft a, Ft b, Ft c, Ft d, Op x, Op y, Op z) {return z(y(x(a, b), c), d);}, "%8d: ((%2d%c%2d)%c%2d)%c%2d=24\n"}, {[](Ft a, Ft b, Ft c, Ft d, Op x, Op y, Op z) {return y(x(a, b), z(c, d));}, "%8d: (%2d%c%2d)%c(%2d%c%2d)=24\n"}, {[](Ft a, Ft b, Ft c, Ft d, Op x, Op y, Op z) {return z(x(a, y(b, c)), d);}, "%8d: (%2d%c(%2d%c%2d))%c%2d=24\n"}, {[](Ft a, Ft b, Ft c, Ft d, Op x, Op y, Op z) {return x(a, z(y(b, c), d));}, "%8d: %2d%c((%2d%c%2d)%c%2d)=24\n"}, {[](Ft a, Ft b, Ft c, Ft d, Op x, Op y, Op z) {return x(a, y(b, z(c, d)));}, "%8d: %2d%c(%2d%c(%2d%c%2d))=24\n"}, }; int count = 0; Ft r[13]{}; generate(begin(r), end(r), [=]()mutable{return Ft(++count);}); for (auto a : r) for (auto b : r) for (auto c : r) for (auto d : r) for (auto x : f) for (auto y : f) for (auto z : f) for (auto u : g) { Ft sum = u.first(a, b, c, d, x.first, y.first, z.first); if (sum > 23.99 && sum < 24.01) { printf(u.second, ++count, (int)a, x.second, (int)b, y.second, (int)c, z.second, (int)d); } } return 0; } //Haskell 版本:Python codeimport Text.Printf
type Operand a = (String, a -> a -> a) opts = [ ("+",(+)), ("-",(-)), ("*",(*)), ("/",(/)) ] :: [Operand Float] grps :: [Operand Float] -> [Float] -> [(String, Float)] grps [(nx,x),(ny,y),(nz,z)] [a,b,c,d] = [ ( printf "((%2.0f%s%2.0f)%s%2.0f)%s%2.0f" a nx b ny c nz d, ((a `x` b) `y` c) `z` d ), ( printf "(%2.0f%s%2.0f)%s(%2.0f%s%2.0f)" a nx b ny c nz d, (a `x` b) `y` (c `z` d) ), ( printf "(%2.0f%s(%2.0f%s%2.0f))%s%2.0f" a nx b ny c nz d, (a `x` (b `y` c))`z` d ), ( printf "%2.0f%s((%2.0f%s%2.0f)%s%2.0f)" a nx b ny c nz d, a `x`((b `y` c) `z` d) ), ( printf "%2.0f%s(%2.0f%s(%2.0f%s%2.0f))" a nx b ny c nz d, a `x` (b `y` (c `z` d)) ) ] target = 24 :: Int input = [1..13] :: [Int] check d v = dd - epsilon < v && v < dd + epsilon where epsilon = 0.01 :: Float dd = fromIntegral d results = filter (check target . snd) [r|v<-vs,o<-os,r<-grps o v] where ns = map fromIntegral input :: [Float] vs = [[a,b,c,d]|a<-ns,b<-ns,c<-ns,d<-ns] os = [[x,y,z]|x<-opts,y<-opts,z<-opts] putResult :: (String, Float) -> IO () putResult = uncurry $ printf "%s = %2.0f\n"//Java code
public class Operator {
private static float MIN_NUMBER = 23.99f;
private static float MAX_NUMBER = 24.01f;
private static char[] OPERS = { '+', '-', '*', '/' };
public static void main(String[] args) {
// 0至13
int a, b, c, d;
// 0:+; 1:-; 2:*; 3:/
int oper1, oper2, oper3;
int count = 0;
float r1, r2, r3;
String message;
for (a = 0; a <= 13; a++) {
for (b = 0; b <= 13; b++) {
for (c = 0; c <= 13; c++) {
for (d = 0; d <= 13; d++) {
for (oper1 = 0; oper1 <= 3; oper1++) {
for (oper2 = 0; oper2 <= 3; oper2++) {
for (oper3 = 0; oper3 <= 3; oper3++) {
// ((A @ B) @ C) @ D
try {
r1 = calculate(a, b, oper1);
r2 = calculate(r1, c, oper2);
r3 = calculate(r2, d, oper3);
if (r3 > MIN_NUMBER && r3 < MAX_NUMBER) {
count++;
message = String
.format("%d: ((%d %s %d) %s %d) %s %d",
count, a,
OPERS[oper1], b,
OPERS[oper2], c,
OPERS[oper3], d);
System.out.println(message);
}
} catch (Exception e) {
}
// (A @ B) @ (C @ D)
try {
r1 = calculate(a, b, oper1);
r2 = calculate(c, d, oper3);
r3 = calculate(r1, r2, oper2);
if (r3 > MIN_NUMBER && r3 < MAX_NUMBER) {
count++;
message = String
.format("%d: (%d %s %d) %s (%d %s %d)",
count, a,
OPERS[oper1], b,
OPERS[oper2], c,
OPERS[oper3], d);
System.out.println(message);
}
} catch (Exception e) {
}
// (A @ (B @ C)) @ D
try {
r1 = calculate(b, c, oper2);
r2 = calculate(a, r1, oper1);
r3 = calculate(r2, d, oper3);
if (r3 > MIN_NUMBER && r3 < MAX_NUMBER) {
count++;
message = String
.format("%d: (%d %s (%d %s %d)) %s %d",
count, a,
OPERS[oper1], b,
OPERS[oper2], c,
OPERS[oper3], d);
System.out.println(message);
}
} catch (Exception e) {
}
// A @ ((B @ C) @ D)
try {
r1 = calculate(b, c, oper2);
r2 = calculate(r1, d, oper3);
r3 = calculate(a, r2, oper1);
if (r3 > MIN_NUMBER && r3 < MAX_NUMBER) {
count++;
message = String
.format("%d: %d %s ((%d %s %d) %s %d)",
count, a,
OPERS[oper1], b,
OPERS[oper2], c,
OPERS[oper3], d);
System.out.println(message);
}
} catch (Exception e) {
}
// A @ (B @ (C @ D))
try {
r1 = calculate(c, d, oper3);
r2 = calculate(b, r1, oper2);
r3 = calculate(a, r2, oper1);
if (r3 > MIN_NUMBER && r3 < MAX_NUMBER) {
count++;
message = String
.format("%d: %d %s (%d %s (%d %s %d))",
count, a,
OPERS[oper1], b,
OPERS[oper2], c,
OPERS[oper3], d);
System.out.println(message);
}
} catch (Exception e) {
}
}
}
}
}
}
}
}
}
private static float calculate(float a, float b, int oper) {
float result = 0;
switch (oper) {
case 0:
result = a + b;
break;
case 1:
result = a - b;
break;
case 2:
result = a * b;
break;
case 3:
result = a / b;
break;
}
return result;
}
}
//Java code
object TwentyFour {
def operate(a: Double, op: String, b: Double): Double = op match { case "+" => a + b case "-" => a - b case "*" => a * b case "/" => a / b } val funcs = List( (n1: Double, op1: String, n2: Double, op2: String, n3: Double, op3: String, n4: Double) => { ("((" + n1 + op1 + n2 + ")" + op2 + n3 + ")" + op3 + n4, operate(operate(operate(n1, op1, n2), op2, n3), op3, n4)) }, (n1: Double, op1: String, n2: Double, op2: String, n3: Double, op3: String, n4: Double) => { ("(" + n1 + op1 + n2 + ")" + op2 + "(" + n3 + op3 + n4 + ")", operate(operate(n1, op1, n2), op2, operate(n2, op3, n4))) }, (n1: Double, op1: String, n2: Double, op2: String, n3: Double, op3: String, n4: Double) => { ("(" + n1 + op1 + "(" + n2 + op2 + n3 + "))" + op3 + n4, operate(operate(n1, op1, operate(n2, op2, n3)), op3, n4)) }, (n1: Double, op1: String, n2: Double, op2: String, n3: Double, op3: String, n4: Double) => { (n1 + op1 + "((" + n2 + op2 + n3 + ")" + op3 + n4 + ")", operate(n1, op1, operate(operate(n2, op2, n3), op3, n4))) }, (n1: Double, op1: String, n2: Double, op2: String, n3: Double, op3: String, n4: Double) => { (n1 + op1 + "(" + n2 + op2 + "(" + n3 + op3 + n4 + "))", operate(n1, op1, operate(n2, op2, operate(n3, op3, n4)))) }) def main(args: Array[String]) { val ns = 1 to 13; val ops = Array("+", "-", "*", "/") for (a <- ns) for (op1 <- ops) for (b <- ns) for (op2 <- ops) for (c <- ns) for (op3 <- ops) for (d <- ns) for (func <- funcs) { val e = func(a, op1, b, op2, c, op3, d) if (e._2 > 23.99 && e._2 < 24.01) println(e._1) } } }//Java
public static void main(String[] args) { int[] o=new int[]{1,1,1,1}; char[] op=new char[]{'+','-','*','/'}; int e=1,f=1,g=1,sumTimes=0; double h=0,i=0; while(o[0]<14){o[1]=1; while(o[1]<14){o[2]=1; while(o[2]<14){o[3]=1; while(o[3]<14){e=1; while(e<5){f=1; while(f<5){g=1; while(g<5){ //((A @ B) @ C) @ D h=0; switch(e){case 1:h=o[0]+o[1];break;case 2:h=o[0]-o[1];break;case 3:h=o[0]*o[1];break;case 4:h=o[0]/o[1];break;} switch(f){case 1:h=h+o[2];break;case 2:h=h-o[2];break;case 3:h=h*o[2];break;case 4:h=h/o[2];break;} switch(g){case 1:h=h+o[3];break;case 2:h=h-o[3];break;case 3:h=h*o[3];break;case 4:h=h/o[3];break;} if(h>=23.99&h<=24.01){sumTimes++; System.out.println(sumTimes+" 公式1:(("+(int)o[0]+op[e-1]+(int)o[1]+")"+op[f-1]+(int)o[2]+")"+op[g-1]+(int)o[3]); } h=0; //(A @ B) @ (C @ D) switch(e){case 1:h=o[0]+o[1];break;case 2:h=o[0]-o[1];break;case 3:h=o[0]*o[1];break;case 4:h=o[0]/o[1];break;} switch(f){case 1:i=o[2]+o[3];break;case 2:i=o[2]-o[3];break;case 3:i=o[2]*o[3];break;case 4:i=o[2]/o[3];break;} switch(g){case 1:h=h+i;break;case 2:h=h-i;break;case 3:h=h*i;break;case 4:h=(i==0?0.0001:h/i);break;} if(h>=23.99&h<=24.01){sumTimes++; System.out.println(sumTimes+" 公式2:("+(int)o[0]+op[e-1]+(int)o[1]+")"+op[g-1]+"("+(int)o[2]+op[f-1]+(int)o[3]+")"); } h=0; //(A @ (B @ C)) @ D switch(e){case 1:h=o[1]+o[2];break;case 2:h=o[1]-o[2];break;case 3:h=o[1]*o[2];break;case 4:h=o[1]/o[2];break;} switch(f){case 1:h=o[0]+h;break;case 2:h=o[0]-h;break;case 3:h=o[0]*h;break;case 4:h=(h==0?0.0001:o[0]/h);break;} switch(g){case 1:h=h+o[3];break;case 2:h=h-o[3];break;case 3:h=h*o[3];break;case 4:h=(h==0?0.0001:h/o[3]);break;} if(h>=23.99&h<=24.01){ sumTimes++; System.out.println(sumTimes+" 公式3:("+(int)o[0]+op[f-1]+"("+(int)o[1]+op[e-1]+(int)o[2]+"))"+op[g-1]+(int)o[3]); } h=0; //A @ ((B @ C) @ D) switch(e){ case 1:h=o[1]+o[2];break;case 2:h=o[1]-o[2];break;case 3:h=o[1]*o[2];break;case 4:h=o[1]/o[2];break;} switch(f){case 1:h=h+o[3];break;case 2:h=h-o[3];break;case 3:h=h*o[3];break;case 4:h=(h==0?0.0001:h/o[3]);break;} switch(g){case 1:h=o[0]+h;break;case 2:h=o[0]-h;break;case 3:h=o[0]*h;break;case 4:h=(h==0?0.0001:o[0]/h);break;} if(h>=23.99&h<=24.01){sumTimes++; System.out.println(sumTimes+" 公式4:"+(int)o[0]+op[g-1]+"(("+(int)o[1]+op[e-1]+(int)o[2]+")"+op[f-1]+(int)o[3]+")"); } //A @ (B @ (C @ D)) h=0; switch(e){case 1:h=o[2]+o[3];break;case 2:h=o[2]-o[3];break;case 3:h=o[2]*o[3];break;case 4:h=o[2]/o[3];break;} switch(f){case 1:h=o[1]+h;break;case 2:h=o[1]-h;break;case 3:h=o[1]*h;break;case 4:h=(h==0?0.0001:o[1]/h);break;} switch(g){case 1:h=o[0]+h;break;case 2:h=o[0]-h;break;case 3:h=o[0]*h;break;case 4:h=(h==0?0.0001:o[0]/h);break;} if(h>=23.99&h<=24.01){sumTimes++; System.out.println(sumTimes+" 公式5:"+(int)o[0]+op[g-1]+"("+(int)o[1]+op[f-1]+"("+(int)o[2]+op[e-1]+(int)o[3]+"))"); } h=0; g++; } f++; } e++; } o[3]++; } o[2]++; } o[1]++; } o[0]++; } }//C# code
namespace MyConsoleLab
{ class Program { delegate float Operation(float num1,float num2); static Dictionary<int, Operation> Opera = new Dictionary<int, Operation>() { { 0,(x, y) => x + y }, { 1,(x, y) => x - y }, { 2,(x, y) => x * y }, { 3,(x, y) => x / y } }; static Dictionary<int, string> Translate = new Dictionary<int, string>() { {0,"+" },{1,"-" },{2,"*" },{3,"/" } }; static int count = 0; static void Main() { ForeachNumbers(); Console.WriteLine("program completed! {0} results!",count); Console.ReadKey(); } private static void ForeachNumbers() { for (int num1 = 1; num1 <= 13; num1++) { for (int num2 = 1; num2 <= 13; num2++) { for (int num3 = 1; num3 <= 13; num3++) { for (int num4 = 1; num4 <= 13; num4++) { PrintResult(num1, num2, num3, num4); } } } } } private static void PrintResult(int num1, int num2, int num3, int num4) { for (int op1 = 0; op1 < 4; op1++) { for (int op2 = 0; op2 < 4; op2++) { for (int op3 = 0; op3 < 4; op3++) { //((A @ B) @ C) @ D if (Opera[op3](Opera[op2](Opera[op1](num1, num2), num3), num4) == 24) { Console.WriteLine("(({0} {4} {1}) {5} {2}) {6} {3}", num1, num2, num3, num4, Translate[op1], Translate[op2], Translate[op3]); count++; } //(A @ B) @ (C @ D) if (Opera[op2](Opera[op1](num1, num2), Opera[op3](num3,num4)) == 24) { Console.WriteLine("({0} {4} {1}) {5} ({2} {6} {3})", num1, num2, num3, num4, Translate[op1], Translate[op2], Translate[op3]); count++; } //(A @ (B @ C)) @ D if (Opera[op3](Opera[op1](num1, Opera[op2](num2, num3)), num4) == 24) { Console.WriteLine("({0} {4} ({1} {5} {2}) {6} {3}", num1, num2, num3, num4, Translate[op1], Translate[op2], Translate[op3]); count++; } //A @ ((B @ C) @ D) if (Opera[op1](num1,Opera[op3](Opera[op2](num2, num3), num4)) == 24) { Console.WriteLine("{0} {4} (({1} {5} {2}) {6} {3})", num1, num2, num3, num4, Translate[op1], Translate[op2], Translate[op3]); count++; } //A @ (B @ (C @ D)) if (Opera[op3](num1,Opera[op2](num2,Opera[op3](num3, num4))) == 24) { Console.WriteLine("{0} {4} ({1} {5} ({2} {6} {3}))", num1, num2, num3, num4, Translate[op1], Translate[op2], Translate[op3]); count++; } } } } } } } //Python code from __future__ import division operator = ('+', '-', '*', '/') N = 1 def factors(): for A in xrange(1, 14): for B in xrange(1, 14): for C in xrange(1, 14): for D in xrange(1, 14): yield A, B, C, D def opers(): for op1 in xrange(0, 4): for op2 in range(0, 4): for op3 in range(0, 4): yield operator[op1], operator[op2], operator[op3] def check_res(fmt, A, B, C, D, op1, op2, op3): global N sumexp = fmt % (A, op1, B, op2, C, op3, D) try: sum = eval(sumexp) except ZeroDivisionError: return if sum > 23.99 and sum < 24.01: print N, sumexp, "=24" N += 1 def main(): fact = factors() for A, B, C, D in fact: oper = opers() for op1, op2, op3 in oper: # ((A @ B) @ C) @ D check_res("((%d%s%d)%s%d)%s%d", A, B, C, D, op1, op2, op3) # (A @ B) @ (C @ D) check_res("(%d%s%d)%s(%d%s%d)", A, B, C, D, op1, op2, op3) # (A @ (B @ C)) @ D check_res("(%d%s(%d%s%d))%s%d", A, B, C, D, op1, op2, op3) # A @ ((B @ C) @ D) check_res("%d%s((%d%s%d)%s%d)", A, B, C, D, op1, op2, op3) # A @ (B @ (C @ D)) check_res("%d%s(%d%s(%d%s%d))", A, B, C, D, op1, op2, op3) if __name__ == "__main__": main() //沾了动态语言eval的光啊.(所以运行速度也就比C慢些)。//JavaScript code
<!DOCTYPE html>
<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>demo by zswang</title> <script type='text/javascript' src='http://code.jquery.com/jquery-1.6.4.js'></script> <script type='text/javascript'>//<![CDATA[ $(window).load(function(){ function searchExpression(dest, numbers) { if (!numbers) return; var precision = 1e-6; // 精度 var dict = {}; function search(numbers, expressions, level, dest, outputs) { var result; if ((level <= 1) && (Math.abs(numbers[0] - dest) <= precision)) { !dict[expressions[0]] && outputs.push(expressions[0]); dict[expressions[0]] = true; return true; } for (var i = 0; i < level; i++) { for (var j = i + 1; j < level; j++) { var A = numbers[i]; var B = numbers[j]; numbers[j] = numbers[level - 1]; var vExpA = expressions[i]; var vExpB = expressions[j]; expressions[j] = expressions[level - 1]; expressions[i] = '(' + vExpA + '+' + vExpB + ')'; numbers[i] = A + B; if (search(numbers, expressions, level - 1, dest, outputs)) Result = true; expressions[i] = '(' + vExpA + '-' + vExpB + ')'; numbers[i] = A - B; if (search(numbers, expressions, level - 1, dest, outputs)) Result = true; expressions[i] = '(' + vExpB + '-' + vExpA + ')'; numbers[i] = B - A; if (search(numbers, expressions, level - 1, dest, outputs)) result = true; expressions[i] = '(' + vExpA + '*' + vExpB + ')'; numbers[i] = A * B; if (search(numbers, expressions, level - 1, dest, outputs)) result = true; if (B != 0) { expressions[i] = '(' + vExpA + '/' + vExpB + ')'; numbers[i] = A / B; if (search(numbers, expressions, level - 1, dest, outputs)) result = true; } if (A != 0) { expressions[i] = '(' + vExpB + '/' + vExpA + ')'; numbers[i] = B / A; if (search(numbers, expressions, level - 1, dest, outputs)) result = true; } numbers[i] = A; numbers[j] = B; expressions[i] = vExpA; expressions[j] = vExpB; } } return result; } if (typeof numbers == "string") { numbers = numbers.split(/[\s,;g]+/); } var expressions = []; var outputs = []; for (var i = 0; i < numbers.length; i++) { numbers[i] = +numbers[i]; expressions.push(numbers[i]); } search(numbers, expressions, numbers.length, dest, outputs); return outputs; } $("#go").click(function() { var outputs = searchExpression(+$("#dest").val(), $("#inputs").val()); $("#outputs").val(outputs && outputs.join("\n")); }); });//]]> </script> </head> <body> <body> <div> 目标数:<input id="dest" value="24"/> 数组:<input id="inputs" value="10,10,4,4"/><input type="button" id="go" value="计算吧!"/> </div> <div> <textarea id="outputs"></textarea> </div> </body> </html>/×
无效代码
#include<stdio.h>
#include<algorithm> using namespace std; typedef float ( __cdecl *TYPE_MYFUN )( float , float ); float FunAdd( float x, float y ) { return x + y; } float FunSub( float x, float y ) { return x - y; } float FunMul( float x, float y ) { return x * y; } float FunDiv( float x, float y ) { return x / y; } int main( int argc, char* argv[] ) { float Num[4] = { 3 , 3 , 8 , 8 };//答案为 8/(3-8/3) ; //float Num[4] = { 5 , 5 , 5 , 1 };//答案为5/(5-(1/5)); sort( Num , Num + 4 ); static char op[4] = { '+' , '-' , '*' , '/'}; TYPE_MYFUN Fun[4] = { FunAdd , FunSub , FunMul , FunDiv }; do { for( int x = 0 ; x < 4 ; x++ ) { for( int y = 0 ; y < 4 ; y++ ) { for( int z = 0 ; z < 4 ; z++ ) { float sum = Fun[z]( Fun[y]( Fun[x]( Num[0], Num[1] ) , Num[2] ) , Num[3] ); if( ( sum > 23.9 ) && ( sum < 24.1 ) ) { printf( "((%d%c%d)%c%d)%c%d", ( int )Num[0], op[x] , ( int )Num[1], op[y] , ( int )Num[2], op[z], ( int )Num[3] ); return 0; } sum = Fun[z]( Fun[x]( Num[0], Num[1] ) , Fun[y]( Num[2] , Num[3] ) ); if( ( sum > 23.9 ) && ( sum < 24.1 ) ) { printf( "(%d%c%d)%c(%d%c%d)", ( int )Num[0], op[x] , ( int )Num[1], op[z] , ( int )Num[2], op[y], ( int )Num[3] ); return 0; } sum = Fun[z]( Num[0] , Fun[y]( Num[1] , Fun[x]( Num[2], Num[3] ) ) ); if( ( sum > 23.9 ) && ( sum < 24.1 ) ) { printf( "%d%c(%d%c(%d%c%d))", ( int )Num[0], op[z] , ( int )Num[1], op[y] , ( int )Num[2], op[x], ( int )Num[3] ); return 0; } } } } }while( next_permutation( Num , Num + 4 ) ); return 0; } #include <stdio.h> int rpn (int *); int main (void) { int A, B, C, D, N; int c[8]; int op1, op2, op3; char opc[4] = {'+', '-', '*', '/'}; N = 0; for (A = 1; A <= 13; A++){ for (B = 1; B <= 13; B++){ for (C = 1; C <= 13; C++){ for (D = 1; D <= 13; D++){ for (op1 = 0; op1 < 4; op1++){ for (op2 = 0; op2 < 4; op2++){ for (op3 = 0; op3 < 4; op3++){ c[0] = A; c[1] = B; c[2] = opc[op1]; c[3] = C; c[4] = opc[op2]; c[5] = D; c[6] = opc[op3]; c[7] = 0; if (rpn(c) == 0){ N++; printf ("%8d: ((%2d%c%2d)%c%2d)%c%2d = 24\n", N, A, opc[op1], B, opc[op2], C, opc[op3], D); } c[0] = A; c[1] = B; c[2] = opc[op1]; c[3] = C; c[4] = D; c[5] = opc[op2]; c[6] = opc[op3]; c[7] = 0; if (rpn(c) == 0){ N++; printf ("%8d: (%2d%c%2d)%c(%2d%c%2d) = 24\n", N, A, opc[op1], B, opc[op3], C, opc[op2], D); } c[0] = A; c[1] = B; c[2] = C; c[3] = opc[op1]; c[4] = opc[op2]; c[5] = D; c[6] = opc[op3]; c[7] = 0; if (rpn(c) == 0){ N++; printf ("%8d: (%2d%c(%2d%c%2d))%c%2d = 24\n", N, A, opc[op2], B, opc[op1], C, opc[op3], D); } c[0] = A; c[1] = B; c[2] = C; c[3] = opc[op1]; c[4] = D; c[5] = opc[op2]; c[6] = opc[op3]; c[7] = 0; if (rpn(c) == 0){ N++; printf ("%8d: %2d%c((%2d%c%2d)%c%2d) = 24\n", N, A, opc[op3], B, opc[op1], C, opc[op2], D); } c[0] = A; c[1] = B; c[2] = C; c[3] = D; c[4] = opc[op1]; c[5] = opc[op2]; c[6] = opc[op3]; c[7] = 0; if (rpn(c) == 0){ N++; printf ("%8d: %2d%c(%2d%c(%2d%c%2d)) = 24\n", N, A, opc[op3], B, opc[op2], C, opc[op1], D); } } } } } } } } system("pause"); return 0; } #define MAXVAL 10 int sp; double val[MAXVAL]; void push (double f) { val[sp++] = f; } double pop (void) { return val[--sp]; } int rpn (int *c) { double op2; int ch; sp = 0; while (1){ ch = *c++; switch (ch){ case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.00) push(pop() / op2); else return 1; break; case 0: op2 = pop(); if (23.99 < op2 && op2 < 24.01) return 0; else return 1; break; default: push((double)(ch - 0)); break; } } }×/
转载地址:http://jpfci.baihongyu.com/