新聞中心
java:求一組數(shù)的組合情形,如String[] a={1,2,3,4,5,6},從中任意挑出三個放在一組,他們所有組合的情況。
Note1:
郊區(qū)網(wǎng)站建設公司成都創(chuàng)新互聯(lián),郊區(qū)網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為郊區(qū)成百上千家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的郊區(qū)做網(wǎng)站的公司定做!
挑三個就用三個for-loop.
int[] a={1,2,3,4,5};
for(int i=1; ia.length-2; i++) {
for(j=1+1; ja.length-1; j++) {
for(k=j+1; ka.length; k++) {
System.out.println(a[i]+","+a[j]+","+a[k]);
}
}
}
Note2:
若挑出的個數(shù)要到執(zhí)行期才得知, 就要改用別的做法.
可以將不定層數(shù)的for-loop轉(zhuǎn)成一個for-loop.
用Java代碼編碼: 有1~8個號碼,需要選擇6個出來,組成不同的組合,不處理順序,只關注重復,詳情如下,
如果不管排序的話遞歸 循環(huán)都能實現(xiàn)這個 ?給你一個分別用遞歸和循環(huán)實現(xiàn)的例子
import?java.util.Arrays;
public?class?TestArray?{
public?static?void?main(String[]?args)?{
int[]?a?=?{?1,?2,?3,?4,?5,?6,?7,?8};
System.out.println("***********循環(huán)*************");
ergodicArray(a,?6);
System.out.println("***********遞歸*************");
recursionArray(a,new?int[6],0,0);
}
//遞歸
public?static?void?recursionArray(int[]?a,int[]?b,int?start,int?index){
if(b.lengtha.length)
throw?new?RuntimeException("長度錯誤");
else{
if(indexb.length){
int[]?c=b.clone();
for(int?i=start;ia.length;i++){
//System.out.println("i:"+i+"?index:"+index);
c[index]=a[i];???????
recursionArray(a,c,i+1,index+1);
}????
}else
System.out.println(Arrays.toString(b));
}
}
//循環(huán)
public?static?void?ergodicArray(int[]?a,int?length){
if(lengtha.length)
throw?new?RuntimeException("長度錯誤");
else{
int[]?b=new?int[length];
for(int?i=0;ilength;i++)
b[i]=i;
ergodicArray(a,b);
}
}
public?static?void?ergodicArray(int[]?a,?int[]?b)?{
while?(b[0]?=?(a.length?-?b.length))?{
if?(b[b.length?-?1]??a.length)?{
for?(int?i?=?0;?i??b.length;?i++)?{
System.out.print(a[b[i]]);
if(ib.length-1)
System.out.print(',');
}
System.out.println();
++b[b.length?-?1];
}?else?{
int?j?=?b.length?-?1;
while?(j?=?0)?{
if?(b[j]?!=?a.length?-?(b.length?-?j)?+?1)?{
//?j--;
break;
}?else
j--;
}
if?((b[0]?==?(a.length?-?b.length?-?1)?||?b[j]?=?(a.length?-?b.length?+?j)))?{
int?t?=?0;
for?(int?i?=?j;?i??b.length;?i++)?{
if?(i?==?j)?{
b[i]++;
}?else?{
b[i]?=?b[i?-?1]?+?1;
}
}
}
}
}
}
請問下列JAVA代碼選哪個是正確的?為什么?
Fourth questions wrong, should be C, Java class
parameter list fifth questions wrong, goto is a reserved word Java, but also belongs to the keyword Java is not used, malloc is a variable named
sixth questions wrong, to initialize local variables, global variables have a default value of
8 B
10 C, the default variables belong to global variables value, and belongs to the static, available in the static method or the main method inside
12 D = is the comparison operator, with || logic operator, | is bitwise (binary)
13 A
14 B the default constructor is defined in your class, and no other constructors, define the parameter less constructor
15 B默認構(gòu)造函數(shù)沒有返回類型,系統(tǒng),同名的類,不是靜態(tài)的
16如果(改良薄噢樂安)需要的是值類型
17 C超載的方法的返回類型,與同一名稱的第一個方法,類型或二參數(shù)個數(shù)不同,對點
【Java問題】有沒有大佬給個代碼和思路!
因為題目要求拷貝Example1-10.java文件從Java_Source到Java目錄下,可以把數(shù)字拼到文件名的字符串中,從而把Example1-10.java文件,一個一個復制到目標目錄下.
完整的Java程序如下
import java.io.File;
import java.IoException;
public class test{
pubilc static void main(String args[])throws IoException{
for(int i=1;i=10;i++){
String S1 = "G:/Java_Source/Example"+i+".java";
String S2 = "G:/Java/Example"+i+".java";
File Source = new File(S1);
File dest = new File(S2);
java.nio.file.Files.copy(Source.toPath(),dest.toPath());
}
System.out.printIn("File copy");
}
}
Java代碼:從1到21隨機選7個數(shù),要求不重復,幫忙檢查!
是編譯錯誤,數(shù)組越界,算法可以這:樣實現(xiàn):隨機選數(shù),把選好的數(shù)字放到一個表(數(shù)組)中;如果重復則反復執(zhí)行。代碼如下:
class Main {
public static void main(String args[]) {
final int N = 21;
final int M = 7;
int selected[] = new int[N];//用來記得每次選的數(shù)字表格
int cnt = 0;// 已選的數(shù)字個數(shù)
while (cnt M) {
int n = (int) (Math.random() * N) + 1;
boolean find = false;
for (int i = 0; i cnt; i++) {
if (selected[i] == n) {// 表示n重復
find = true;
break;
}
}
if (!find) {//表示選的n是不重復的
selected[cnt] = n;
cnt++;
}
}
for(int i=0;iM;i++)//打印每次選擇的數(shù)字
System.out.print(selected[i]+" ");
}
}
當前文章:挑一挑java代碼 第一行代碼java
標題路徑:http://www.dlmjj.cn/article/docdiie.html