01/10/2018, 10:16
Về random string trong java
mọi người cho mình hỏi, mình có 1 string ví dụ như “NguyenVanA”, mình muốn random lại vị trí, xáo trộn các phần tử kiểu như “ganAnNyeuV” chẳng hạn, ai có giải thuật nào ngắn gọn có thể giúp mình không ạ
Bài liên quan
tự viết đi, chỉ là đảo lộn thứ tự chữ cái thôi mà
Mình có cái cách này. Dài và thủ công lắm
Google: Scramble String + < Ngôn ngữ Lập trình >
package try_chap1;
public class try_part2 {
public static void main(String[] arg){
String[] chu = {“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”,“t”,“u”,“o”,“p”,“q”,“r”,“y”,“i”,“x”,“w”}; // đổi các giá trị bên trong nếu bạn muốn
final int max = 17; // số lớn nhất
final int min = 0;// số nhỏ nhất
int range = (max - min) + 1;
int soan = (int)(Math.random() * range) + min; //Thuật toán sẽ tự động ra số bất kì từ 0-17;
System.out.println(chu[soan]);
}
}
Bài cả năm trước rồi giờ còn vào đào
Đây là bài toán shuffle, dùng thuật toán này là được
Fisher–Yates shuffle
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence—in plain terms, the algorithm shuffles the sequence. The algorithm effectively puts all the elements into a hat; it continually determines the next element by randomly drawing an element from the hat until no elements remain. The algorithm produces an unbiased permutation: every permutation is equally likely. The modern version of the algorithm is efficient: it takes time proportional to the number...
Implement dễ cực.