码迷,mamicode.com
首页 > 其他好文 > 详细

贪心 - bailian4137:最小新整数

时间:2020-04-01 11:16:40      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:can   turn   int   for   while   策略   rac   链接   open   

题目链接

http://bailian.openjudge.cn/practice/4137/
这个题目原本以为选出前k大的数删除就可以得到正确结果,但是这种策略是错的,举一个反例:52376 2,如果取出7 6,得到523,显然还有更小的数 236。
正确的贪心策略是从左到右每次选取一个比后一位大的数,删除,进行k次,如果所有数字是增序的,就删除最后一个数字。

解题代码

#include <cstdio>
#include <cstring>
char s[15];
int m;

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%s %d", s, &m);
        int l = strlen(s);
        while(m--){
            for(int i = 0; i < l; i++){
                if(s[i] > s[i+1]){
                    for(int j = i; j < l; j++){
                        s[j] = s[j+1];
                    }
                    break;
                }
            }
            l--;
        }
        printf("%s\n", s);
    }
    return 0;
}

贪心 - bailian4137:最小新整数

标签:can   turn   int   for   while   策略   rac   链接   open   

原文地址:https://www.cnblogs.com/zhangyue123/p/12610678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!