2015年9月10日 星期四

[Java] 好題目3

有1、2、3、4個數字,能組成多少個互不相同且無重複數字的三位元數?

並輸出各種組合?

程式碼如下

int i,j,k;
int count=0;
System.out.print("\n");
for (i=1;i<5;i++)
{
 for(j=1;j<5;j++)
 {
  for (k=1;k<5;k++)
  {
   if (i!=k && i!=j && j!=k)
   {
                           System.out.printf("%d,%d,%d\n",i,j,k);
       count++;
   }
  }
 }
}
System.out.println("total:" + count);

執行結果
2,3,4                                                                                                                                                    
2,4,1                                                                                                                                                    
2,4,3                                                                                                                                                    
3,1,2                                                                                                                                                    
3,1,4                                                                                                                                                    
3,2,1                                                                                                                                                    
3,2,4                                                                                                                                                    
3,4,1                                                                                                                                                    
3,4,2                                                                                                                                                    
4,1,2                                                                                                                                                    
4,1,3                                                                                                                                                    
4,2,1                                                                                                                                                    
4,2,3                                                                                                                                                    
4,3,1                                                                                                                                                    
4,3,2                                                                                                                                                    
total:24   

題目來源:

http://stenlyho.blogspot.tw/2007/04/c100-1-10.html

沒有留言:

張貼留言