導航:首頁 > 編程語言 > java打亂演算法

java打亂演算法

發布時間:2023-03-20 01:22:20

❶ 用java程序冒泡排序演算法把一組打亂順序的數字從小到大排列並列印出來

publicclassMaoPao
{
publicstaticvoidmain(Stringargs[])
{
int[]arr={2,1,3,4,6,5,7,8,9,0,10};
//N是數組的元素個數,這樣無論多少個數,直接修改arr中的元素就行了,
//不需要調整循環次數
intN=arr.length;
inttemp=0;
//冒泡排序:每次把最大的放到最後,N-i是因為第i次排序之後,
//數組arr的最後i個數已經是按照大小順序的了,所以不需要再排序了
//比如第一次排序之後,最後一個數肯定是最大的,下一次只需要排前9個就行了。
for(inti=1;i<N;++i)
{
for(intj=0;j<N-i;++j)
{
//如果前面的數比後面的大,則不是按照順序的,因此要交換
if(arr[j]>arr[j+1])
{
temp=arr[j];//交換2個數
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}

for(inti=0;i<N;++i)//輸出排序後的結果
{
System.out.print(arr[i]+"");
}

}
}


忘採納。

❷ java中怎麼把一個數組元素隨機打亂順序

暈.不用那麼麻煩.

先轉化為list(為什麼不一開始就用List呢?)

例:

String[] arr = new String[] {"1", "2"};
List list = Arrays.asList(arr);

直接調用shuffle,就是隨機回排序

例:Collections.shuffle(list);

直接輸出就是你想要答的結果

❸ 如何在JAVA中隨機打亂輸出一組數字而不重復

哥給你完善下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class A6 {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

boolean same = false;
int num1 = 0;
int num2 = 0;
while (!same) {// if the numbers which user enter are the same,do it
//碰沒睜 again
try {
System.out.print("enter a number: ");//笑歲 ask user a number
num1 = Integer.parseInt(in.readLine());
System.out.print("enter another number: ");// ask user another
// number
num2 = Integer.parseInt(in.readLine());
if (num1 != num2) {
same = true;// if the number are different,continue the next part and don't need to enter again
} else {
System.out.println("two numbers is same.");// if not , ask user to enter again
}
} catch (NumberFormatException nfc) {// text the enter is a number or not,if not,ask user enter again
System.out.println("not a number");// show user the reason why get false
}
}

//Add starts
List<Integer>察清 list = new ArrayList<Integer>();
int max = num1 > num2? num1: num2;
int min = num1 < num2? num1: num2;

Random rand = new Random();

while(list.size() < (max - min + 1)){
int value = min + rand.nextInt(max - min + 1);

if(value >= min && value <= max && !list.contains(value)){
list.add(new Integer(value));
}
}

//Calcuate sum for each pair
int sum[] = new int[list.size() - 1];

for(int i = 0; i < list.size() - 1; i++){
sum[i] = list.get(i).intValue() + list.get(i+1).intValue();
}

Arrays.sort(sum); //升序排列結果

System.out.println("Max pair sum is: " + sum[sum.length - 1]);
System.out.println(list.toString());

//Add ends
}
}

--------------測試
enter a number: 6
enter another number: -3
Max pair sum is: 10
[3, 4, 6, -2, 0, 2, -1, 5, 1, -3]

❹ java怎麼打亂一個由鍵盤錄入規定數組長度以及數據的一維數組並遍歷出來

首先鍵盤族肆錄入可以用scanner類實現控制台輸入,然後打亂數組可以用random類產生隨機數來控制輸入的數存入隨機的數組位置,這個地方要注意隨機數不可重復,要兆褲轎控制范圍在純配定義的數組長度內,最後遍歷直接一個for循環。

❺ Java如何打亂集合中的元素

package com.yii;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class CollectionShuffle {
public static void main(String[] argv) throws Exception {
ArrayList<String> obj = new ArrayList<String>();
obj.add("A");
obj.add("B");
obj.add("C");
obj.add("D");
obj.add("E");
obj.add("F");
Collections.shuffle(obj);
System.out.println(obj);
}}

❻ java【不用list】如何將數組元素順序打亂

這好辦得很嘛, 假如長度為10, 從10內隨機取一個數,把改下標的值和下標0互換, 換個10次或者更高就行了

❼ java里怎樣打亂(洗牌)一個數組

//給你個思路:給要給隨機值,該隨機值在索引范圍內,然後從索引值裡面取元素,在該元素對應
//的位置,進行二次隨機取其他元素,然後進行交換,就可以了!
//還有更簡單的辦法,java早就想到了這一點,在集合工具裡面就提供該方法,其實他內部也是
//這樣的思路,用隨機值進行交換!
importjava.util.*;
publicclassArrayListTest{
publicstaticvoidmain(String[]args){
Integer[]arr=newInteger[10];
for(inti=0;i<arr.length;i++){
arr[i]=i+1;
}
System.out.println(" 原序:"+Arrays.toString(arr)+" ");//原數組!
List<Integer>list=Arrays.asList(arr);//借用集合幫忙排序!
for(Integera:list){
Collections.shuffle(list);//隨機排序!
System.out.println("隨機:"+Arrays.toString(arr));//排序後!
}
}
}
//數字或者數量,你可以隨意修改,二維數組也可以照用!

❽ 關於洗牌演算法,請用JAVA編寫,定義一個數組,儲存1-52以內的數,打亂順序輸出!

import java.util.Enumeration;
import java.util.Hashtable;/**
* 7. * 亂序撲克牌 洗牌方法 8. * 9. *
*
* @author virture 10. * 11.
*/
public class Cards { Hashtable htMember = new Hashtable();// 放置撲克牌的Hash表 public Cards() { } public void put(String card) {
htMember.put(card, card);
} public void get() {
System.out.println("你拿到的牌是:");
Enumeration RLKey = htMember.keys();
while (RLKey.hasMoreElements()) {
String accKey = RLKey.nextElement().toString();// 取HashTable中的關鍵字詞
System.out.print((String) htMember.get(accKey) + ",");
}
} public static void main(String[] args) {
String[] cards = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
String[] kinds = { "黑桃", "紅桃", "梅花", "方塊" }; Cards cardList = new Cards(); String suit;// 當前選中牌的類型
String face;// 當前選中牌
int randomCardNum = 52;// 當前隨機取牌的個數,記住不能大於全部牌52張 while (true) {
suit = kinds[Math.round((float) Math.random() * (kinds.length - 1))];
face = cards[Math.round((float) Math.random() * (cards.length - 1))]; cardList.put(suit + face);
if (cardList.htMember.size() >= randomCardNum
&& cardList.htMember.size() <= 52) {
break;
}
}
cardList.get();
}
}

❾ 請問Java如何用math. random方法打亂輸出的字元

String ss = "1,2,3,4,5,6,7,8,9";
System.out.println(ss);
String[] strs = ss.split(",");
Random a = new Random();
int i = strs.length;
while (i > 0){
int num = a.nextInt(strs.length);
while(num < strs.length && strs[num]==""){
num++;
if(num == strs.length){
num = 0;
}
}
System.out.print(strs[num]+" ");
strs[num] = "";
i--;
}
可以考慮隨機數,輸出之後就給空渣磨;
不給空的話,可以記錄已經輸出之後的num值
可以考慮用set 保存數字的李輪集合,這樣原數組的內容是沒清空的
String ss = "1,2,3,4,5,6,7,8,9";
System.out.println(ss);
String[] strs = ss.split(",");
Random a = new Random();
Set set= new HashSet();
int i = strs.length;
while (i >哪梁信 0){
int num = a.nextInt(strs.length);
while(num < strs.length && set.contains(num)){
num++;
if(num == strs.length){
num = 0;
}
}
System.out.print(strs[num]+" ");
set.add(num);
i--;
}

❿ 怎麼用java代碼把打亂的數字1到13按從大到小排序

那要看你的13個數字是怎麼輸入的了
如果直接是在數組里,用冒泡排序
int a[]={1,10,7,8,5,3,2,4,6,11,13,12,9};
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i]>a[j]) {
int t=a[i];
a[i]=a[j];
a[j]=t;
}

}

}
for (int i : a) {
System.out.print(i+" ");
}

閱讀全文

與java打亂演算法相關的資料

熱點內容
專題學習網站源碼 瀏覽:163
jsphead什麼 瀏覽:88
gps串口數據怎麼發送 瀏覽:968
win10文件主頁共享查看 瀏覽:411
中國聯通有哪些app是免流的 瀏覽:176
邊做邊保存的文件找不到了 瀏覽:858
win10照片應用文件夾名稱 瀏覽:966
編程如何解決資金的原子性 瀏覽:638
如何製作廣角鏡頭矯正文件 瀏覽:513
在網頁開發中應該選用哪個資料庫 瀏覽:742
iphone5移動卡貼 瀏覽:990
電腦文件的格式 瀏覽:127
extjs的xtype 瀏覽:959
suse11iso文件要u盤安裝 瀏覽:153
如何將報表統計數據轉化為圖形 瀏覽:444
如何寄快遞材料文件 瀏覽:265
java構造方法private 瀏覽:475
手機文件找回恢復 瀏覽:516
word怎麼把u盤里的文件拔掉 瀏覽:976
港版蘋果用的插排 瀏覽:1000

友情鏈接