Tuesday, June 26, 2007

Q11: number composition

Q: How many three digit numbers can you generate with 1, 2, 3, 4. The numbers are not duplicated and each number doesn't have duplicate digits.

A:

===========================================
import java.util.Vector;

public class NumberComposition {
private static Vector nums = new Vector();

public static void generateNumbers(){
int num;

for(int i=1; i <= 4; i++){
num =0;
for(int j=1; j <= 4; j++){
for(int k=1; k <= 4; k++){
if((i!=j) && (i!=k) && (j!=k)){
num = i* 100 + j*10 + k;
nums.add(num);
}else continue;
}
}
}
}

public static int getNumberCounter(){
return nums.size();
}

public static void printNumbers(){
for(int i=0; i < nums.size(); i++){
System.out.print(nums.get(i) + " ");
}
}

public static void main(String[] args) {
generateNumbers();
System.out.println("Total number: " + getNumberCounter());
printNumbers();
}
}
==========================================

result:
Total number: 24
123 124 132 134 142 143 213 214 231 234 241 243 312 314 321 324 341 342 412 413 421 423 431 432

No comments: