三道機試題

第一題 水仙花數

水仙花數是指一個 n 位正整數 ( n≥3 ),它的每個位上的數字的 n 次冪之和等於它本身。

例如:153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 *3

求所有三位數中的水仙花數。

輸出要求:

    第1個水仙花數:

    ...

    水仙花數總和爲:

參考代碼:

public class Main {
	public static void main(String[] args) {
		int[] num = new int[20];
		int k = 0;
		for (int i = 100; i < 1000; i++) {
			int a = i / 100;
			int b = i % 100 / 10;
			int c = i % 100 % 10;

			int count = (a*a*a) + (b*b*b) + (c*c*c);
			if(count == i){
				num[k] = i;
				k++;
			}
		}
		int sum = 0;
		for(int i = 0 ; i < k ; i ++){
			System.out.println("第" + (i+1) +"個水仙花數: " + num[i]);
			sum = sum + num[i];
		}
		
		System.out.println("水仙花數總和爲: " + sum);
		
	}
}

第二題:密碼破解

題目的意思就是 輸入字符串,爲".-#"的組合,爲密碼串,將密碼串進行翻譯

"."代表1,-代表 0 , # 爲多個字符的分隔符(多個#號代表爲一個)。

將輸入的字符串轉換爲對於的字母,對於關係如下

0對應F         1對應G         2 對應R   3對應S  

...(中間太多了,看switch語句中的對應關係即可)

輸入:  --.####.###-..

輸出: GGS

參考代碼:

public class er {
	private static char intToChar(int n){
		char ch = 0;
		switch(n){
			case 0:
			case 17:
				ch = 'F';
				break;
			case 1:
				ch = 'G';
				break;
			case 2:
				ch = 'R';
				break;
			case 3:
				ch = 'S';
				break;
			case 4:
				ch = 'T';
				break;
			case 5:
				ch = 'L';
				break;
			case 6:
				ch = 'M';
				break;
			case 7:
				ch = 'N';
				break;
			case 8:
				ch = 'O';
				break;
			case 9:
				ch = 'P';
				break;
			case 10:
				ch = 'Q';
				break;
			case 11:
				ch = 'W';
				break;
			case 12:
				ch = 'X';
				break;
			case 13:
				ch = 'Y';
				break;
			case 14:
				ch = 'Z';
				break;
			case 15:
				ch = 'U';
				break;
			case 16:
				ch = 'A';
				break;
			case 18:
				ch = 'H';
				break;
			case 19:
				ch = 'I';
				break;
			case 20:
				ch = 'J';
				break;
			case 21:
				ch = 'K';
				break;
			case 22:
				ch = 'B';
				break;
			case 23:
				ch = 'C';
				break;
				
			case 24:
				ch = 'D';
				break;
			case 25:
				ch = 'E';
				break;
			case 26:
				ch = 'l';
				break;
			case 27:
				ch = 'm';
				break;
			case 28:
				ch = 'n';
				break;
			case 29:
				ch = 'o';
				break;
			case 30:
				ch = 'p';
				break;	
			case 31:
				ch = 'i';
				break;
			case 32:
				ch = 'j';
				break;
			case 33:
				ch = 'k';
				break;
			case 34:
				ch = 'f';
				break;
			case 35:
				ch = 'g';
				break;
			case 36:
				ch = 'h';
				break;
			case 37:
				ch = 'a';
				break;
			case 38:
				ch = 'b';
				break;
			case 39:
				ch = 'c';
				break;
			case 40:
				ch = 'd';
				break;
			case 41:
				ch = 'e';
				break;
			case 42:
				ch = 'q';
				break;
			case 43:
				ch = 'r';
				break;
			case 44:
				ch = 'w';
				break;
			case 45:
				ch = 'x';
				break;
			case 46:
				ch = 'y';
				break;
			case 47:
				ch = 'z';
				break;
			case 48:
				ch = 's';
				break;
			case 49:
				ch = 't';
				break;
			case 50:
				ch = 'u';
				break;
			case 51:
				ch = 'v';
				break;			
		}
		return ch;
	}
	public static void main(String[] args) {
		Scanner sin = new Scanner(System.in);
		if(sin.hasNext()){
			String input = sin.nextLine();
			String[] point = input.split("#");
			if(point.length == 0 ){
				System.out.println(" ");
			}
			StringBuffer result = new StringBuffer();
			int count = 0;
			char[] res = new char[point.length];
			
			for (int i = 0; i < point.length; i++) {
				for (int j = 0; j < point[i].length(); j++) {
					if(point[i].charAt(j) == '-'){
						result.append('0');
					}else if(point[i].charAt(j) == '.'){
						result.append('1');
					}
				}
				String num = result.toString();
				count= Integer.parseInt(num,2);
				if(count < 0 || count > 52){
					System.out.println("ERROR");
					break;
				}
				res[i] = intToChar(count);
				result = new StringBuffer();
			}
			
			for (int i = 0; i < res.length; i++) {
				System.out.print(res[i]);
			}
			
		}
	}

}

第三題:查找循環調用

判斷函數之間的調用關係是否爲循環調用,例如  A調用B, B調用C, C調用A; AB爲循環調用。

A調用B,B調用A,則AB也稱爲循環調用。

輸入信息,輸入一個數字n,表示後面有n個調用關係,第二行到第n+1行爲調用關係,第n+2行爲判斷的兩個函數。

輸出信息:T(表示循環調用),F(表示不是循環調用)

輸入:3(幾個調用關係)

A B

B C

C A

A B

輸出:T

參考代碼:


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class san {
	private static char isTrue(String result , String[] input){
		char end = 'F';
		Map<String, String> mapList = new HashMap<>();
		String[] results = result.split(" ");
		for (int i = 0; i < input.length; i++) {
			String[] spli = input[i].split(" ");
			mapList.put(spli[0], spli[1]);
		}
		
		StringBuffer su = new StringBuffer();
		String in =  results[0];
		for (int i = 0; i < mapList.size(); i++) {
			su.append(mapList.get(in));
			in = mapList.get(in);
		}
		
		String res = su.toString();
		if(res.contains(results[0]) && res.contains(results[1]) ){
			if(res.indexOf(results[0]) > res.indexOf(results[1])){
				end ='T';
			}
		}
		return 'T';
	}
	
	public static void main(String[] args) {
		Scanner sin = new Scanner(System.in);
		int N = 0;
		while(sin.hasNext()){
			N = Integer.parseInt(sin.nextLine());
			String[] input = new String[N];
			
			for (int i = 0; i < input.length; i++) {
				input[i] = sin.nextLine();
			}
			String result = sin.nextLine();
		
			System.out.println(isTrue(result, input));
			
		}
	}
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章