zoukankan      html  css  js  c++  java
  • 2017百度web前端实习生在线笔试题

    代码:

     1 import java.util.Scanner;
     2 
     3 public class Main {
     4     public static void main(String[] args) {
     5         Scanner sc = new Scanner(System.in);
     6         int n = sc.nextInt();
     7         int num[]=new int[n];//用户输入的数组
     8         int b[]=new int[n];//复制num
     9         int c[]=new int[n];//依次保存最小值下标(第1小、第2小、第3小...)
    10         
    11         for(int i=0;i<n;i++){
    12             num[i]=sc.nextInt();
    13         }
    14         for(int i=0;i<n;i++)
    15         {
    16             b[i]=num[i];
    17         }
    18         //num排序 升序
    19         int temp;
    20         for(int i=0;i<n;i++){
    21             for(int j=i;j<n;j++){
    22                 if(num[i]>num[j]){
    23                     temp=num[j];
    24                     num[j]=num[i];
    25                     num[i]=temp;
    26                 }
    27             }   
    28         }
    29         //找到每个最小值下标
    30         int i=0,j=0,k=0;
    31         while(true){
    32             if(num[i]==b[j]){
    33                 c[k]=j;
    34                 i++;
    35                 k++;
    36                 j=0;
    37             }
    38             else{
    39                 j++;
    40             }
    41             if(k==n){
    42                 break;
    43             }
    44         }
    45         //计算结果
    46         for(int m=0;m<n-1;m++){
    47             if(c[m]>c[m+1]){
    48                 System.out.println(n-(m+2)+1);
    49                 break;
    50             }
    51         }
    52     }
    53 }

    思路:主要是找规律,我找了好长时间,发现了规律。

    假设数组长度n,第x小值位置在第x-1小值位置前面,就开始移动,则最少操作次数为:n-x+1

    举例:

    (1):数组长度为4 ,值分别是 19 7 8 25。最少操作次数:4-3+1=2

    (2):数组长度为6 ,值分别是 19 8 7 9 10 25。最少操作次数:6-2+1=5

    第(1)个例子,最小值是7,第二小是8,第三小是19。8在7后面,不移动8,19在8前面,移动19,19是第三小,所以操作次数:4-3+1=2

    第(2)个例子,最小值是7,第二小是8。发现8在7前面,开始移动8,8是第二小,所以操作次数:6-2+1=5

    总结:只要再给我10秒钟,我这道编程题分数就拿到手了,唉。。。

    通过这次在线笔试,发现对于前端的知识了解的太少太少了,要想从事前端,还需要加倍努力,补充知识。

  • 相关阅读:
    Web 存储之localStorage
    webpack之font-awesome
    vue-cli快速构建vue项目
    npm与cnpm混用导致的问题
    错误:linker command failed with exit code 1 (use -v to see invocation)
    mac 显示隐藏文件
    iOS 不要使用tag传递TableViewCell的indexPath值
    iOS background location
    github+hexo 搭建个站
    iOS CoreBluetooth
  • 原文地址:https://www.cnblogs.com/jinghun/p/6777566.html
Copyright © 2011-2022 走看看