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;
}
相关文章推荐
- nyoj 1057 寻找最大数(三)(贪心)
- NYOJ 1057 寻找最大数(三)
- nyoj1057 寻找最大数(三)
- nyoj 1057寻找最大数(三) 贪心
- NYOJ 1057 寻找最大数(三)
- nyoj 1057寻找最大数(三)
- NYOJ 1057 寻找最大数(三) (贪心)
- NYOJ1057:寻找最大数(三)
- NYOJ-1057-寻找最大数(三)
- nyoj 1057 寻找最大数(三)
- 【贪心】 nyoj1057寻找最大数(三)
- NYOJ---题目448寻找最大数
- NYOJ 1057 寻找最大数(三)(贪心)
- 题目1057:寻找最大数(三)
- NYOJ 1057 寻找最大数(三)(贪心)
- NYOJ 1057 寻找最大数(三)贪心算法
- nyoj 1057 寻找最大数(三)
- nyoj--1057--寻找最大数(三)(贪心)
- 南阳oj NYOJ 寻找最大数 题目236
- nyoj 1057 寻找最大数(三) 贪心