第六屆藍(lán)橋杯java試題-九數(shù)組分?jǐn)?shù)
來源:程序員人生 發(fā)布時(shí)間:2015-05-20 10:25:00 閱讀次數(shù):3842次
1,2,3...9 這9個(gè)數(shù)字組成1個(gè)分?jǐn)?shù),其值恰好為1/3,如何組法?
代碼以下
/**
*
* @author Administrator
* 類名:Main
* 創(chuàng)建時(shí)間:2015⑸⑴
* vesion1.0
*/
public class Main
{
public static void test(int[] x)
{
int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
if(a*3==b) System.out.println(a + " " + b);
}
public static void f(int[] x, int k)
{
if(k>=x.length){
test(x);
return;
}
for(int i=k; i<x.length; i++){
{
int t=x[k]; x[k]=x[i]; x[i]=t;
}
f(x,k+1);
{
int t=x[k]; x[k]=x[i]; x[i]=t;
}
}
}
public static void main(String[] args)
{
int[] x = {1,2,3,4,5,6,7,8,9};
f(x,0);
}
}
注:更多java資源盡在java教程網(wǎng)
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)