2020科大讯飞提前批笔试

2020科大讯飞提前批笔试

1.键盘录入1,5,10,50,100元钞票的数量abcde,再录入总金额k,怎么样才能用最少的钞票凑够k元,输出所需钞票数目,凑不出来输出-1

import java.util.Scanner;
public class Test6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int moeny[] = {1,5,10,50,100};
        int[] a = new int[5];
        int count = 0;
        for(int i = 0;i < a.length;i++){
            a[i] = sc.nextInt();
        }
        int  k = sc.nextInt();

        //贪心,尽可能去用大面额优先,先把大面额用完这样钞票会少
        for(int i = 4;i >= 0;i--){
            //先把大面额用完,100,50,5,1
            while(k >= moeny[i] && a[i] > 0){
                k -= moeny[i];
                a[i] -= 1;
                count++;
            }
        }
        if(k == 0){
            System.out.println(count);
        }else {
            System.out.println(-1);
        }
    }
}

2.用某种排序方法对给定序列进行排序,输出排序过程。
例子:先从键盘读取序列的长度,再读入序列 25 84 21 47 15 27 68 35 20 ,其排序过程如下:
15 20 21 25 47 27 68 35 84
15 20 21 25 47 27 68 35 84
15 20 21 25 47 27 68 35 84
15 20 21 25 35 27 47 68 84
15 20 21 25 27 35 47 68 84
15 20 21 25 27 35 47 68 84

经典快排:以一个数为基准值,比其小的放左边,比其大的放右边,再对左右用相同手段递归排序
第一次以25为分割基准:15 20 21 25 47 27 68 35 84
第二次对25左边递归以15为基准:15 20 21 25 47 27 68 35 84
第三次:对15左边递归没有,对其右边递归,以20为基准:
15 20 21 25 47 27 68 35 84
第四次:25左边递归完毕,递归25的右边,第一趟以47为基准:15 20 21 25 35 27 47 68 84
第五次:递归右半部分47的左边:15 20 21 25 27 35 47 68 84
第六次:15 20 21 25 27 35 47 68 84
这里中轴值取得是最左边

import java.util.Scanner;
import java.util.Arrays;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] arr = new int[n];
		for(int i = 0;i < n;i++){
			arr[i] = sc.nextInt();
		}
		System.out.println(Arrays.toString(QuickSort(arr,0,arr.length - 1)));
	}
	//快速排序
public static int[] QuickSort(int[] arr,int left,int right){
    if(left < right){
        int pivot = partition(arr,left,right);
        //左递归
        QuickSort(arr,left,pivot - 1);
        //右递归
        QuickSort(arr,pivot + 1,right);

    }
    return arr;

}
//将数组整理成有序数组,使左边所以都比基准值小,右边均比基准值大,并且返回排列后基准值索引
public static int partition(int[] arr,int left,int right){
    //取第一个作基准值
    int key = arr[left];
    while(left < right){
        //找到比基准值小的跳出,把比基准值小的放左边
        while(left < right && arr[right] >= key){
            --right;
        }
        arr[left] = arr[right];
        //大的放右边
        while(left < right && arr[left] <= key){
            ++left;
        }
        arr[right] = arr[left];
    }
    //基准值插入中间
    arr[left] = key;
    return left;
}

}

3.判断两个矩形是否相交,输入为两个矩形对角线的顶点坐标,相交输出1,不相交输出0
示例输入
0 0 4 3 0 1 5 2
意思:第一个矩形对角线顶点(0,0)(4,3),第二个矩阵对角线顶点(0,1)(5,2)

import java.util.Scanner;
public class Test6 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
        int[] rec1 = new int[4];
        int[] rec2 = new int[4];
        boolean flag = false;
        int[] temp = new int[8];
        for(int i = 0;i < temp.length;i++){
            temp[i] = sc.nextInt();
        }
        //这里做处理保证输入的坐标是左上右下顺序
        if(temp[0] > temp[2]){
            int a = temp[0];
            int b = temp[1];
            temp[0] = temp[2];
            temp[1] = temp[3];
            temp[2] = a;
            temp[3] = b;
            
        }
        if(temp[4] > temp[6]){
            int a = temp[4];
            int b = temp[6];
            temp[4] = temp[5];
            temp[5] = temp[7];
            temp[6] = a;
            temp[7] = b;
        }
        for(int i = 0;i < rec1.length;i++){
            rec1[i] = temp[i];
        }
        for(int i = 0;i < rec2.length;i++){
            rec2[i] = temp[i + 4];
        }
        flag = isXJ(rec1,rec2);
        if(flag){
            System.out.println(1);
        }else{
            System.out.println(0);
        }
        
   }

    /**
     * 
     * @param rec1 第一个矩形左上右下坐标
     * @param rec2 第二个矩形左上右下坐标
     * @return
     */
    public static boolean isXJ(int[] rec1,int[] rec2){
        boolean flag = false;
        flag = (long)(rec2[2] - rec1[0]) * (rec2[0] - rec1[2]) <= 0 &&
                (long)(rec2[3] - rec1[1]) * (rec2[1] - rec1[3]) <= 0;
        return flag;
    }
}




方法二:用质心判断
import java.util.Scanner;;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] temp = new int[8];
        for (int i = 0; i < temp.length; i++) {
            temp[i] = sc.nextInt();
        }
        int x1 = temp[0];
        int y1 = temp[1];
        int x2 = temp[2];
        int y2 = temp[3];
        int x3 = temp[4];
        int y3 = temp[5];
        int x4 = temp[6];
        int y4 = temp[7];

        double barycenter_x1 = (x1 + x2) / 2.0;
        double barycenter_y1 = (y1 + y2) / 2.0;
        double barycenter_x2 = (x3 + x4) / 2.0;
        double barycenter_y2 = (y3 + y4) / 2.0;

        /* 若两质心的距离小于临界值则相交 */
        if (Math.abs(barycenter_x1 - barycenter_x2) <= (Math.abs(x1 - x2) + Math.abs(x3 - x4)) / 2.0 &&
                Math.abs(barycenter_y1 - barycenter_y2) <= (Math.abs(y1 - y2) + Math.abs(y3 - y4)) / 2.0)
        {
            System.out.println(1);;
        }else {
            System.out.println(0);
        }

    }


}

4.给一串字符串,输出里面的数字,尽可能考虑异常输入
示例
输入:+a13
输出:13

import java.util.Scanner;
public class Test6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        str.replace(" ","");
        StringBuffer res = new StringBuffer();
        for(int i = 0;i < str.length();i++){
        	if(str.charAt(i) == '-'){
				res.append(str.charAt(i))
			}
            if('0' <= str.charAt(i) && str.charAt(i) <= '9'){
                res.append(str.charAt(i));
            }
        }
        System.out.println(res.toString());
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值