HDU-2025

查找最大元素

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


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


#解题思路:
用字符数组存储字符串,gets()实现输入字符串。第一个for循环中对每一个字符进行遍历比较,找到最大字符的下标并且保存。第二个for循环遍历输出,一旦碰到a[i]的值与a[j]的值相等,则输出一个"(max)"字符串。

#源代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

char a[120];
int main()
{
	int j;
	while(gets(a))
	{
		int x = '0';
		int p = strlen(a);
		for(int i = 0;i < p;i++)
		{
			if(x <= a[i]){
				x = a[i];
				j = i;	
			}
		}
		
		for(int i = 0;i < p;i++)
		{
			cout<<a[i];
			if(a[i] == a[j])
				cout<<"(max)";
		}
		cout<<endl;
	}
	return 0;
}
 

#心得体会:
很简单的一道题,关键在于要保存最大值字符下标即可。


 
posted @ 2018-01-29 12:35  Western_Trail  阅读(231)  评论(0编辑  收藏  举报