[英]How would I get 8 combinations of 4 numbers in java
所以我想要數字 0、1、2、3,以及它們在 8 個地方的每一個組合,比如從
00000000 至 33333333
我寫了一些技術上可行但看起來丑陋且效率低下的東西,你們知道更好的方法嗎,我發誓我可以用 %10 做一些事情,並檢查它是否低於 4,但我無法想象它。
for(int i1 = 0; i1 < 4; i1++) {
for(int i2 = 0; i2 < 4; i2++) {
for(int i3 = 0; i3 < 4; i3++) {
for(int i4 = 0; i4 < 4; i4++) {
for(int i5 = 0; i5 < 4; i5++) {
for(int i6 = 0; i6 < 4; i6++) {
for(int i7 = 0; i7 < 4; i7++) {
for(int i8 = 0; i8 < 4; i8++) {
System.out.println("" +((Integer)i1).toString()+((Integer)i2).toString()+((Integer)i3).toString()+((Integer)i4).toString()+((Integer)i5).toString()+((Integer)i6).toString()+((Integer)i7).toString()+((Integer)i8).toString());
}
}
}
}
}
}
}
}
編輯:感謝您對 Base 4 的建議這現在是我修改后的代碼
public static void main(String[] args) {
for(int i = 0; i<65536;i++){
String x =Integer.toString(i,4);
while(x.length() < 8){
x = "0" + x;
}
System.out.println(x);
}
}
編輯 2:我把 while 循環弄錯了,我會有一些數字的兩倍,而其他數字都沒有,所以我在上面的代碼中修復了它
您可以從 0 循環到 33333333,只打印數字中僅出現 0、1、2 和 3 位的數字:
int high = 33; // replace with 33333333 for your actual example
int width = 3; // replace with 8 for your actual example
String mask = "%0" + width + "d";
for (int i=0; i <= high; ++i) {
boolean print = true;
int num = i;
while (num > 0) {
if (num % 10 != 0 && num % 10 != 1 && num % 10 != 2 && num % 10 != 3) {
print = false;
break;
}
num /= 10;
}
if (print) {
System.out.println(String.format(mask, i));
}
}
這打印:
000
001
002
003
010
011
012
013
020
021
022
023
030
031
032
033
只是為了添加另一種(我猜效率低下)的方法來做到這一點,您可以編寫一個置換 function 遞歸生成字符串置換,如下所示:
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
permute("", stringList);
}
private static void permute(String res, List<String> resultList) {
if (res.length() == 8) {
resultList.add(res);
return;
}
for (int i=0; i<=3; i++) {
permute(res + i, resultList);
}
}
現在stringList
將保存所有排列。 但是,此方法會創建許多中間字符串,因此編寫此方法只是為了了解如何遞歸地完成它。
使用StringBuilder
:
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
permute(new StringBuilder(""), stringList);
}
private static void permute(StringBuilder sb, List<String> resultList) {
if (sb.length() == 8) {
resultList.add(sb.toString());
return;
}
for (int i=0; i<=3; i++) {
sb.append(i);
permute(sb, resultList);
sb.setLength(sb.length()-1);
}
}
您可以為此使用遞歸:
public void combinations(int current,int depth){
current*=10;
for(int i=0;i<4;i++){
current++;
if(depth==0){
System.out.println(current);
}else{
combinations(current,depth-1);
}
}
}
//when you want to calculate it
combinations(0,7);
這種方法很有效,因為它只計算你需要的值,不需要存儲不必要的值,也不需要自己創建字符串。
您修改后的代碼可以提高一點效率。
由於 Java 沒有 lpad function,因此您可以使用包含適合給定長度的 pad 的字符串數組來模擬它。 這有減少垃圾收集的好處。
public class TenFourGB {
public static void main (String[] args) {
String pads[] = {"00000000","0000000","000000","00000","0000","000","00","0",""};
for (int i=0; i<65536; i++){
String x = Integer.toString(i, 4);
System.out.print(pads[x.length()]); // pads length is coupled to i's max value
System.out.println(x);
}
}
}
該解決方案的時機(>> /dev/null):
real 0m0.397s
real 0m0.229s
real 0m0.228s
real 0m0.235s
real 0m0.232s
Tim 的解決方案* 時機(>> /dev/null):
real 0m0.423s
real 0m0.443s
real 0m0.612s
real 0m0.461s
real 0m0.426s
我不得不在 Tim 的代碼中修改high
和width
以使它發出與我相同的數字,從而使它成為一場公平的比賽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.