选择显示字体大小

用0-9之间的四个数求24的算法

大家可能都见过以下的问题

给定0-9之间的四个数,通过加、减、乘、除运算得出结果24
可以使用括号

例如:三个5一个1
5 x (5 - 1/5) = 24
还有3、4、5、6,大家可以试着算一下
下面是我用java写的一个程序
程序后面是表达式,它需要从文件读入

package compute24;

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

public class compute24 {
        static float[][] expression = new float[40][15];

        static int[][] fournumber = new int[24][4];

        static int[] fournum = { 0, 5, 5, 5, 1 };
                //可以在5551位置输入要计算的四个数
        public static void main(string[] args) throws ioexception {
                // this.readexpressionfromfile();
                compute24 obj1 = new compute24();
                obj1.readexpressionfromfile();
                obj1.arrangefournumber();
                obj1.insertnumbertoexpression();
                // obj1.computeexpression(expression);

        }

        public void arrangefournumber() {
                /* arrange the four number */
                /*
                 * make a canzhao array
                 */
                int[][] compare = { { 1, 2, 3, 4 }, { 1, 3, 2, 4 }, { 1, 3, 4, 2 },
                                { 1, 2, 4, 3 }, { 1, 4, 2, 3 }, { 1, 4, 3, 2 }, { 2, 1, 3, 4 },
                                { 2, 1, 4, 3 }, { 2, 3, 1, 4 }, { 2, 3, 4, 1 }, { 2, 4, 3, 1 },
                                { 2, 4, 1, 3 }, { 3, 1, 2, 4 }, { 3, 1, 4, 2 }, { 3, 2, 1, 4 },
                                { 3, 2, 4, 1 }, { 3, 4, 2, 1 }, { 3, 4, 1, 2 }, { 4, 1, 2, 3 },
                                { 4, 1, 3, 2 }, { 4, 2, 1, 3 }, { 4, 2, 3, 1 }, { 4, 3, 1, 2 },
                                { 4, 3, 2, 1 } };
                for (int i = 0; i < 24; i++) {
                        for (int j = 0; j < 4; j++) {
                                fournumber&#91;i&#93;&#91;j&#93; = fournum&#91;compare&#91;i&#93;&#91;j&#93;&#93;;
                        }
                }
                /*
                 * search a easy way to compute the combination of four number
                 */
        }

        public void readexpressionfromfile() throws ioexception {
                int getchar;
                fileinputstream fin;
                try {
                        fin = new fileinputstream(
                                        &quot;d:\\javacode\\compute24\\compute24\\expression.txt&quot;);
                } catch (filenotfoundexception e) {
                        system.out.println(&quot;file not found&quot;);
                        return;
                } catch (arrayindexoutofboundsexception e) {
                        system.out.println(&quot;usage:showfile file&quot;);
                        return;
                }
                for (int i = 0; i < 38; i++) {
                        for (int j = 0; j < 15; j++) {
                                getchar = fin.read();
                                expression&#91;i&#93;&#91;j&#93; = getchar;
                                if (getchar == 48)
                                        break;

                        }

                }
                // how to read form file?
        }

        public void insertnumbertoexpression() {
                /*
                 * insert the 24 group number into the expression
                 */
                for (int sign = 0; sign < 24; sign++) {/*
                                                                                                 * sign to help inserting 24
                                                                                                 * pairs of four number
                                                                                                 */
                        for (int i = 0; i < 14; i++) {
                                for (int j = 0, k = 0; k < 7; j++, k = k + 2)
                                        expression&#91;i&#93;&#91;k&#93; = fournumber&#91;sign&#93;&#91;j&#93;;

                        }

                        for (int i = 14; i < 24; i++) {
                                for (int j = 0, k = 1; k < 9; j++, k = k + 2) {
                                        expression&#91;i&#93;&#91;k&#93; = fournumber&#91;sign&#93;&#91;j&#93;;
                                        if (k == 3)
                                                k++;
                                }

                        }

                        for (int i = 24; i < 26; i++) {
                                for (int j = 0, k = 0; k < 8; j++, k = k + 2) {
                                        expression&#91;i&#93;&#91;k&#93; = fournumber&#91;sign&#93;&#91;j&#93;;
                                        if (k == 2)
                                                k++;
                                }
                        }

                        for (int i = 26; i < 34; i++) {
                                for (int j = 0, k = 1; k < 9; j++, k = k + 2) {
                                        expression&#91;i&#93;&#91;k&#93; = fournumber&#91;sign&#93;&#91;j&#93;;
                                        if (k == 5)
                                                k++;
                                }
                        }

                        for (int i = 34; i < 35; i++) {
                                for (int j = 0, k = 0; k < 9; j++, k = k + 2) {
                                        expression&#91;i&#93;&#91;k&#93; = fournumber&#91;sign&#93;&#91;j&#93;;
                                        if (k == 0)
                                                k++;
                                }
                        }

                        for (int i = 35; i < 38; i++) {
                                for (int j = 0, k = 1; k < 10; j++, k = k + 2) {
                                        expression&#91;i&#93;&#91;k&#93; = fournumber&#91;sign&#93;&#91;j&#93;;
                                        if (k == 3)
                                                k = k + 2;
                                }
                        }
                        this.computeexpression();
                }

        }


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons