導航:首頁 > 編程語言 > 窮舉法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 瀏覽:25
客戶端框架源碼 瀏覽:210
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