杭电oj —— 2025

import java.util.Scanner;

public class HDU_oj2025 {
/*
 * 对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”
 */
	public static void main(String[] args) {
		Scanner sn = new Scanner(System.in);
		while(sn.hasNext()) {
			String s = sn.nextLine();
			char max = s.charAt(0); //与C不同
			int[] index = new int[s.length()];
			int m = 0;
			
			//step 1:找出最大的字母
			for(int i = 0; i < s.length();i++) {
				if(max < s.charAt(i)) {
					max = s.charAt(i);
				} 
			}
			
			//step 2:记录出所有最大字母出现的下标
			for(int i = 0; i < s.length();i++) {
				if(max == s.charAt(i)) {
					index[m++] = i;
				} 
			}
			
			//step 3: 输出
			m = 0;
			for(int i = 0; i < s.length();i++) {
				System.out.print(s.charAt(i));
				if(i == index[m]) { //index[] 里面存放的为有序数据
					System.out.print("(max)");
					m++;
				}		
			}
			System.out.println();
		}
		sn.close();
	}

}

猜你喜欢

转载自blog.csdn.net/LiLi_code/article/details/87825892