HDUACM2025

查找最大元素

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43748    Accepted Submission(s): 23779


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)
 

Author
lcy
 

Source
 

Recommend

lcy   |   We have carefully selected several similar problems for you:  2024 2027 2026 2031 2007 



#include<stdio.h>
#include<String.h>

 void insert(char str[],int index){     
	int len = strlen(str);
	char ins[6] = "(max)"; 
	for(int i = len - index;i>=1;i--){    //將index後面的字符向後移5個位置
		str[index+i+5] = str[index+i];   
	}
	for(int i = 0;i < 5;i++){
		str[i+index+1] = ins[i];
	}
}

int main(){
	char str[200],maxs;
	
	while(scanf("%s",&str) != EOF){
		
	 	maxs = str[0];
		
		
		for(int i = 0;i < strlen(str);i++){
			if(str[i]>= maxs) maxs = str[i];	
		}
		
		for(int i = 0;i < strlen(str);i++){
			if(str[i]==maxs){
				insert(str,i);
				i+=5;
			}
		}
		
		
		for(int i = 0;i < strlen(str);i++){
			printf("%c",str[i]);
		}
		printf("\n");
	}
	
	return 0;
}


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