最大递增数

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


// MaxIncreseNum.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

char* MaxIncreaseNum(char*numstr);

int _tmain(int argc, _TCHAR* argv[])
{
	//char* in = "48917467";
	//char* in = "12345678";
	char* in = "244891746789";

	cout << "原字符串是:";
	for (int i = 0; i < strlen(in); i++)
		cout << in[i];
	cout << endl;
	cout << "字符串长度为"<<strlen(in) << endl;
	char*out = MaxIncreaseNum(in);
	cout << "最大递增数是:";
	for (int i = 0; i < strlen(in); i++)
		cout << out[i];
	cout << endl;



	//下面测试atoi函数,只是验证,与题目无关
	int k;
	k = atoi("123    ");//测试带空格的字符串的输出,结果输出为123
	cout << k <<0;
	system("pause");
	return 0;
}


char* MaxIncreaseNum(char*numstr)
{
	int j = 0;
	char*findNum = new char[strlen(numstr)];//store the find num
	char*MaxNum = new char[strlen(numstr)];//store the max num
	for (int i = 0; i < strlen(numstr); i++)
	{
		findNum[i] = ' ';
		MaxNum[i] = ' ';
	}
	MaxNum[0] = numstr[0];
	int digitofnum = 1;
	while (j < strlen(numstr)-1)
	{
		int k = 0;
		findNum[0] = numstr[j];
		while (numstr[j] < numstr[j + 1])
		{
			k = k + 1;
			j = j + 1;
			findNum[k] = numstr[j];
		}
		if (k + 1>digitofnum)//如果找到的数的位数大于之前的最大数的位数
		{
			digitofnum = k + 1;
			for (int i = 0; i < strlen(numstr); i++)
			{
				MaxNum[i] = findNum[i];
			}
		}
		if (k + 1 == digitofnum);//如果找到的数的位数等于之前的最大数的位数
		{
			int gg = atoi(MaxNum);
			int kk = atoi(findNum);
			if (kk>gg)
				for (int i = 0; i < strlen(numstr); i++)
				{
					MaxNum[i] = findNum[i];
				}
		}
		j = j + 1;
	}

	return MaxNum;
}





你可能感兴趣的:(笔试)