随笔1_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 随笔1

随笔1

 2013/10/24 18:53:05  QuarterLifeForJava  程序员俱乐部  我要评论(0)
  • 摘要:packagetest;importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;publicclassMainCalcuate{privatestaticList<Integer>list=newArrayList<Integer>();//用于存放左边一组数据privatestaticList<Integer>leftList=newArrayList<
  • 标签:随笔
class="java" name="code">
package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainCalcuate {
	
	private static List<Integer> list  = new ArrayList<Integer>();
	
	//用于存放左边一组数据
	private static List<Integer> leftList  = new ArrayList<Integer>();
	
	//用于存放右边一组数据
	private static List<Integer> rightList  = new ArrayList<Integer>();
	
	//统计左边集合的和
	private static int leftValue = 0;
	
	//统计右边集合的和
	private static int rightValue = 0;
	
	public static void doMain(int arry[]) {
		for(int i=0;i<arry.length;i++){
			list.add(arry[i]);
	    }
		Collections.shuffle(list);
		//System.out.println(list);
		while(list.size()!=0){
			balance(leftValue,rightValue);
		}
		//System.out.println(leftList + "=" + leftValue);
		//System.out.println(rightList + "=" + rightValue);
		MyReduce.myJDBCInsert(leftValue, rightValue, leftList.toString() ,rightList.toString());
		leftValue = 0;
		rightValue = 0;
		leftList.removeAll(leftList);
		rightList.removeAll(rightList);
	}
	
	//核心算法
	public static void balance(int left,int right){
		int x = list.remove(0);
		if(x+left>x+right){
			rightList.add(x);
			rightValue = rightValue+x;
		}else{
			leftList.add(x);
			leftValue = leftValue+x;
		}
	}
}


package test;

import java.util.Random;

public class MainProgram implements Runnable{
	
	//假设数值范围为1-10
	private static final int number = 10;
	
	//数组大小
	private static final int arrayNumber = 10;
	
	private static int count = 0;
	
	private static final int arry[] = getArry();

	public void run() {
		/**数字越大,答案越多,成功率也越高*/
		while(count!=(arrayNumber*2)){
			MainCalcuate.doMain(arry);
			count++;
		}
	}

	public static int[] getArry(){
		//随机生成1-10之间的十个数
		int arry[] = new int[arrayNumber];
		for(int i=0;i<arrayNumber;i++){
			arry[i] = new Random().nextInt(number)+1;
		}
		return arry;
	}
	
	public static void main(String[] args) {
		Thread t = new Thread(new MainProgram());
		t.start();
	}

}


package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MyReduce {
	
	public static void myJDBCInsert(int left,int right,String leftArry,String rightArry){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			String URL="jdbc:mysql://localhost:3306/test"; 
			String Username="root";                           
			String Password="";                           
			Connection conn=DriverManager.getConnection(URL,Username,Password);
			String sql = "insert into test(LEFTVALUE,RIGHTVALUE,LEFTARRY,RIGHTARRY) values(?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1,left);
			pstmt.setInt(2,right);
			pstmt.setString(3,leftArry);
			pstmt.setString(4,rightArry);
			pstmt.executeUpdate();
			pstmt.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void myJDBCAnaly(){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			String URL="jdbc:mysql://localhost:3306/test"; 
			String Username="root";                           
			String Password="";                           
			Connection conn=DriverManager.getConnection(URL,Username,Password);
			String sql = "SELECT test.LEFTARRY,test.RIGHTARRY " +
						 "FROM test " +
						 "WHERE ABS(test.LEFTVALUE-test.RIGHTVALUE)" +
						 "=(SELECT MIN(ABS(test.LEFTVALUE-test.RIGHTVALUE)) " +
						 "FROM test)";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				System.out.print(rs.getString("LEFTARRY")+"=");
				System.out.println(rs.getString("RIGHTARRY"));
			}
			pstmt.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void deleteDB(boolean flag){
		if(flag){
			try{
				Class.forName("com.mysql.jdbc.Driver");
				String URL="jdbc:mysql://localhost:3306/test"; 
				String Username="root";                           
				String Password="";                           
				Connection conn=DriverManager.getConnection(URL,Username,Password);
				String sql = "delete from test";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				pstmt.executeUpdate();
				pstmt.close();
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		MyReduce.myJDBCAnaly();
		deleteDB(true);
	}

}


建表语句(MySQL)
/*
Navicat MySQL Data Transfer

Source Server         : mysql
Source Server Version : 50610
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 50610
File Encoding         : 65001

Date: 2013-10-24 16:34:51
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `test`
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `key` int(11) NOT NULL AUTO_INCREMENT,
  `LEFTVALUE` int(11) NOT NULL,
  `RIGHTVALUE` int(11) NOT NULL,
  `LEFTARRY` varchar(255) NOT NULL,
  `RIGHTARRY` varchar(255) NOT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB AUTO_INCREMENT=262 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of test
-- ----------------------------

算法参考:http://www.iteye.com/problems/98441
发表评论
用户名: 匿名