给你一个数字n(可能有前缀0)。
要求从高位到低位,进行 进栈出栈 操作,是最后输出的结果最大。
789 75948
987 98457
输出结果应该和输入位数相同
代码:
#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