当前位置: 代码迷 >> Web前端 >> 随机数+结合
  详细解决方案

随机数+结合

热度:544   发布时间:2012-08-25 10:06:20.0
随机数+组合
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<script type="text/javascript">
	var xmlhttp;
	function createXmlHttpReq() {
		if (window.ActiveXObject) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} else if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		}
	}
	function createAcct() {
		createXmlHttpReq();
		xmlhttp.open("GET",'<c:url value="/fundAcct.do"/>',false);
		xmlhttp.onreadyStateChange = handleRes;
		xmlhttp.send(null);
	}
	function handleRes() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var div = document.createElement("div");
			div.style.width=document.documentElement.offsetWidth-22;
			div.style.height=document.documentElement.offsetHeight-22;
			div.style.position="absolute";
			div.style.top="0";
			div.style.left="0";
			div.style.background="#D4D4D4";
			div.style.filter = "alpha(opacity=80)";
			document.body.appendChild(div);
			document.getElementById("result").innerHTML = xmlhttp.responseText;
		}
	}
</script>
</head>
<body bgcolor="#EBEEF7">
<c:if test="${empty RES}">
	<c:if test="${not empty ERR}">
		<div><font style="font-weight: bold;" color="red"><c:out value="${ERR}"/></font></div>
	</c:if>
	<form id="myForm" action="<c:url value="/fundAcct.do"/>" method="post" target="targetFrame">
		编号:<input type="text" tabindex="0" name="fundNo" value="<c:out value="${fundAcctDTO.fundNo }"/>"/><br/>
		名称:<input type="text" tabindex="1" name="fundName" value="<c:out value="${fundAcctDTO.fundName }"/>"/><br/>
		<input type="submit" value="提交" id="sub">
	</form>
	<hr/><br/>
	<iframe name="targetFrame" src="/blank.screen" frameborder="0"></iframe>
	<script type="text/javascript">
		parent.document.all.targetFrame.style.height=document.documentElement.scrollHeight;
		parent.document.all.targetFrame.style.width=document.documentElement.scrollWidth;
	</script>
</c:if>
<c:if test="${not empty RES}">
	<table>
		<tr><td>结果</td></tr>
		<c:if test="${empty account}">
			<tr><td>无结果</td></tr>
		</c:if>
		<c:if test="${not empty account}">
			<c:forEach items="${account}" var="acc" varStatus="s">
				<tr><td>
					<c:out value="${acc}"/>----<c:out value="${blue[s.index]}"/>
				</td></tr>
			</c:forEach>
		</c:if>
	</table>	
</c:if>
</body>
</html>




import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.paic.pafa.app.lwc.service.databinding.BindException;
import com.paic.pafa.app.web.servlet.ModelAndView;
import com.paic.pafa.app.web.servlet.mvc.SimpleFormController;
import com.pingan.nts.extrading.invest.dto.FundAcctDTO;

public class FundAcctController extends SimpleFormController {

	private static final Log LOG = LogFactory.getLog(FundAcctController.class);

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		response.setContentType("text/html; charset=utf-8");
		LOG.info("ENTER.....");
		FundAcctDTO fundAcctDTO = (FundAcctDTO)command;
		Map<String, Object> model = new HashMap<String, Object>();
		if (StringUtils.isEmpty(fundAcctDTO.getFundNo())) {
			model.put("ERR", "账号编码不能为空!");
			return new ModelAndView(getFormView(), model);
		}
		model.put("RES", "RES");
		if (StringUtils.isEmpty(fundAcctDTO.getFundName())) {
			model.put("account", randomNote(33, 6, 5));
			model.put("blue", randomNote(16, 1, 5));
		}
		return new ModelAndView(getSuccessView(), model);
	}

	private static List<String> ls_ball = new LinkedList<String>();
	private static void init(int range) {
		ls_ball.clear();
		for (int i = 1; i <= range; i++) {
			ls_ball.add(i<10?"0"+i:""+i);
		}
	}
	
	public static void main(String[] args) {
		System.out.println(getVal(33, 6));
		randomNote(33, 6, 1);
		randomNote(16, 1, 1);
	}
	
	private static String[] randomNote(int range, int number, int numbers) {
		String[] result = new String[numbers];
		for (int i = 0; i < numbers; i++) {
			result[i] = randomNum(number, range);
			System.out.println(result[i]);
		}
		return result;
	}
	
	private static String randomNum(int number, int range) {
		init(range);
		StringBuilder result = new StringBuilder();
		for (int i = 0; i < number; i++) {
			int size = random(ls_ball.size());
			result.append(", ").append(ls_ball.get(size));
			ls_ball.remove(size);
		}
		return result.substring(2);
	}
	
	private static int random(int deep) {
		return (int)(Math.random() * deep);
	}
	
	private static String getStrVal(int num1, int num2) {
		if (num1 == 1) {
			return "1";
		}
		StringBuilder result = new StringBuilder();
		for (int i = 1; i <= num2; i++) {
			result.append("-").append(num1--);
		}
		return result.substring(1);
	}
	
	private static int getVal(int num1, int num2) {
		String str1 = getStrVal(num1, num2);
		String str2 = getStrVal(num2, num2-1);
		int dividends = 1;
		int divisors = 1;
		int[] nums1 = getVal(str1);
		int[] nums2 = getVal(str2);
		for (int i = 0; i < nums2.length; i++) {
			for (int j = 0; j < nums1.length; j++) {
				if (nums1[j] % nums2[i] == 0) {
					nums1[j] = nums1[j] / nums2[i];
					nums2[i] = 1;
					break;
				}
			}
		}
		for (int n1 : nums1) {
			dividends *= n1;
		}
		for (int n2 : nums2) {
			divisors *= n2;
		}
		
		return dividends / divisors;
	}
	
	private static int[] getVal(String str) {
		String[] strs = str.split("-");
		int size = strs.length;
		int[] nums = new int[size];
		for (int i = 0; i < size; i++) {
			nums[i] = Integer.parseInt(strs[i]);
		}
		return nums;
	}
	
	private static void px(String targetStr, int number) {
		StringBuilder sb = new StringBuilder();
		String src = null;
		for (int i = 1; i <= number; i++) {
			src = i<10?"0"+i:""+i;
			if (targetStr.contains(src)) {
				sb.append(", ").append(src);
			}
		}
		System.out.println(sb.substring(2));
	}
	
}
  相关解决方案