zoukankan      html  css  js  c++  java
  • 独木舟上的旅行

    import java.util.Arrays;
    import java.util.Scanner;
    /*
     * 独木舟上的旅行
     * 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别。一条独木舟最多只能乘坐两个人,且乘客的总重量不能超过独木舟的最大承载量。
     * 我们要尽量减少这次活动中的花销,所以要找出可以安置所有旅客的最少的独木舟条数。现在请写一个程序,读入独木舟的最大承载量、旅客数目和每位旅客的重量。
     * 根据给出的规则,计算要安置所有旅客必须的最少的独木舟条数,并输出结果。
     * 输入第一行输入s,表示测试数据的组数;
              每组数据的第一行包括两个整数w,n,80<=w<=200,1<=n<=300,w为一条独木舟的最大承载量,n为人数;
              接下来的一组数据为每个人的重量(不能大于船的承载量);输出每组人数所需要的最少独木舟的条数。
              样例输入3
       85 6
       5 84 85 80 84 83
       90 3
       90 45 60
       100 5
       50 50 90 40 60
              样例输出5
       3
       3
     */
    public class Main2 {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int s = sc.nextInt();
    		int[] count = new int[s];
    		while(s>0) {
    			count[s-1] = 0;
    			int w = sc.nextInt();
    			int n = sc.nextInt();
    			int[] arr = new int[n];
    			for(int i = 0; i < arr.length; i++) {
    				arr[i] = sc.nextInt();
    			}
    			Arrays.sort(arr);
    			int i = 0, j = arr.length - 1;
    			/*while(i <= j) {
    				if(arr[i] + arr[j] <= w) {
    					i++;
    					j--;
    				}else {
    					j--;
    				}
    				count++;
    			}*/
    			while(i <= j) {
    				if(arr[i] + arr[j] <= w) 
    					i++;
    				j--;
    				count[s-1]++;
    			}
    			s--;
    		}
    		for(int b = count.length - 1; b >= 0; b--) {
    			System.out.println(count[b]);
    		}
    		
    	}
    
    }
    

      

  • 相关阅读:
    关于mui的主页面、子页面、页面跳转
    设计图片反复闪,点击后停止(设计定时器)
    购物车+支付宝
    登陆判读,并跳转到指定页面(window.location.href='http://localhost/index.html')
    template.js遍历对象的写法
    jqurey的跨域使用getjson(http://www.jb51.net/Special/894.htm)
    安装sass并ruby更改淘宝镜像
    Y君面试记
    MapReduce 阅读笔记
    安全感
  • 原文地址:https://www.cnblogs.com/zhaohuan1996/p/8832660.html
Copyright © 2011-2022 走看看