Find the largest element
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/others) total submission (s): 28095 accepted submission (s): 15577
Problem description searches for the maximum letter of each input string and inserts the string "(max)" after the letter )".
Input data includes multiple test instances. Each instance consists of a string of no more than 100 characters in length. The string consists of only uppercase and lowercase letters.
Output outputs a line of strings for each test instance. The output result is the result of inserting the string "(max)". If there are multiple largest letters, insert "(max)" after each maximum letter )".
Sample inputabcdefgedcbaxxxxx
Sample outputabcdefg (max) fedcbax (max) x (max)
#include <stdio.h>#include <string.h>char max(char s[]){ int i; char m=‘\0‘; for(i=0;s[i];i++) if(m<s[i])m=s[i]; return m;}int main(){ int i; char s[105],m; while(scanf("%s",s)!=EOF) { m=max(s); for(i=0;s[i];i++) { putchar(s[i]); if(m==s[i])printf("(max)"); } printf("\n"); } return 0;}
View code
# Include <stdio. h>
# Include <string. h>
Char max (char s [])
{
Int I;
Char M = '\ 0 ';
For (I = 0; s [I]; I ++)
If (M <s [I]) M = s [I];
Return m;
}
Int main ()
{
Int I;
Char s [105], M;
While (scanf ("% s", S )! = EOF)
{
M = max (s );
For (I = 0; s [I]; I ++)
{
Putchar (s [I]);
If (M = s [I]) printf ("(max )");
}
Printf ("\ n ");
}
Return 0;
}