您的位置:首页 > 编程语言 > C语言/C++

NYOJ题目1057-寻找最大数(三)

2017-08-18 21:47 204 查看


寻找最大数(三)

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

描述

给出一个整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的整数。

求这个新的整数的最大值是多少。

输入多组测试数据。

每组测试数据占一行,每行有两个数N和K (1 ≤ N≤ 10^18; 0 ≤ K ≤ 100).
输出每组测试数据的输出占一行,输出移动后得到的新的整数的最大值。
样例输入
1990 1
100 0
9090000078001234 6


样例输出
9190
100
9907000008001234


解题思路:

找出k个数中最大的一个,然后其余数字一次移动,然后移动多少次,k--,然后决定下一次找最大数的区间缩小多少

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
char str[25];
int n;
while(scanf("%s%d",str,&n)!=EOF)
{
int head,tail,j,i,pos;
char max,temp;
int len=strlen(str);
head = 0;
max = '0';
while(1)
{
if(head == len || !n)
break;
tail = head + n;
if(tail >= len)
tail = len - 1;
max = '0';
for(i=tail; i>=head; --i)
{
if(str[i]>=max)
{
pos=i;
max=str[i];
}
}

if(pos!=head)
{
temp = str[pos];
for(j=pos; j>head; --j)
{
str[j]=str[j-1];
--n;
}
str[head]=temp;
}
++head;
}
printf("%s\n",str);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 水题