Q1. 8 桁の一意の 900 万から 1000 万の数値のみの文字列を生成できますか?
はい、10 桁の 1,2,3,4,5,6,7,8,9,0 を使用して 10000000 8 桁の一意の数値のみの文字列を生成できます
考えられるすべての組み合わせに対して正しいロジックを記述している場合、重複は発生しませんが、安全のために set を使用できます。
java.lang.OutOfMemoryError エラーが発生しているのは、その数を生成してメモリに保持しているためです。これに対する解決策は、いくつかの小さな数字のチャンクを生成してデータベースに保存してから、リストをクリアして、次の数字のチャンクを入力し、すべての数字をデータベースに保存するまで繰り返し続けることです。
Q2. 1 回のプログラム実行で 900 万から 1000 万の一意の「数値のみ」の文字列を生成するにはどうすればよいですか?
これは、目標を達成するために使用できる組み合わせコードです
public class Combination{
public static int count = 0;
public static ArrayList<String> list;
public Combination(){
list = new ArrayList<String>();
}
public static void main(String[] args){
Combination c = new Combination();
Scanner sc = new Scanner(System.in);
String str = sc.next();
int num = sc.nextInt();
if(num>str.length()){
System.out.println("This combination is not possible");
System.out.println(num+" should be less than or equal to the length of the string "+str);
}else{
System.out.println("Processing....");
char[] array = new char[num];
c.fillNthCharacter(0,array,str);
System.out.println("Total combination = "+count);
}
}
public static void fillNthCharacter(int n,char[] array,String str){
for(int i=0;i<str.length();i++){
array[n]=str.charAt(i);
if(n<array.length-1){
fillNthCharacter(n+1,array,str);
}else{
count++;
//System.out.println(new String(array));
list.add(new String(array));
if(list.size()>100000){
//code to add into database
list.clear();
}
}
}
}
}