您的位置:首页 > 其它

寻找最大数(二)

2015-07-24 21:01 239 查看


寻找最大数(二)

时间限制: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: