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

寻找最大数(二)

时间:2015-07-24 22:46:17      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

寻找最大数(二)

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述

给你一个数字n(可能有前缀0)。

要求从高位到低位,进行 进栈出栈 操作,是最后输出的结果最大。

 

输入
有多组测试数据。
对于每组数据,输入一个n(0<=n<=10^100).
输出
输出栈操作后的结果。
样例输入
789
75948
样例输出
987
98457
提示
(用0表示进栈,1表示出栈)
789 输出 987 过程是 000111
75948 输出 98457 过程是 0001001111

输出结果应该和输入位数相同

代码:

#include<stdio.h>
#include<string.h>
int main(void)
{
	int i,j;
	int top1,top2,count,max;
	char str[110],str1[110],str2[110];
	while(scanf("%s",str)!=EOF)
	{
		top1=-1;
		top2=-1;
		for(i=0;i<strlen(str);i++)
		{
		    max=str[i]-'0';
			count=i;
			for(j=i+1;j<strlen(str);j++)
			{
				if(max<str[j]-'0')
				{
					max=str[j]-'0';
					i=j;
				}
			}
			if(top1>=0&&max<=str1[top1]-'0')
			{
				top2++;
				str2[top2]=str1[top1];
				top1--;
				i=count-1;
			}
			else
			{
				top2++;
				str2[top2]=str[i];
				for(j=count;j<i;j++)
				{
					top1++;
					str1[top1]=str[j];
				}
			}
		}
		while(top1>=0)
		{
			top2++;
			str2[top2]=str1[top1];
			top1--;
		}
		top2++;
		str2[top2]='\0';
		printf("%s\n",str2);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

寻找最大数(二)

标签:

原文地址:http://blog.csdn.net/qq_16997551/article/details/47046687

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