码迷,mamicode.com
首页 > 其他好文 > 详细

求最大递增数

时间:2017-01-29 17:00:25      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:nbsp   style   clu   algo   pac   最大   span   namespace   else   

题目:

描述:  
输入一串数字,找到其中包含的最大递增数。递增数是指相邻的数位从小到大排列的数字。如: 2895345323,递增数有:289,345,23, 那么最大的递减数为345。  
输入:  
输入一串数字,默认这串数字是正确的,即里面不含有字符/空格等情况  
输出:  
输出最大递增数  
样例输入:  
123526897215  
样例输出:  
2689

代码:

#include "cstdio"
#include "algorithm"
#include "cstring"
#include "cmath"
using namespace std;
char s[500];
int main()
{
    int ans=0,maxn;
    while (scanf("%s",s)==1){
        int n=strlen(s);
        ans=s[0]-0;
        maxn=ans;
        for(int i=1;i<n;i++){
            if(s[i]>s[i-1]){
                ans=ans*10+s[i]-0;
            }
            else {
                maxn=max(ans,maxn);
                ans=s[i]-0;
                maxn=max(ans,maxn);
            }
        }
        printf("%d\n",maxn);
    }
    return 0;
}

 

求最大递增数

标签:nbsp   style   clu   algo   pac   最大   span   namespace   else   

原文地址:http://www.cnblogs.com/mj-liylho/p/6357553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!