zoukankan      html  css  js  c++  java
  • 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果

     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 }
  • 相关阅读:
    UVa 1451 Average (斜率优化)
    POJ 1160 Post Office (四边形不等式优化DP)
    HDU 3507 Print Article (斜率DP)
    LightOJ 1427 Substring Frequency (II) (AC自动机)
    UVa 10245 The Closest Pair Problem (分治)
    POJ 1741 Tree (树分治)
    HDU 3487 Play with Chain (Splay)
    POJ 2828 Buy Tickets (线段树)
    HDU 3723 Delta Wave (高精度+calelan数)
    UVa 1625 Color Length (DP)
  • 原文地址:https://www.cnblogs.com/sun-rain/p/4908511.html
Copyright © 2011-2022 走看看