寻找最大数(二)

寻找最大数(二)

时间限制: 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值