PRoblem Description 對于輸入的每個字符串,查找其中的最大字母,在該字母后面插入字符串“(max)”。
Input 輸入數據包括多個測試實例,每個實例由一行長度不超過100的字符串組成,字符串僅由大小寫字母構成。
Output 對于每個測試實例輸出一行字符串,輸出的結果是插入字符串“(max)”后的結果,如果存在多個最大的字母,就在每一個最大字母后面都插入”(max)”。
Sample Input abcdefgfedcba xxxxx
Sample Output abcdefg(max)fedcba x(max)x(max)x(max)x(max)x(max)
import java.util.*;class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String sr = sc.next(); char []s=sr.toCharArray(); int a[]=new int[s.length]; for(int i=0;i<s.length;i++){ a[i]=(int)s[i]; //將字母轉化為ASC碼 } int n =a[0]; for(int i=1;i<s.length;i++){ if(n<a[i]){n=a[i];} //找出最大的 } for(int i=0;i<s.length;i++){ if(a[i]==n){ System.out.print(s[i]+"(max)"); } else{ System.out.print(s[i]); } } // 輸出 System.out.println(); } }}新聞熱點
疑難解答