• 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果


     1 package com.rui.test; 
     2 
     3 import java.util.Random;
     4 
     5 /** 
     6 * @author poseidon 
     7 * @version 1.0
     8 * @date:2015年10月25日 上午11:12:24 
     9 * @description: 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果
    10 * 陷阱:
    11 * 1.循环需要倒着写,为什么?
    12 *     想想顺着写循环的结果,第一个元素的值会发生变化后面再除就会有问题
    13 * 2.需要考虑除数为零的边界
    14 * 考察:
    15 *     面试者的思维、分析、编程的能力
    16 * 小结:
    17 *     题目不难,但是可以看出编程过程中思考问题的全面性,以及防止异常的发生,保障程序的健壮
    18 * 来源:
    19 *     《编程之美》
    20 * 
    21 */ 
    22 public class Function {
    23 
    24     public static void main(String[] args) {
    25         int[] arr = getRadom(10,100);
    26         int len = arr.length;
    27         System.out.println("遍历前-->");
    28         for(int i=0;i<len;i++){
    29             System.out.print(arr[i]+",");
    30         }
    31         if(arr[0] !=0){
    32             for(int i=len-1;i>=0;i--){
    33                 arr[i] /= arr[0];
    34             }
    35             System.out.println();
    36             System.out.println("遍历后-->");
    37             for(int i=0;i<len;i++){
    38                 System.out.print(arr[i]+",");
    39             }
    40         }else{
    41             System.out.println("除数不能为零");
    42         }
    43     }
    44     
    45     /**
    46      * 获取随机数组:长度为len,范围0-num
    47      */
    48     public static int[] getRadom(int len,int num) {
    49         int[] arr = new int[len];
    50         for(int i=0;i<len;i++){
    51             Random random = new Random();
    52             arr[i] = random.nextInt(num);
    53         }
    54         return arr;
    55     }
    56     
    57     
    58 }
  • 相关阅读:
    数字以万做单位——Java
    创建二维码工具类——Java
    Java 截取指定长度的字符串
    堆排序
    context:component-scan 的使用说明
    @Autowired @Resource @Qualifier的区别
    声明对象和创建对象的区别
    maven 命令
    maven 创建
    maven pom.xml文件
  • 原文地址:https://www.cnblogs.com/sun-rain/p/4908511.html
Copyright © 2020-2023  润新知