一.输入10个人的成绩,计算平均分,记录低于平均分的人数,并将他们的成绩输入到一个数组中
import
java.util.Scanner;
public class ChengJi {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("输入十个人的成绩");
int [] a=new int[10];
int sum=0;
int count=0;
double average=0.0;
for(int i=0;i<10;i++) {
a[i]=sc.nextInt();
sum+=a[i];
}
average=sum/10.0;
for(int i=0;i<a.length;i++) {
if(a[i]<average) {
count++;
}
}
System.out.println("平均分为:"+average);
System.out.println("低于平均分的人数有"+count+"个");
}
}
二.使用递归方法从数组中找出最大值
public class SZMax {
public static void main(String[] args) {
int[] a= {6,1,5,4,51,11,1,14,1,25,9,474,26,7};
int max=Result(a,0,0);
System.out.println("最大值为:"+max);
}
public static int Result(int [] a,int i,int max) {
if(i==a.length) {
return max;
}
max=Math.max(max,a[i]);
i++;
return Result(a,i,max);
}
}
三.求解一元二次方程组(三种情况)(一个解,两个,复数解)
import
java.math.BigDecimal;
import
java.util.Scanner;
public class FangChen {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("输入a:");
double a=sc.nextDouble();
System.out.println("输入b:");
double b=sc.nextDouble();
System.out.println("输入c:");
double c=sc.nextDouble();
double d=b*b-4*a*c;
if(d<0) {
System.out.println("方程有复数解");
lowerzero(a,b,d);
}else if(d==0) {
System.out.println("方程有唯一解");
equalzero(a,b,d);
}else {
System.out.println("方程有两个解");
higherzero(a,b,d);
}
}
public static void lowerzero(double a ,double b,double d) {
double p=-b/(a*2);
double q=Math.sqrt(-d);
BigDecimal bd=new BigDecimal(q);
System.out.println("x1="+p+"+"+bd.setScale(3,BigDecimal.ROUND_HALF_UP)+"i");
System.out.println("x2="+p+"-"+bd.setScale(3,BigDecimal.ROUND_HALF_UP)+"i");
}
public static void equalzero(double a,double b,double d) {
double x=-(b/(a*2));
System.out.println("x="+x);
}
public static void higherzero(double a,double b,double d) {
double p=-b/(a*2);
double q=Math.sqrt(d);
System.out.println("x1="+p+"+"+q);
System.out.println("x2="+p+"-"+q);
}
}
四.输入字符串和一个数字,并将字符串以数组方式存储并进行平移操作, 如abdcf 向前平移2个 变成dcfab
import
java.util.Scanner;
public class YiDong {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s=sc.nextLine();
System.out.println("请输入一个数字:");
int n=sc.nextInt();
char[] chs=s.toCharArray();
char[] c=move(chs,n);
System.out.println("移动后的字符串为:");
for(int i=0;i<c.length;i++) {
System.out.print(c[i]);
}
System.out.println();
}
public static char[] move(char[] chs,int n) {
char [] arr=new char[chs.length];
int t=0;
for(int i=0;i<chs.length;i++) {
t=(i+n)%chs.length;
arr[t]=chs[i];
}
return arr;
}
}
五.用二维数组实现矩阵乘法
import java.util.Scanner;
public class ChengFa {
public static void main(String[] args) {
int [][] a= {{1,2,3},{3,4,5},{5,6,7}};
int [][] b= {{1,2,3},{2,3,4},{3,4,5}};
System.out.println("第一个数组:");
for(int i=0;i<a[0].length;i++) {
for(int j=0;j<a.length;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("第二个数组:");
for(int i=0;i<b[0].length;i++) {
for(int j=0;j<b.length;j++) {
System.out.print(b[i][j]+" ");
}
System.out.println();
}
System.out.println("结果为:");
int [][] re=Multiple(a,b);
print(re);
}
public static int [][] Multiple(int [][] a,int [][] b) {
int [][] re = new int[a.length][b[0].length];
for (int i = 0; i<a.length; i++) {
for (int j = 0; j<b[0].length; j++) {
for (int k = 0; k<a[0].length; k++) {
re[i][j]= re[i][j]+a[i][k]*b[k][j];
}
}
}
return re;
}
public static void print(int[][] re) {
for(int i=0;i<re[0].length;i++) {
for(int j=0;j<re.length;j++) {
System.out.print(re[i][j]+" ");
}
System.out.println();
}
}
}
六.输入工人的工资,要求工资从小到大进行排序,并以链表的形式进行存储并输出
七.快速排序
public class Qucik {
public static void main(String[] args) {
int[] arr = { 49, 38, 65, 97, 23, 22, 76, 1, 5, 8, 2, 0, -1, 22 };
System.out.println("排序前:");
for(int j:arr) {
System.out.print(j+" ");
}
System.out.println();
quickSort(arr, 0, arr.length - 1);
System.out.println("排序后:");
for (int i : arr) {
System.out.print(i+" ");
}
}
private static void quickSort(int[] arr, int low, int high) {
if (low < high) { // 找寻基准数据的正确索引
int index = getIndex(arr, low, high);
quickSort(arr, 0, index - 1);
quickSort(arr, index + 1, high);
}
}
private static int getIndex(int[] arr, int low, int high) {
int tmp = arr[low]; // 基准数据
while (low < high) {
while (low < high && arr[high] >= tmp) {
high--;
}// 当队尾的元素大于等于基准数据时,向前挪动high指针
arr[low] = arr[high]; //尾元素小于tmp,将其赋值给low
while (low < high && arr[low] <= tmp) {
low++;
} // 当队首元素小于等于tmp时,向前挪动low指针
arr[high] = arr[low]; //首元素大于tmp,将其赋值给high
}
// 跳出循环时low和high相等,此时的low或high就是tmp的正确索引位置, low位置的值并不是tmp,所以需要将tmp赋值给arr[low]
arr[low] = tmp;
return low; // 返回tmp的正确位置
}
}
请文明使用资源,如果发现有代码错误或者不懂欢迎留言评论。