Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

How to make 9 8 7 6 =24


6 replies to this topic

#1 LuciferStar

    Advanced Member

  • Kontributors
  • PipPipPipPipPipPipPip
  • 114 posts
  • Location:Suzhou Jiangsu China

Posted 10 August 2004 - 07:44 AM

#include "stdio.h"[br]#include "process.h"[/br]int fun0(int x,int y){return (x+y);}[br]int fun1(int x,int y){return (x-y);}[/br]int fun2(int x,int y){return (x*y);}[br]int fun3(int x,int y){return (x/y);}[/br]int (*assign[4])(int x,int y);[br]void assign_function(void)[/br]{[br]assign[0]=fun0;[/br]assign[1]=fun1;[br]assign[2]=fun2;[/br]assign[3]=fun3;[br]}[/br]//const int data[4]={9,8,7,1};[br]int data[4]={9,8,7,1};[/br]const int fetch[24][4]={[br]{3,2,1,0},{0,3,2,1},{1,0,3,2},{2,1,0,3},[/br]{3,1,2,0},{0,3,1,2},{2,0,3,1},{1,2,0,3},[br]{3,0,1,2},{2,3,0,1},{1,2,3,0},{0,1,2,3},[/br]{3,2,0,1},{1,3,2,0},{0,1,3,2},{2,0,1,3},[br]{3,0,1,2},{1,3,0,2},{2,1,3,0},{0,2,1,3},[/br]{3,1,0,2},{2,3,1,0},{0,2,3,1},{1,0,2,3}};[br]int calculator[4];[/br]void result(int x,int y,int r,int n)[br]{[/br]int a[4],b[4];[br]char  fomular[20],o[4]={'+','-','*','/'};[/br]for(int i=0;i<4;i++)[br]{[/br]a[i]=fetch[x][i];[br]b[i]=data[fetch[y][i]];//printf("a[%d]:%d  b[%d]:%d  o[%d]:%c  o[a[%d]]:%c\n",i,a[i],i,b[i],i,o[i],i,o[a[i]]);[br]}[/br]if((a[0]==0||a[0]==1)&&(a[1]==2||a[1]==3))[/br]   sprintf(fomular,"Method %d:  (%d%c%d)%c%d%c%d=%d\n",n,b[0],o[a[0]],b[1],o[a[1]],b[2],o[a[2]],b[3],r);[br]else if((a[0]==2||a[0]==3)&&(a[3]!=2||a[3]!=3))[/br]   sprintf(fomular,"Method %d:  %d%c%d%c%d%c%d=%d\n",n,b[0],o[a[0]],b[1],o[a[1]],b[2],o[a[2]],b[3],r);[br]else sprintf(fomular,"Method %d:  (%d%c%d%c%d)%c%d=%d\n",n,b[0],o[a[0]],b[1],o[a[1]],b[2],o[a[2]],b[3],r);[/br]//printf("\tResult:\n");[br]printf("\t%s",fomular);[/br][br]}[/br][br]void main(void)[/br]{  int r=24;[br]   //int temp;[/br]   int n=0;[br]assign_function();[/br]printf("\t\tAchieve 24 point\n\n\tBy Lucifer Star\n\tSingle Studio\n\t\tEMAIL:LetMeGetOne@hotmail.com\n\n");[br]printf("*****************************************\n");[/br]printf("\tPlease input 1st number:");[br]scanf("%d",&data[0]);[/br]printf("\tPlease input 2nd number:");[br]scanf("%d",&data[1]);[/br]printf("\tPlease input 3rd number:");[br]scanf("%d",&data[2]);[/br]printf("\tPlease input 4th number:");[br]scanf("%d",&data[3]);[/br]printf("\tWhat do you want to achieve(default:24):");[br]scanf("%d",&r);[/br]//scanf("%d",&temp);[br]//temp=getch();[/br]//if(temp!=0x0d) r=temp;[br][/br]printf("\n     ***********************************\n");[br]if(data[0]==0||data[1]==0||data[2]==0||data[3]==0) {printf("The 4 numbers shouldn't be 0!");exit(0);}[/br]for(int ii=0;ii<24;ii++)[br]{[/br]   for(int i=0;i<24;i++)[br]   {[/br]	int z=0;[br]	for(int j=0;j<4;j++)[/br]	{[br]  calculator[j]=data[fetch[i][j]];[/br]	}[br]	for(int k=0;k<3;k++)[/br]	{[br]  if(k==0)[/br]  {[br]     if(calculator[0]%calculator[1]!=0&&fetch[ii][0]==3)[/br]        {z=0;break;}[br]     else z=assign[fetch[ii][k]](calculator[k],calculator[k+1]);[/br]  }[br]  else[/br]  {  if(fetch[ii][k]==3)[br]       if(calculator[k+1]==0||z==0||(z%calculator[k+1])!=0)[/br] 	 {z=0;break;}[br]     z=assign[fetch[ii][k]](z,calculator[k+1]);[/br]  }[br]	}[/br]	if(z==r)[br]	{    n++;result(ii,i,r,n);   }[/br]    }[br]}[/br]printf("\n******************************************\n");[br]if(n==0) printf("Sorry,it's unable to do so!");[/br]else printf("Thanks for using my software!");
}


#2 neeki4444

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 38 posts

Posted 01 September 2004 - 02:18 PM

don't use printf use cout its more secure. Using print may lead to bufferover flows and hackers can easily gain control of the computer by injecting some extra code themselves

#3 OpaQue

    Administrator

  • Admin - The Official Guru
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1,895 posts
  • Gender:Male
  • Location:Somewhere in Time & Space.
  • Interests:Discovering Myself.
  • myCENT:-129.97

Posted 01 September 2004 - 06:07 PM

I prefer printf because it is fast and gives more power. Formatting can be done easily. Besides, looking at the above program, since iostream.h is not included, cout cannot be included. its a pure c program.

#4 LuciferStar

    Advanced Member

  • Kontributors
  • PipPipPipPipPipPipPip
  • 114 posts
  • Location:Suzhou Jiangsu China

Posted 03 September 2004 - 11:40 AM

I've learned C for more than 5 years,
and I found it's hard for me to learn C++,though I can understand it.

CLASS is hard to me.

the output of cout is variable,uncontrolable,I think.

#5 goranche

    Newbie

  • Kontributors
  • Pip
  • 7 posts

Posted 24 September 2004 - 01:53 PM

don't be scared with c++. Every thing that you are doing in C++ you CAN do in C but in more complex way...
classes are invented to make peoples unsecured but it's not so hard...

To all of you...
I'm from Serbia. I'm new here on Trap17. I'm pleased to be with you folks, but you must forgive me on my "bad" english.. :(

#6 buxgoddess

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 395 posts
  • Gender:Female
  • Location:Cosmos
  • myCENT:56.7

Posted 25 December 2008 - 09:04 AM

Now something I get to add here is the way the quoted text is posted. There are [br] tags in between the lines. You cannot directly copy the text into the c++ ide and you need to edit them. I would suggest that any such thing copied from another location must be edited so that they look organized. Look at the first quoted text, they just look junk and as a first time learner will find the codes are not very friendly. Any matter must be organized as I think the posts are done for some information and not wasting time over what is quoted and find that there are too many errors or logical problems with the program. All people who post their posts must be held responsible and asked to edit their posts for making them more informative and neat. Incentives may be given. And for badly posted posts there should be no myCent awarded until resolved by editing. I would not like too much mods/admin intervention but a neater look is definitely welcome.

#7 akira550

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 348 posts
  • Gender:Male
  • Location:Geek Ville
  • myCENT:48.85

Posted 19 September 2009 - 08:57 AM

you can't use the cout function because the iostream.h is not included the program
hmm anyway cout is more secure than printf but if its only a school project just use printf :lol:





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users