2017百度web前端實習生線上筆試題

來源:互聯網
上載者:User

標籤:努力   ==   技術   需要   最小   知識   .com   事前   輸入   

代碼:

 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秒鐘,我這道編程題分數就拿到手了,唉。。。

通過這次線上筆試,發現對於前端的知識瞭解的太少太少了,要想從事前端,還需要加倍努力,補充知識。

2017百度web前端實習生線上筆試題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.