生活随笔
收集整理的這篇文章主要介紹了
4.10招商银行笔试编程题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.序列找數
題目描述:從非負整數序列0,1,2,….,n中給出包含其中n個數的子序列,請找出未出現在該子序列中的那個數。 輸入描述:輸入為n+1個非負整數,用空格分開。 其中:首個數字為非負整數序列的最大值n,后面n個數字為子序列中包含的數字。 輸出描述:輸出為1個數字,即未出現在子序列中的那個數。 示例: 輸入: 3 3 0 1 輸出: 2
package org.buptdavid.datastructure.array;
import java.util.Scanner;
/*** 序列找數* Created by ZD on 2018/4/10.*/
public class Main_Z1 {public static void main (String[] args){Scanner sc =
new Scanner(System.in);
int n = sc.nextInt();
if (n >=
0 ){StringBuilder b =
new StringBuilder();
for (
int i =
0 ; i < n; i++){b.append(sc.nextInt());}findNoNumber(b.toString());}}
/*** 找到未出現在該子序列中的數* @param s*/ private static void findNoNumber (String s) {
if (s ==
null || s.length() ==
0 )
return ;
for (
int i =
0 ; i <= s.length(); i++){
if (!s.contains(
" " +i+
" " )){System.out.println(i);
return ;}}}
}
2.小招喵跑步
題目描述: 小招喵喜歡在數軸上跑來跑去,假設它現在站在點n處,它只會3種走法,分別是: 1.數軸上向前走一步,即n=n+1; 2.數軸上向后走一步,即n=n-1; 3.數軸上使勁跳躍到當前點的兩倍,即n=2*n 現在小招喵在原點,即n=0,它想去點x處,快幫小招喵算算最快的走法需要多少步? 輸入描述: 小招喵想去的位置x 輸出描述: 小招喵最少需要的步數 示例: 輸入: 3 輸出: 3
import java.util.Scanner;
/*** 小招貓跑步* Created by ZD on 2018/4/10.*/
public class Main_Z2 {public static void main (String[] args){Scanner sc =
new Scanner(System.in);
while (sc.hasNext()) {
long x = sc.nextLong();System.out.println(countQuickSteps(x));}}
/*** 計算最快走法需要多少步* @param x*/ private static long countQuickSteps (
long x) {
if (x <
0 )x = -x;
long quickSteps;
if (x ==
0 )
return 0 ;
if (x ==
1 )
return 1 ;
if (x ==
2 )
return 2 ;
if (x %
2 ==
0 ){quickSteps = countQuickSteps(x/
2 ) +
1 ;}
else {quickSteps = countQuickSteps(x /
2 ) +
2 ;}
return quickSteps;}
}
3.滿足條件的數字
題目描述: 如果一個正整數,它的質因數只包含2,3,5,那么這個數滿足條件。 同時,特別規定1也是滿足條件的數字。 示例:前10個滿足條件的數字是1,2,3,4,5,6,8,9,10,12 請編寫一個函數,返回第n個滿足條件的數 輸入描述: 輸入為n 輸出描述: 輸出為第n個滿足條件的數 示例: 輸入: 5 輸出: 5
package org.buptdavid.datastructure.array;
import java.util.Scanner;
/*** Created by ZD on 2018/4/10.*/
public class Main_Z3 {public static void main (String args[]){Scanner sc =
new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();countN(n);}}
/*** 打印出第n個滿足條件的數* @param n*/ private static void countN (
int n) {
if (n <=
0 )
return ;
int count =
1 ;
int number =
1 ;
while (count < n){number++;
int temp = number;
boolean flag1 =
true ;
while (temp !=
1 && flag1) {
if (temp %
2 ==
0 ) {temp = temp /
2 ;flag1 =
true ;
continue ;}
else {flag1 =
false ;}
if (temp %
3 ==
0 ) {temp = temp /
3 ;flag1 =
true ;
continue ;}
else {flag1 =
false ;}
if (temp %
5 ==
0 ) {temp = temp /
5 ;flag1 =
true ;
continue ;}
else {flag1 =
false ;}}
if (temp ==
1 )count++;}System.out.println(number);}}
說明:第二題只能通過80%,第三題,當n足夠大時,時間復雜度高,所以只通過83%。
總結
以上是生活随笔 為你收集整理的4.10招商银行笔试编程题 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。