求最大递增数

来源:互联网 发布:ping测试网络 编辑:程序博客网 时间:2024/04/27 18:22
输入一串数字,找到其中包含的最大递增数。递增数是指相邻的数位从小到大排列的数字。如: 2895345323,递增数有:289,345,23, 那么最大的递减数为345。
运行时间限制: 无限制
内存限制: 无限制
输入:
输入一串数字,默认这串数字是正确的,即里面不含有字符/空格等情况
输出:
输出最大递增数
样例输入:
123526897215
样例输出:

2689


华为机试题,要注意处理多余的数组空间,发现vs2012有的时候加注释代码报错,不知道什么原因?

#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){char input[100]; int heap[10]={0};cout<<"please input your numbers:"<<endl;cin>>input;char tmp[10];int t= 0;int h=0;for(int i =0;i<strlen(input);i++) {tmp[t]=input[i];i++;while(input[i]>tmp[t]) {tmp[++t]=input[i++];}tmp[t+1]='\0';t=0;while (tmp[t]!='\0'){heap[h]+=(tmp[t]-'0')*pow(10,strlen(tmp)-t-1);t++;}memset(tmp,'\0',10);t=0;h++;i--;}int max =heap[0];for (int h = 0; h < sizeof(heap)/sizeof(int)&&heap[h]>0; h++){max=heap[h]>max?heap[h]:max;}cout<<max<<endl;system("PAUSE");return 0;}


0 0
原创粉丝点击