本文目錄一覽:
- 1、用Java編寫 數字由小到大的排列
- 2、java 輸入任意幾個數字,怎樣將他們從大到小排序?
- 3、JAVA 100個數字用算法排序
- 4、java實現數字排列
- 5、java從鍵盤依次輸入10個正整數,存放在數組中,每輸入一個整數就進行排序,而不是最後排序?
- 6、java實現字符串中數字的截取排序;請給出具體的代碼
用Java編寫 數字由小到大的排列
排序算法,基本的高級語言都有一些提供。C語言有qsort()函數,C++有sort()函數,java語言有Arrays類(不是Array)。用這些排序時,都可以寫自己的排序規則。
Java API對Arrays類的說明是:此類包含用來操作數組(比如排序和搜索)的各種方法。
實現代碼如下:
package com.test.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test{
public static void main(String args[]){
System.out.println(“請輸入數字並按回車, 輸入其他字符按回車退出”);
Scanner scan = new Scanner(System.in);
ListInteger list = new ArrayListInteger();
while(scan.hasNextInt()){
list.add(scan.nextInt());
scan = new Scanner(System.in);
}
Test test = new Test();
ListInteger fList = test.arrangeList(list);
test.printList(fList);
}
public ListInteger arrangeList(ListInteger list){
ListInteger rList = new ArrayListInteger();
for(int i=0;ilist.size();i++){
int k = 0;
int temp = list.get(i);
for(int j=1;jlist.size();j++){
if(templist.get(j)){
k = j;
temp = list.get(j);
}
}
if(rList.add(list.get(k))){
list.remove(k);
i–;
}
}
return rList;
}
public void printList(ListInteger list){
for(Integer i:list){
System.out.print(i+” “);
}
}
}
java 輸入任意幾個數字,怎樣將他們從大到小排序?
public static void main(String[] args) { Scanner scan = new Scanner(System.in)。
}
System.out.println(“從大到小輸出:”);
for (int m = num.length-1; m =0; m–) {
System.out.println(num[m]);//從大到小輸出
}
}
String[] str = scan.nextLine().split(” “),// 輸入時以空格隔開數字。
int[] num = new int[str.length];//初始化一個整型數組,長度為你輸入數字的個數。
String[] str = scan.nextLine().split(” “);// 輸入時以空格隔開數字
int[] num = new int[str.length];//初始化一個整型數組,長度為你輸入數字的個數
for (int i = 0; i str.length; i++) {
num[i] = Integer.parseInt(String.valueOf(str[i]));//將字符轉換為int型再賦給整型數組
}
Arrays.sort(num);//升序排序
System.out.println(“從小到大輸出:”);
for (int j = 0; j num.length; j++) {
System.out.println(num[j]);//從小到大輸出
JAVA 100個數字用算法排序
class SortTest { // 冒泡排序
public void sort(int[] args) {
for (int m : args) {
System.out.print(“排序前 ” + args[m] + ” “);
}
int time1 = 0, time2 = 0;
for (int i = 0; i args.length – 1; i++) {
++time1;
for (int j = i + 1; j args.length; j++) {
++time2;
int temp;
if (args[i] args[j]) {
temp = args[j];
args[j] = args[i];
args[i] = temp;
}
}
}
System.out.println();
System.out.println(“外循環次數:” + time1 + “內循環次數:” + time2);
for (int n : args) {
System.out.print(“排序後 ” + n + ” “);
}
}
public static void main(String[] args) {
int[] arg = new int[] { 2, 1, 4, 5, 8, 7, 6, 3, 9, 0 };
new SortTest().sort(arg);
}
}
// 降序排列 循環次數最少
// 輸出結果為:
// 排序前 4 排序前 1 排序前 8 排序前 7 排序前 9 排序前 3 排序前 6 排序前 5 排序前 0 排序前 2
// 外循環次數:9 內循環次數:45
// 排序後 0 排序後 1 排序後 2 排序後 3 排序後 4 排序後 5 排序後 6 排序後 7 排序後 8 排序後 9
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* class name: RapidSort
* description: Java快速排序法:數組和集合
* @author Jr
*
*/
public class RapidSort {
public static void QuickSort(int e[], int first, int end) {
int i = first, j = end, temp = e[first];
while (i j) {
while (i j e[j] = temp)
j–;
e[i] = e[j];
while (i j e[i] = temp)
i++;
e[j] = e[i];
}
e[i] = temp;
if (first i – 1)
QuickSort(e, first, i – 1);
if (end i + 1)
QuickSort(e, i + 1, end);
}
public static void main(String[] args) {
int arr[] = { 49, 38, 65, 97, 76, 13, 27, 49 };
int len = 8;
int i;
System.out.printf(“before sort\n”);
for (i = 0; i len; i++)
System.out.printf(“%d “, arr[i]);
System.out.printf(“\n”);
QuickSort(arr, 0, len – 1);
System.out.printf(“after sorted\n”);
for (i = 0; i len; i++)
System.out.printf(“%d “, arr[i]);
}
}
結果:
before sort
49 38 65 97 76 13 27 49
after sorted
13 27 38 49 49 65 76 97
java實現數字排列
電腦上沒有編譯器了,我說下思路吧,你把這四個數字放到一個整形數組裡面,),百是指百分位,十指十分位,個就是個位,為了防止重複數字出現,用了判斷語句if(..)
然後
public void 方法(數組)
{
for( int i=0;i數組.length;i++)
{
int 百= 數組[i]*100;
for( int j=0;j數組.length;j++)
{
if(j==i)
continue;
int 十= 數組[j]*10;
for( int k=0;k數組.length;k++)
{
if(k==i||k==j)
continue;
int 個= 數組[k];
int total = 百+十+個;
System.out.println( total);
}
}
}
}
java從鍵盤依次輸入10個正整數,存放在數組中,每輸入一個整數就進行排序,而不是最後排序?
按照你的要求依次輸入10個正整數,每輸入一個整數就進行排序的Java程序如下
import java.util.Scanner;
public class J {
public static void bubblesort(int[] arr,int n){
for(int i=0;in-1;i++){
for(int j=0;jn-1-i;j++){
if(arr[j]arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;in;i++){
System.out.print(arr[i]+” “);
}
System.out.println();
}
public static void main(String[] args) {
int a[]=new int[10];
Scanner sc=new Scanner(System.in);
for(int i=0;i10;i++){
a[i]=sc.nextInt();
bubblesort(a,i+1);
}
}
}
java實現字符串中數字的截取排序;請給出具體的代碼
import java.util.Set;
import java.util.TreeSet;
public class Demo {
public static void main(String[] args) {
String str1 = “-1an2b3j2.5”;
String str2 = “123ab1sd2asd3”;
Demo demo = new Demo();
demo.getNumbers(str1);
demo.getNumbers(str2);
}
public void getNumbers(String str) {
SetInteger set = new TreeSetInteger();
for (String s : str.split(“\\D+”)) {
if (!s.equals(“”)) {
set.add(Integer.parseInt(s));
}
}
System.out.println(set);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/184483.html