Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
虽然没过,早上写的代码太烂了,现在用string类来做,实在爽啊。。简洁多了。。。#include <string> #include <cmath> #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; #define eps 1e-8 #define DOUBLEMAX 100000000000000.0 #define is_zero(x) \ (fabs(x) < eps) #define is_eq(a,b) \ (is_zero(fabs(a-b))) #define module(a,b,ch,stra,strb,fun) \ t=Q(a,b,ch,&flg); \ if(flg)return 0; \ strt='('+stra+ch+strb+')'; \ if(fun)return 1 string res; inline double Q(double a,double b,char c,int *flg) { switch(c){ case '+':return a+b; case '-':return a-b; case '*':return a*b; case '/': if(is_zero(b))*flg=1; else return a/b; } return DOUBLEMAX; } int f2(double a,double b,string stra,string strb,char c) { double t; int flg=0; string strt; t=Q(a,b,c,&flg); if(flg)return 0; if(is_eq(t,24.0)){ res= stra+c+strb; return 1; } return 0; } int f3(double a,double b,double c, string stra,string strb,string strc, char c1,char c2) { double t; int flg=0; string strt; //c1 module(a,b,c1,stra,strb,f2(t,c,strt,strc,c2)); //c2 module(b,c,c2,strb,strc,f2(a,t,stra,strt,c1)); return 0; } int f4(double a,double b,double c,double d, string stra,string strb,string strc, string strd,char c1,char c2,char c3) { double t; int flg=0; string strt; //c1 module(a,b,c1,stra,strb,f3(t,c,d,strt,strc,strd,c2,c3)); //c2 module(b,c,c2,strb,strc,f3(a,t,d,stra,strt,strd,c1,c3)); //c3 module(c,d,c3,strc,strd,f3(a,b,t,stra,strb,strt,c1,c2)); return 0; } void itoa(int num,char *r) { if(num<10){r[0]= char(num+'0');r[1]=0;} else { r[0]=char(num/10 +'0'); r[1]=char(num%10 +'0'); r[2]=0; } } int main() { int a,b,c,d; char ca[3],cb[3],cc[3],cd[3]; string sa,sb,sc,sd; scanf("%d%d%d%d",&a,&b,&c,&d); itoa(a,ca); itoa(b,cb); itoa(c,cc); itoa(d,cd); sa=ca; sb=cb; sc=cc; sd=cd; char sig[4]={'+','-','*','/'}; char c1,c2,c3; int s1,s2,s3; for(s1=0;s1<4;s1++){ c1=sig[s1]; for(s2=0;s2<4;s2++){ c2=sig[s2]; for(s3=0;s3<4;s3++){ c3=sig[s3]; if(f4(a,b,c,d,sa,sb,sc,sd, c1,c2,c3)){ printf("%s",res.c_str()); return 0; } } } } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator