算法复杂度-实验博客
1、求一个整数二维数组Arr[N][N]的所有元素之和
public class Array {
public int sum(int [][] data){
int sum = 0;
for(int i = 0; i<data.length; i++)
for (int j = 0; j<data.length; j++)
sum += data[i][j];
return sum;
}
}
public class ArrayTest {
public static void main(String[] args) {
int [][]array = {{1,2,4,5},{6,7,8,9},{10,11,12,13},{14,15,16,17}};
Array array1 = new Array();
System.out.println(array1.sum(array));
}
}
- 测试截图1
2、对于输入的任意 3 个整数, 将它们按从小到大的顺序输出。
import java.util.Scanner;
public class SortingThree {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("a=");
int a=input.nextInt();
System.out.print("b=");
int b=input.nextInt();
System.out.print("c=");
int c=input.nextInt();
int temp;
if(a>b){
temp=a;
a=b;
b=temp;
}
if(a>c){
temp=a;
a=c;
c=temp;
}
if(b>c){
temp=b;
b=c;
c=temp;
}
System.out.println(a+","+b+","+c);
}
}
- 测试截图2
对于输入的任意 n 个整数, 输出其中的最大和最小元素。
import java.util.Scanner;
public class MaxAndMin {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("The size of number:");
int n=scanner.nextInt();
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
int []b=new int[n];
for(int i=0;i<n;i++)
{System.out.print("Number"+(i+1)+":");
b[i]=scanner.nextInt();
}
for(int i=0;i<n;i++)
{
if(min>b[i]){min=b[i];}
if(max<b[i]){max=b[i];}
}
System.out.println("Min:"+min+" Max:"+max);
}
}
- 测试截图3