打印成3个数为一组的(5*3*3=45)组号码?格式:
127 128 129 137 138 139 147 148 149 227
228 229 237 238 239 247 248 249 327 328
329 337 338 339 347 348 349 427 428 429
437 438 439 447 448 449 527 528 529 537
538 539 547 548 549
127 128 129 137 138 139 147 148 149 227
228 229 237 238 239 247 248 249 327 328
329 337 338 339 347 348 349 427 428 429
437 438 439 447 448 449 527 528 529 537
538 539 547 548 549
import java.util.*;
class Test {
public static void main(String[] args) {
int[] a = {1,2,3,4, 5};
int[] b = {2,3,4};
int[] c = {7,8,9};
int[][] all = {a, b, c}; int[] idx = new int[all.length];
Arrays.fill(idx, 0);
List<String> result = new ArrayList<String>();
while (true) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<idx.length; i++) {
sb.append(all[i][idx[i]]);
}
result.add(sb.toString());
idx[idx.length-1]++;
for (int i=idx.length-1; i>0; i--) {
if (idx[i] == all[i].length) {
idx[i] = 0;
idx[i-1]++;
}
}
if (idx[0] == all[0].length) {break;}
}
for (int i=0; i<result.size(); i++) {
System.out.printf("%s ", result.get(i));
if ((i+1)%10 == 0) {System.out.println();}
}
}
}
原题:
文件dingwei.txt中的内容是:
12345/234/789
01246/01246/01247
12478/23456/258
678/268/357
要求:读取文件内容,对数据进行处理,使输出内容与下面一致。127 128 129 137 138 139 147 148 149 227
228 229 237 238 239 247 248 249 327 328
329 337 338 339 347 348 349 427 428 429
437 438 439 447 448 449 527 528 529 537
538 539 547 548 549000 001 002 004 007 010 011 012 014 017
020 021 022 024 027 040 041 042 044 047
060 061 062 064 067 100 101 102 104 107
110 111 112 114 117 120 121 122 124 127
140 141 142 144 147 160 161 162 164 167
200 201 202 204 207 210 211 212 214 217
220 221 222 224 227 240 241 242 244 247
260 261 262 264 267 400 401 402 404 407
410 411 412 414 417 420 421 422 424 427
440 441 442 444 447 460 461 462 464 467
600 601 602 604 607 610 611 612 614 617
620 621 622 624 627 640 641 642 644 647
660 661 662 664 667122 125 128 132 135 138 142 145 148 152
155 158 162 165 168 222 225 228 232 235
238 242 245 248 252 255 258 262 265 268
422 425 428 432 435 438 442 445 448 452
455 458 462 465 468 722 725 728 732 735
738 742 745 748 752 755 758 762 765 768
822 825 828 832 835 838 842 845 848 852
855 858 862 865 868623 625 627 663 665 667 683 685 687 723
725 727 763 765 767 783 785 787 823 825
827 863 865 867 883 885 887
import java.util.*;class Test {
public static void main(String[] args) {
try {
String filename = "dingwei.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(filename)));
for (String buf = br.readLine(); buf != null; buf = br.readLine()) {
String[] data = buf.split("/");
char[][] c = new char[data.length][];
for (int i=0; i<c.length; i++) {
c[i] = data[i].toCharArray();
}
doCombine(c);
System.out.println();
}
br.close();
} catch (Throwable e) {
e.printStackTrace();
}
} public static void doCombine(char[][] c) {
int[] idx = new int[c.length];
Arrays.fill(idx, 0);
List<String> result = new ArrayList<String>();
while (true) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<idx.length; i++) {
sb.append(c[i][idx[i]]);
}
result.add(sb.toString());
idx[idx.length-1]++;
for (int i=idx.length-1; i>0; i--) {
if (idx[i] == c[i].length) {
idx[i] = 0;
idx[i-1]++;
}
}
if (idx[0] == c[0].length) {break;}
}
for (int i=0; i<result.size(); i++) {
System.out.printf("%s ", result.get(i));
if ((i+1)%10 == 0) {System.out.println();}
}
System.out.println();
}
}