zoukankan      html  css  js  c++  java
  • 查找最大元素

    解法一

    #include <iostream>

    #include <cstdio>

    #include <algorithm>

    #include <string.h>

    using namespace std;

    char a[1000];

    char b[1000];

    void fun(char a[],char* dest)  //实现将"(max)"加入dest所指的空间

    {

        char c[1000];

        strcpy(c,dest+1);

        *(dest+1)='';

        strcat(a,"(max)");

        strcpy(dest+6,c);   //注意这里是6

    }

    int main()

    {

        while(scanf("%s",&a)!=EOF)

        {

            int l=strlen(a);

            for(int i=0;i<l;i++)

                b[i]=a[i];

            sort(a,a+l);

            char x=a[l-1];      //先借助a数组的排序找到最大的那个字符

            for(int i=0;i<l;i++)

            {

                if(b[i]==x)

                {   

                    fun(b,&(b[i]));

                    i=i+5;l=l+5;    //不能在max里面在找,并且长度也应该随着数组的变化动态变化

                }

            }

            cout<<b<<endl;

            for(int i=0;i<1000;i++)  //为了防止上一次造成的影响,这里必须在处理下一个字符串之前清空原有的所有记录

            {

                a[i]='';b[i]='';

            }

        }

        return 0;

    }

    解法2(手动抄袭,果然还是一寸短一寸强)

    #include <stdio.h>

    #include<string.h>

    int main()

    {

        int i;

        char str[100],maxch;

        gets(str);

        maxch=str[0];

        for(i=0;str[i]!='';i++){

            if(str[i]>maxch){

                maxch=str[i];

            }

        }

        for(i=0;str[i]!='';i++){

            printf("%c",str[i]);

            if(str[i]==maxch){

                printf("(max)");

            }

        }

        printf(" ");

        return 0;

    }

    #include <stdio.h>

    #include<string.h>

    int main()

    {

        int i;

        char str[100],maxch;

        while(gets(str)!=EOF)

        {

            maxch=str[0];

            for(i=0;str[i]!='';i++)     //先用擂台法求出整个字符串中的最大字符maxch

            {

                if(str[i]>maxch)

                    maxch=str[i];

            }

            for(i=0;str[i]!='';i++)  //一个一个扫描,碰到最大字符,停止对字符串的输出,先输出“(max)",再接着输出原来的字符串

            {

                printf("%c",str[i]);

                if(str[i]==maxch)

                {

                    printf("(max)");

                }

            }

            printf(" ");

        }

        return 0;

    }

    这篇文章,是又一个故事的结束...
    lazy's story is continuing.
  • 相关阅读:
    查看每个核的资源情况
    什么时候使用NO_UNNEST
    走FILTER效率高的2种情况
    PL/SQL 包头和包体
    产品研发要配合好
    ElasticSearch 文档并发处理以及文档路由
    ES(ElasticSearch) 索引创建
    BaikalDB技术实现内幕(三)--代价模型实现
    腾讯位置服务地图SDK自定义地图和路况
    mysql数据库优化
  • 原文地址:https://www.cnblogs.com/Hello-world-hello-lazy/p/13791893.html
Copyright © 2011-2022 走看看