1. 程式人生 > >問題 1232: 查詢最大元素

問題 1232: 查詢最大元素

/*題目描述
對於輸入的每個字串,查詢其中的最大字母,在該字母后面插入字串“(max)”。

輸入
輸入資料包括多個測試例項,每個例項由一行長度不超過100的字串組成,字串僅由大小寫字母及數字構成

輸出
對於每個測試例項輸出一行字串,輸出的結果是插入字串“(max)”後的結果,如果存在多個最大的字母,就在每一個最大字母后面都插入"(max)"。

樣例輸入
abcdefgfedcba
xxxxx
樣例輸出
abcdefg(max)fedcba
x(max)x(max)x(max)x(max)x(max)/
import java.util.
;

public class Text_1232 {
public static void main(String[] args) {
Scanner sc = new Scanner(

System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
System.out.println(result(s));
}
}

public static String result(String s) {
	char max = s.charAt(0);
	int i;
	for (i = 1; i < s.length(); i++) {
		if (max < s.charAt(i)) {
			max = s.charAt(i);
		}
	}
	String result = "";
	for (int j = 0; j < s.length(); j++) {
		if (max > s.charAt(j)) {
			result = result + s.charAt(j);
		} else if (max == s.charAt(j)) {
			result = result + s.charAt(j) + "(max)";
		} else {
			result = result + s.charAt(j);
		}
	}
	return result;
}

}