答案如下:import java.util.ArrayList; import java.util.List;/** * 9个数1,2,3...9,分成三组,即3个3位数.求解这3个3位数的值的比为1:2:3的所有情况 * @Date 2008/12/20, 上午 11:14:47 * @author Hannibal */ public class Arithmetic_1 {
public static void main(String[] args) {
String[] input = {"1","2","3","4","5","6","7","8","9"}; List<String> temp = new ArrayList<String>();
//得到所有的3位数组合(1~9所有可能的组合),放入List中。 for (int i = 0; i < input.length; i++) for (int j = i+1; j < input.length; j++) for (int k = j+1; k < input.length; k++) //注意根据算法,这里的List一定是从小到大的 temp.add(input[i]+input[j]+input[k]); //连成3位数
public static void g() { boolean[] barr = new boolean[9]; for (int i = 123; i < 333; i++) { for (int j = 0; j < barr.length; j++) { barr[j] = false; } if (h(i, 1, barr)) { System.out.println(i + " " + i * 2 + " " + i * 3); } } } private static boolean h(int x, int a, boolean[] barr) { if (a > 3) { return true; } int y = x * a; while (y > 0) { int s = y - y / 10 * 10; if (s == 0 || barr[s - 1]) { return false; } barr[s - 1] = true; y /= 10; } return h(x, a + 1, barr); }
7楼的算法很好。 我也写了一个 public class NumTest { public static boolean isRep(int num){ StringBuffer ns=new StringBuffer(new Integer(num).toString()); ns.append(new Integer(2*num).toString()); ns.append(new Integer(3*num).toString()); String s=ns.toString();
import java.util.List;/**
* 9个数1,2,3...9,分成三组,即3个3位数.求解这3个3位数的值的比为1:2:3的所有情况
* @Date 2008/12/20, 上午 11:14:47
* @author Hannibal
*/
public class Arithmetic_1 {
public static void main(String[] args) {
String[] input = {"1","2","3","4","5","6","7","8","9"};
List<String> temp = new ArrayList<String>();
//得到所有的3位数组合(1~9所有可能的组合),放入List中。
for (int i = 0; i < input.length; i++)
for (int j = i+1; j < input.length; j++)
for (int k = j+1; k < input.length; k++)
//注意根据算法,这里的List一定是从小到大的
temp.add(input[i]+input[j]+input[k]); //连成3位数
//对所有3位数,遍历取出3个3位数,判断是否一符合条件
for (int i = 0; i < temp.size(); i++)
for (int j = i+1; j < temp.size(); j++)
for (int k = j+1; k < temp.size(); k++) {
if(compare(temp.get(i) , temp.get(j), temp.get(k))){
System.out.println(temp.get(i)+" "+temp.get(j)+" "+temp.get(k));
}
}
}
//判断是否符合条件。
//调用时由于,算法原因,一定是a<b<c的。
static boolean compare(String a , String b, String c){
//默认一定要a1<b1<c1
float a1 = Integer.parseInt(a);
float b1 = Integer.parseInt(b);
float c1 = Integer.parseInt(c);
// System.out.println(a1/c1);
if(a1/b1==0.5 && a1/c1==(float)1/(float)3)
return true;
return false;
}
}
结果如下:
123 246 369
189 378 567
public static void g() {
boolean[] barr = new boolean[9];
for (int i = 123; i < 333; i++) {
for (int j = 0; j < barr.length; j++) {
barr[j] = false;
}
if (h(i, 1, barr)) {
System.out.println(i + " " + i * 2 + " " + i * 3);
}
}
} private static boolean h(int x, int a, boolean[] barr) {
if (a > 3) {
return true;
}
int y = x * a;
while (y > 0) {
int s = y - y / 10 * 10;
if (s == 0 || barr[s - 1]) {
return false;
}
barr[s - 1] = true;
y /= 10;
}
return h(x, a + 1, barr);
}
我也写了一个
public class NumTest {
public static boolean isRep(int num){
StringBuffer ns=new StringBuffer(new Integer(num).toString());
ns.append(new Integer(2*num).toString());
ns.append(new Integer(3*num).toString());
String s=ns.toString();
if(s.contains("0"))
return false;
for(int i=1;i<=s.length();i++){
String is=new Integer(i).toString();
if(!s.contains(is))
continue;
if(s.indexOf(is)!=s.lastIndexOf(is)){
return false;
}
}
return true;
}
public static void main(String args[]){
for(int i=123;i<333;i++){
if(isRep(i)){
System.out.println(i+" "+2*i+" "+3*i);
}
}
}
}