華爲校園招聘上機試題Java實現(二)

題目1:迴文數字猜想

給出一個數字N,如68,定義它的逆數爲86,那麼經過68+86=154,154+451=605,605+506=1111這樣幾次操作後,可以得到迴文數字1111。現在需要判斷經過7次操作是否可以得到迴文,如果能,輸出迴文,如果不能輸出0。
思路:拆分各個位數並逆序相加,對和數判斷迴文。
(數字與逆序相加,如果對應位置相加不產生進位,那麼結果肯定是迴文;產生進位的有可能是迴文,如605+506)

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package tongji.edu.main;  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. public class MainHuiwen {  
  6.   
  7.     public static void main(String[] args) {  
  8.         @SuppressWarnings("resource")  
  9.         int n = new Scanner(System.in).nextInt(); //獲取輸入數字  
  10.         int hui = huiWen(n);  
  11.         System.out.println(hui);  
  12.   
  13.     }  
  14.   
  15.     /** 
  16.      * 七次操作後判斷是否是迴文 
  17.      */  
  18.     private static int huiWen(int n) {  
  19.         for (int i = 0; i < 7; i++)  
  20.             n = n + reverseNum(n);  
  21.         if (n == reverseNum(n))  
  22.             return n;  
  23.         else  
  24.             return 0;  
  25.   
  26.     }  
  27.   
  28.     /** 
  29.      * 對數字進行反轉 
  30.      */  
  31.     private static int reverseNum(int n) {  
  32.         return Integer.valueOf(new StringBuffer(String.valueOf(n)).reverse().toString()).intValue();  
  33.     }  
  34.   
  35. }  

題目2:找出大串中指定子串個數並刪除

找出大串中指定子串個數,並刪除,返回最終的字符串和所包含子串的數量

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package study.edu.main;  
  2.   
  3. public class MainDeleteSubString{  
  4.   
  5.     public static void main(String[] args) {  
  6.         String str = "123abc12de234fg1hi34j123k";  
  7.         String subStr = "1";  
  8.         String[] strArray = str.split(subStr);  
  9.         String StrDelsub="";  
  10.         int count=0;  
  11.         for(String temp:strArray){  
  12.             StrDelsub+=temp;  
  13.             if(!temp.equals(""))  //split分割字符串,如果分隔符在首部,分割後的字符串會出現一個空字符串  
  14.                 count++;  
  15.         }  
  16.         System.out.println("The number of sub str is :" + count);  
  17.         System.out.println("The result string is: " + StrDelsub);  
  18.       
  19.     }  
  20.   
  21.           
  22. }  

找出字符串中指定子串個數的另一種方法:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package study.edu.main;  
  2.   
  3. public class MainSubString {  
  4.   
  5.     public static void main(String[] args) {  
  6.         String str = "123abc12de234fg1hi34j123k";  
  7.         String subStr = "12";  
  8.         int n = deleteSub(str, subStr);   
  9.         System.out.println("The number of sub str is :" + n);  
  10.     }  
  11.   
  12.     /** 
  13.      * 利用String的indexOf()函數來判斷長串中指定字串個數 
  14.      */  
  15.     private static int deleteSub(String str, String subStr) {  
  16.         int count = 0;  
  17.         String lstr = str.toString();  
  18.         int i = lstr.indexOf(subStr);  
  19.         int now = 0;  
  20.         while (i != -1 && i < str.length()) {  
  21.             now = lstr.indexOf(subStr, i);  
  22.             if (now != -1) {  
  23.                 count++;  
  24.                 i = now + subStr.length();  
  25.             } else {  
  26.                 i = now;  
  27.             }  
  28.         }  
  29.         return count;  
  30.     }  
  31.   
  32. }  

題目3:約瑟夫環問題

有n 個人圍城一圈每次從1數起數到3就把那個人提出圈子,最後只保留一個人。
輸入: 輸入人數字符串
輸出:把最後一個人所保留位置返回出來。
比如你輸入11 的話即有11個人 [1,2,3,4,5,6,7,8,9,10,11] 。返回的是7.
如果輸入"1a" 的話,返回的是"0"

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package study.edu.main;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.List;  
  5. import java.util.Scanner;  
  6.   
  7. public class MainLink {  
  8.   
  9.     public static void main(String[] args) {  
  10.         @SuppressWarnings("resource")  
  11.         Scanner scanner = new Scanner(System.in);  
  12.         int n = 0;  
  13.         try {  
  14.             n = scanner.nextInt();  
  15.             if(n<1)  
  16.                 throw new Exception("wrong input");  
  17.             List<Integer> peopleLink = new LinkedList<Integer>();  
  18.             for (int i = 1; i <= n; i++) {  
  19.                 peopleLink.add(i);  
  20.             }  
  21.             int count = 0;  
  22.             while (peopleLink.size() > 1) {  
  23.                 for (int i = 0; i < peopleLink.size(); i++) {  
  24.                     count++;  
  25.                     if (count == 3) {  
  26.                         peopleLink.remove(i); // 出圈  
  27.                         count = 0;  
  28.                         i--; // 索引回退1步  
  29.                     }  
  30.                 }  
  31.             }  
  32.             System.out.println(peopleLink.get(0));  
  33.         } catch (Exception e) {  
  34.             System.out.println(0);  
  35.             return;  
  36.         }  
  37.   
  38.     }  
  39.   
  40. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章