导航:首页 > 编程语言 > 穷举法java

穷举法java

发布时间:2022-08-07 09:28:43

❶ 使用java写出一个穷举算法,分不多,求高人热心帮助

求加分,纯手打啊有木有……

public class Test {

/*
*
* 警察局抓住了A、B、C、D四名盗窃嫌疑犯,
* 其中只有一人是小偷。在审问时,A说:“我
* 不是小偷”;B说:“C是小偷”;C说:“小偷
* 肯定是D”;D说:“C在冤枉好人”。现在已经
* 知道这四人中有三人说的是真话,一人说的
* 是假话。请问到底谁是小偷?
*
* 即A说:thief != A;
* B说:thief == C;
* C说:thief == D;
* D说:thief != D;
*
* 先分别假设他们都是小偷,如果这个条件导致
* 四个人有三个说的是真话,一个人是假话
* 即abcd四人说的条件有三个成立,一个不
* 成立即为真相!
*/
public static void main(String[] args) {
char thief;//这个人是小偷
//abcd分别代表真假,0为假,1为真,count为条件成立的个数,count=3说明有是3个说的是真话,1个是假话

int a,b,c,d,count;
for(thief='A';thief<='D';thief++){//先假设小偷是A,然后循环假设
a=thief!='A'?1:0;
b=thief=='C'?1:0;
c=thief=='D'?1:0;
d=thief!='D'?1:0;
count=a+b+c+d;
if(count==3){
System.out.println("真正的小偷是"+thief);
}
}
}
}

❷ Java 穷举问题

没有什么是绝对的,什么叫这个问题没人会解决的?
这个我正好有相应的解法,等一下贴出来:
//不用穷举法,用递归
public class InnerLoops {
public static void main(String[] args) {
String a = "0123456789";//可变
int rows = 3;//可变
char[][] c = new char[rows][];
for(int i=0; i<rows; i++)
c[i]=a.toCharArray();
inner(c,0,0);
}
public static final void inner(char[][] array,int currentRow,int currentColumn){
if(currentColumn<array[currentRow].length){
if(currentRow==array.length-1){

print(array);
}
if(hasNext(array,currentRow)){

inner(array,currentRow+1,0);
}
ring(array,currentRow);
inner(array,currentRow,currentColumn+1);
}
}
private static final boolean hasNext(char[][] array, int row) {
return array.length>row+1;
}
private static final void print(char[][] array) {
System.out.println(toString(array));
}
private static final String toString(char[][] array) {
String s = "";
for(int i=0; i<array.length; i++)
s+=(array[i][0]);
return s;
}
//使row行上的数往前推一位,溢出的0位补到最后一位
private static final void ring(char[][] array, int row) {
char t = array[row][0];
for(int i=1; i<array[row].length; i++)
array[row][i-1]=array[row][i];
array[row][array[row].length-1]=t;
}
}

❸ 急!!在线等!JAVA采用穷举法,对100到999的任意三位数m,首先求其个位、十位、百位,用x y z表示。

x=m/100;
y=m%100/10;
z=m%100%10;

❹ java穷举法给出任一字符串, 如abcdd,找出所有可能的字母组合(如ab ac add dd …… ) 有谢谢!!急!!

public class Zuhe {
public static void Method(String a){
StringBuffer str=new StringBuffer();
int l=a.length();
for(int i=0;i<l;i++){
str.delete(0,str.length());
for(int j=l-i-1;j>-1;j--){
char chh=a.charAt(j);
str.append(chh);
System.out.println(str);
}
}
}
public static void main(String[] args) {
String ssstr="abcd";
Method(ssstr);
}

}

❺ 百钱百鸡(穷举算法)

设公鸡、母鸡、小鸡分别为x、y、z 只,由题意得:

x+y+z =100……①
5x+3y+(1/3)z =100……②
有两个方程,三个未知量,称为不定方程组,有多种解。

令②×3-①得:7x+4y=100;

即:y =(100-7x)/4=25-(7/4)x

由于y 表示母鸡的只数,它一定是自然数,而4 与7 互质,因此x 必须是4 的倍数。我们把它写成:x=4k(k 是自然数),于是y=25-7k,代入原方程组,可得:z=75+3k。把它们写在一起有:
x =4k
y =25 - 7k
z =75+ 3k

一般情况下,当k 取不同数值时,可得到x、y、z 的许多组值。但针对本题的具体问题,由于x、y、z 都是100 以内的自然数,故k 只能取1、2、3 三个值,这样方程组只有以下三组解:

一、 x =4;y =18;z =78

二、 x =8;y =11;z =81

三、 x =12;y =4;z =84

❻ 请用java中的穷举法判断一个范围内的数是否为素数

b=0是把0赋值给b 相当于b的值是0
b==0是判断b是不是等于0 如果表达式成立则执行{}中的内容否则执行else{}的内容
b=1是把1赋值给b 相当于b的值是1

❼ java穷举搜索法,谁来写个!

算24点
穷举法
solution类

package test;

import java.util.Enumeration;
import java.util.Vector;

public class Solution {

static Vector numbers;
private boolean hasSolution;
private Vector theSolution;
private double theResult;
static final int TOTAL_POSITION = 13;
static final int NUM_POSITION[] = { 1, 4, 8, 11 };
static int numCombin[][] = new int[24][4];
static boolean hasNumCombin = false;
static final int OP_POSITION[] = { 2, 6, 10 };
static int opCombin[][] = new int[64][3];
static boolean hasOpCombin = false;
static Character whiteSpace = new Character(' ');
static Character openParen = new Character('(');
static Character closeParen = new Character(')');
static Character addition = new Character('+');
static Character subtract = new Character('-');
static Character multiply = new Character('*');
static Character division = new Character('/');
static final int PAREN_POSITION[] = { 0, 3, 5, 7, 9, 12 };
static final int PAREN_COMBIN[][] = { { 0, 0, 0, 0, 0, 0 },
{ 1, 0, 2, 0, 0, 0 }, { 1, 0, 0, 0, 2, 0 }, { 0, 1, 0, 0, 2, 0 },
{ 0, 1, 0, 0, 0, 2 }, { 0, 0, 0, 1, 0, 2 }, { 1, 0, 2, 1, 0, 2 } };

public Solution(int num0, int num1, int num2, int num3) {
theSolution = null;
numbers = new Vector(4);
numbers.addElement(new Integer(num0));
numbers.addElement(new Integer(num1));
numbers.addElement(new Integer(num2));
numbers.addElement(new Integer(num3));
searchSolution();
}

public Vector getSolution() {
return theSolution;
}

public boolean hasSolution() {
return hasSolution;
}

private static void numberCombination() {
Vector numbers = new Vector(4);
int count = 0;
if (hasNumCombin)
return;
for (int i = 0; i < 4; i++)
numbers.addElement(new Integer(i));

for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
Vector tmpNumbers = (Vector) numbers.clone();
numCombin[count][0] = ((Integer) tmpNumbers.elementAt(i))
.intValue();
tmpNumbers.removeElementAt(i);
numCombin[count][1] = ((Integer) tmpNumbers.elementAt(j))
.intValue();
tmpNumbers.removeElementAt(j);
numCombin[count][2] = ((Integer) tmpNumbers.elementAt(k))
.intValue();
tmpNumbers.removeElementAt(k);
numCombin[count][3] = ((Integer) tmpNumbers.elementAt(0))
.intValue();
tmpNumbers.removeElementAt(0);
count++;
}

}

}

hasNumCombin = true;
}

private static void operatorCombination() {
int count = 0;
if (hasOpCombin)
return;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
opCombin[count][0] = i;
opCombin[count][1] = j;
opCombin[count][2] = k;
count++;
}

}

}

hasOpCombin = true;
}

private void searchSolution() {
Vector tmpExpression = new Vector(13);
int count = 0;
numberCombination();
operatorCombination();
for (int i = 0; i < 13; i++)
tmpExpression.addElement(whiteSpace);
for (int num = 0; num < 24; num++) {
for (int paren = 0; paren < 7; paren++) {
for (int op = 0; op < 64; op++) {
if (count++ % 500 == 0)
for (int i = 0; i < 4; i++)
tmpExpression.setElementAt(numbers
.elementAt(numCombin[num][i]),
NUM_POSITION[i]);

for (int i = 0; i < 6; i++)
switch (PAREN_COMBIN[paren][i]) {
case 1:
tmpExpression.setElementAt(openParen,
PAREN_POSITION[i]);
break;

case 2:
tmpExpression.setElementAt(closeParen,
PAREN_POSITION[i]);
break;

case 0:
tmpExpression.setElementAt(whiteSpace,
PAREN_POSITION[i]);
break;
}

for (int i = 0; i < 3; i++)
switch (opCombin[op][i]) {
case 0:
tmpExpression
.setElementAt(addition, OP_POSITION[i]);
break;

case 1:
tmpExpression
.setElementAt(subtract, OP_POSITION[i]);
break;

case 2:
tmpExpression
.setElementAt(multiply, OP_POSITION[i]);
break;

case 3:
tmpExpression
.setElementAt(division, OP_POSITION[i]);
break;
}

try {
theResult = (new Expression(tmpExpression)).getValue();
} catch (IllegalExpressionException e) {
continue;
}
if (theResult == 24D) {
hasSolution = true;
theSolution = tmpExpression;
return;
}
}

}

}

hasSolution = false;
theSolution = null;
}

public static void printSolution(Vector theSolution) {
for (Enumeration e = theSolution.elements(); e.hasMoreElements();) {
System.out.print(e.nextElement());
}
}

public static void main(String[] args) {
Solution solution = new Solution(5, 7, 8, 9);
if (solution.hasSolution) {
printSolution(solution.getSolution());
}
}
}

IllegalExpressionException类
package test;

public class IllegalExpressionException extends Exception {

public IllegalExpressionException() {
}

public IllegalExpressionException(String msg) {
super(msg);
}
}
Expression类
package test;

import java.util.*;

public class Expression {

protected String inputExpression;
private Stack operatorStack;
private Stack postFixStack;
private final int EOL = 0;
private final int VALUE = 1;
private final int OPAREN = 2;
private final int CPAREN = 3;
private final int MULT = 4;
private final int DIV = 5;
private final int PLUS = 6;
private final int MINUS = 7;
private final int INPUT_PRECEDENCE[] = { 0, 0, 100, 0, 3, 3, 1, 1 };
private final int STACK_PRECEDENCE[] = { -1, 0, 0, 99, 4, 4, 2, 2 };
private double currentValue;
private double theResult;
private int lastToken;

public Expression() {
inputExpression = null;
operatorStack = new Stack();
postFixStack = new Stack();
operatorStack.push(new Integer(0));
}

public Expression(String inString) {
inputExpression = inString.trim();
operatorStack = new Stack();
postFixStack = new Stack();
operatorStack.push(new Integer(0));
}

public Expression(Vector inVector) {
StringBuffer tmpString = new StringBuffer();
for (Enumeration e = inVector.elements(); e.hasMoreElements(); tmpString
.append(e.nextElement()))
;
inputExpression = tmpString.toString().trim();
operatorStack = new Stack();
postFixStack = new Stack();
operatorStack.push(new Integer(0));
}

public void setExpression(String inString) {
inputExpression = inString.trim();
}

public String getExpression() {
return removeSpace(inputExpression);
}

public double getValue() throws IllegalExpressionException {
StringTokenizer parser = new StringTokenizer(inputExpression,
"+-*/() ", true);
do
if (!parser.hasMoreTokens()) {
lastToken = 0;
processToken();
} else {
String token = parser.nextToken();
char firstChar = token.charAt(0);
if (!Character.isWhitespace(firstChar)) {
if (token.length() == 1 && isOperator(firstChar)) {
switch (firstChar) {
case '+':
lastToken = PLUS;
break;

case '-':
lastToken = MINUS;
break;

case '*':
lastToken = MULT;
break;

case '/':
lastToken = DIV;
break;

case '(':
if (lastToken != VALUE)
lastToken = OPAREN;
else
throw new IllegalExpressionException(
"Missing operator");
break;

case ')':
lastToken = CPAREN;
break;
}
} else {
try {
currentValue = Double.valueOf(token).doubleValue();
} catch (NumberFormatException e) {
throw new IllegalExpressionException(
"Unknown symbol");
}
if (lastToken != 1 && lastToken != 3)
lastToken = 1;
else
throw new IllegalExpressionException(
"missing operator");
}
processToken();
}
}
while (lastToken != EOL);
if (postFixStack.empty())
throw new IllegalExpressionException("Missing operand");
theResult = ((Double) postFixStack.pop()).doubleValue();
if (!postFixStack.empty())
throw new IllegalExpressionException("Missing operator");
else
return theResult;
}

private String removeSpace(String inputString) {
StringBuffer s = new StringBuffer();
for (int i = 0; i < inputString.length(); i++) {
char c;
if ((c = inputString.charAt(i)) != ' ')
s.append(inputString.charAt(i));
}

return s.toString();
}

private void processToken() throws IllegalExpressionException {
switch (lastToken) {
case VALUE:
postFixStack.push(new Double(currentValue));
return;

case CPAREN:
int topOperator;
while ((topOperator = ((Integer) operatorStack.peek()).intValue()) != OPAREN
&& topOperator != EOL)
applyOperation(topOperator);
if (topOperator == OPAREN)
operatorStack.pop();
else
throw new IllegalExpressionException("Missing open parenthesis");
break;

default:
while (INPUT_PRECEDENCE[lastToken] <= STACK_PRECEDENCE[topOperator = ((Integer) operatorStack
.peek()).intValue()])
applyOperation(topOperator);
if (lastToken != EOL)
operatorStack.push(new Integer(lastToken));
break;
}
}

private void applyOperation(int topOperator)
throws IllegalExpressionException {
if (topOperator == OPAREN)
throw new IllegalExpressionException("Unbalanced parenthesis");
double rightOperand = getPostStackTop();
double leftOperand = getPostStackTop();
if (topOperator == PLUS)
postFixStack.push(new Double(leftOperand + rightOperand));
else if (topOperator == MINUS)
postFixStack.push(new Double(leftOperand - rightOperand));
else if (topOperator == MULT)
postFixStack.push(new Double(leftOperand * rightOperand));
else if (topOperator == DIV)
if (rightOperand != (double) 0)
postFixStack.push(new Double(leftOperand / rightOperand));
else
throw new IllegalExpressionException("Division by zero");
operatorStack.pop();
}

private double getPostStackTop() throws IllegalExpressionException {
if (postFixStack.empty())
throw new IllegalExpressionException("Missing operand");
else
return ((Double) postFixStack.pop()).doubleValue();
}

private boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '('
|| c == ')';
}
}

❽ 编写一个java程序,用穷举法找出2~50之间的素数,并打印出来.

public static void main(String[] args) {
int i, k;
boolean yes;
for (k = 2; k <= 50; k++) {
yes = true;
i = 2;
while (i <= k - 1 && yes) {
if (k % i == 0)
yes = false;
i++;
}
if (yes)
System.out.print(k + " ");
}
}

阅读全文

与穷举法java相关的资料

热点内容
现代钢琴教程pdf 浏览:23
客户端框架源码 浏览:206
python自动办公能干嘛 浏览:873
程序员追爱 浏览:252
程序员逻辑故事 浏览:768
加密icsot23i2c 浏览:713
你们有什么好的解压软件 浏览:607
常州空气压缩机厂家 浏览:241
安卓如何关闭app内弹出的更新提示 浏览:409
e4a写的app怎么装苹果手机 浏览:201
海立压缩机海信系 浏览:210
社保如何在app上合并 浏览:220
小米加密照片后缀 浏览:236
我的世界网易手机怎么创服务器 浏览:978
载入单页源码 浏览:930
阿里云服务器seo 浏览:777
海洋斗什么时候上线安卓 浏览:86
中行app如何查每日汇款限额 浏览:840
输入服务器sn是什么意思 浏览:725
sha1算法java 浏览:90