基本算法
1.输入10个随机数并能从小到大排序:
import java.util.Scanner;
public class Base_MP {
public static void main(String[] args) {
int[] a=new int[10];
System.out.println("请输入数字(10个):");
Scanner scanner=new Scanner(System.in);
if (scanner.hasNextInt()){
for (int i = 0; i < a.length; i++) {
a[i]=scanner.nextInt();
}
}
for (int i = 0; i < a.length-1; i++) {
for (int j=0;j<a.length-1-i;j++){
if (a[j]>a[j+1]){
int x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
}
}
System.out.print("数字从小到大排列后为:{");
for (int x=0;x<a.length;x++){
System.out.print(a[x]+" ");
}
System.out.println("}");
}
}
2.输入10个随机数并能根据输入的数得出连续相加最大的值
import java.util.Scanner;
public class Base_MP {
public static void main(String[] args) {
int[] a=new int[10];
System.out.println("请输入数字(10个):");
Scanner scanner=new Scanner(System.in);
if (scanner.hasNextInt()){
for (int i = 0; i < a.length; i++) {
a[i]=scanner.nextInt();
}
}
int[] b=new int[10];
for (int i = 0; i < a.length; i++) {
b[i]=a[i];
}
for (int i = 0; i < a.length-1; i++) {
for (int j=0;j<a.length-1-i;j++){
if (a[j]>a[j+1]){
int x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
}
}
int max=a[a.length-1];
System.out.println("最大数:"+GetMax(b, max));
}
public static int GetMax(int[] arr,int t_max){
for (int i=0;i<arr.length-1;i++){
int temp=0;
for (int j=i;j<arr.length-1;j++){
temp+=arr[j];
if (temp>t_max){
t_max=temp;
}
}
}
return t_max;
}
}