zoukankan      html  css  js  c++  java
  • hdu 2025 查找最大元素 解题报告+疑问

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2025

    说的不严谨  题目描述不清楚 测试数据根本没有大写的情况 直接全小写就过了 算上大写却没过 就是搜一遍找到最大字母 再搜一遍找到相同的多输出(max)

    AC了的代码

    #include<iostream>
    #include
    <cstdio>
    #include
    <cstring>
    #include
    <iostream>
    using namespace std;
    char a[105];
    int main()
    {
    int i,j,k;
    char maxn;
    while(scanf("%s",a)!=EOF)
    {
    maxn
    ='a';
    for(i=0;i<strlen(a);i++)
    {
    if(a[i]>=maxn)
    maxn
    =a[i];
    }
    for(i=0;i<strlen(a);i++)
    {
    printf(
    "%c",a[i]);
    if(a[i]==maxn)
    printf(
    "(max)");
    }
    printf(
    "\n");
    }
    //system("pause");
    return 0;
    }

    下面这种代码考虑了大写字母的情况 但是不过 测试数据已经过了 求解释 思路:用cmp函数代替>将大写字母转换为小写字母进行比较 搜索时道maxn 和对应的大写字母都进行

    处理 后加上(maxn)

     1 #include<iostream>
    2 #include<cstdio>
    3 #include<cstring>
    4 using namespace std;
    5 char str[2000];
    6 void insert(int begin)
    7 {
    8 int i,j,k;
    9 for(i=strlen(str);i>begin;i--)
    10 {
    11 str[i+5]=str[i];
    12 }
    13 str[begin+1]='(';
    14 str[begin+2]='m';
    15 str[begin+3]='a';
    16 str[begin+4]='x';
    17 str[begin+5]=')';
    18 }
    19 bool cmp(char a,char b)
    20 {
    21 if(a>='A'&&a<='Z')
    22 {a+=32;}
    23 if(b>='A'&&b<='Z')
    24 {b+=32;}
    25 return a>=b;
    26 }
    27 int main()
    28 {
    29 int i,j,k;
    30 char maxn;
    31 while(scanf("%s",str)!=EOF)
    32 {
    33 maxn='a';
    34 for(i=0;i<strlen(str);i++)
    35 {
    36 if(cmp(str[i],maxn))
    37 {maxn=str[i];}
    38 if(maxn>='A' && maxn<='Z')
    39 {
    40
    41 maxn+=32;
    42
    43 }
    44 }
    45 if(maxn>='A'&&maxn<='Z')
    46 {maxn+=32;}
    47 for(i=0;i<strlen(str);i++)
    48 {
    49
    50 if( str[i]==maxn || str[i]==maxn-32)
    51 {
    52 insert(i);
    53 i+=5;
    54 }
    55 }
    56 printf("%s\n",str);
    57
    58 }
    59 system("pause");
    60 return 0;
    61 }
  • 相关阅读:
    C#轻量级企业事务
    扩展方法
    JDK Environment Variable And Change default JDK
    AsyncTask简单获取网络图片的例子
    mysql基础
    Oracle基础操作
    java 中 colkection集合、迭代器、增强for、泛型
    centos7 解决 mysql_connect()不支持请检查mysql模块是否正确加载
    python 操作MySQL避坑1064
    面向对象学习
  • 原文地址:https://www.cnblogs.com/yujiaao/p/2159672.html
Copyright © 2011-2022 走看看