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

最大递减数

时间:2015-05-09 10:15:45      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

#include "Degressive.h"
#include <stdlib.h>
#include <string.h>






//给出一个非负整数,找到这个非负整数中包含的最大递减数。一个数字的递减数是指相邻的数位从大到小排列的数字。
//如: 95345323,递减数有:953,95,53,53,532,32, 那么最大的递减数为953。
//如果输入的数字为负数,返回-1。
int getMaxDegressiveNum (int num)
{
    // your code here...
if(num < 0)
{
return -1;
}
char c[128];
memset(c, 0,sizeof(char)*128);
itoa(num, c, 10);
int len = strlen(c);//获取c数组中字符的个数


int max = c[0] - ‘0‘;
int tempMax = c[0] - ‘0‘;
if(len == 1)
{
return tempMax;
}
for(int i = 1; i < len; ++i)
{
if(c[i -1] > c[i])//递减
{
tempMax = tempMax * 10 + (c[i] - ‘0‘);
}
else
{
if(tempMax >= max)
{
max = tempMax;
tempMax = c[i] - ‘0‘;
}
}


}
if(tempMax > max)//处理最后的可能的最大值
{
max = tempMax;
}


return max;
}

最大递减数

标签:

原文地址:http://blog.csdn.net/xiaohanstu/article/details/45598903

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