zoukankan      html  css  js  c++  java
  • 作业题

    public class H{
    	public static void main(String[] args) {
    		
    	}
    	//1.设计一个方法,能够找出在两个数组中最大的一个数,
    	//并将其在main方法中打印。
    	//两个数组分别为{21,54,2,63,87,15,9,50}、{11,100,27,94,56}
    	//返回值类型:int
    	//参数列表:int[] a,int[] 
    	/**
    	 * [max description]
    	 * @param  a [description]
    	 * @param  b [description]
    	 * @return   [description]
    	 */
    	public static int max(int[] a,int[] b){
    		/*int max = a[0];
    		for (int num:a) {
    			if(max<num){
    				max = num;
    			}
    		}*/
    		
    		/*int max1 = b[0]
    		for (int num:b) {
    			if(max1<num){
    				max1 = num;
    			}
    		}*/
    		int max = maxArray(a);
    		int max1 = maxArray(b);
    		// 条件表达式?表达式一:表达式二
    		// if(max>max1){
    		// 	return max;
    		// }else{
    		// 	return max1;
    		// }	
    		int max2 = max>max1?max:max1;
    		return max2;
    	} 
    	/**
    	 * [maxArray description]
    	 * @param  array [description]
    	 * @return       [description]
    	 */
    	public static int maxArray(int[] array){
    		int max = array[0];
    		for (int num:array) {
    			if(max<num){
    				max = num;
    			}
    		}
    		return max;
    	}
    	//封装一个方法,求一个行列数相同的
    	//二维数组两条对角线所有元素的和。
    	//long(
    1 2 3 
    
    4 5 6 
    
    7 8 9 
    
    0 0 
    1 1
    2 2
    0 2
    2 0 
    1 2 3 4   0 0 +1 1 + 2 2 + 3 3
    
    5 6 7 8   0 3+1 2 + 2 1 + 3 0
    
    9 0 9 2
    
    3 4 5 6
    
    
    
    	/**
    	 * [add description]
    	 * @param  a [description]
    	 * @return   [description]
    	 */
    	public static long add(int[][] a){
    		long sum = 0;
    		for (int i=0;i<a.length;i++) {
    			for (int j=0;j<a[i].length;j++) {
    				if(i==j||i+j==a.length-1){
    					sum += a[i][j];
    				}
    			}
    		}
    		return sum;
    	}
    	//封装一个方法,将两个一维数组对应位置相加
    	//后存入第三个数组并返回。注意数组长度的区别,
    	//比如一长一短时,要避免下标越界
    	//1 2 3 4 5
    	//1 2 3
    	//2 4 6 4 5
    	/**
    	 * [add description]
    	 * @param  a [description]
    	 * @param  b [description]
    	 * @return   [description]
    	 */
    	public static int[] add(int[] a,int[] b){
    		int max = a.length>b.length?a.length:b.length;
    		a = Arrays.copyOf(a,max);
    		b = Arrays.copyOf(b,max);
    		int[] c = new int[max];
    		for (int i=0;i<max;i++) {
    			c[i] = a[i]+b[i];
    		}
    		return c;
    	}
    }
    

      

  • 相关阅读:
    hmset
    java 调用mongo 判断大于等于 并且小约等于<=
    Maven项目,别人的没问题,自己机器一直有问题
    linux 时间datetimectl 问题
    真正手把手教 git
    0324-SQLMAP使用参数备注
    安全推荐网址:
    JavaScript Base64 作为文件上传的实例代码解析
    学习笔记|变量的解构赋值
    学习笔记|let 和 const 命令
  • 原文地址:https://www.cnblogs.com/yangshuyuan1009/p/9842048.html
Copyright © 2011-2022 走看看