【贪心】A Magic Lamp

Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams.
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?

题意:
多组输入,每次给出一个不超过1000位的整数n,和一个m,
你要做的是在这个n中,去掉m位,使得这个整数尽可能的小(不能更改顺序)。
最后的输出要忽略前导零,

比如 n=1234 ,m=1, 那就输出 123

代码:

ll t,n,m;
string s;
ll ans[maxn];
int main(){
	while(cin>>s>>n){
		ll l=0,r=n,cnt=0;
		n=s.size()-n;
		while(n--){
			int pos=l;
			for(int i=l;i<=r;i++){
				if(s[i] < s[pos]) pos=i;
			}
			ans[cnt++] = s[pos]-'0';
			l=pos+1;
			r++;
		}
		int pos=0;
		while(ans[pos]==0 && pos<cnt) pos++;
		if(pos==cnt) cout<<0;
		for(;pos<cnt;pos++){
			cout<<ans[pos];
		}puts("");
	}
	return 0;
}

实际写代码的时候,s就是n, n就是m

这里先让 l=0,r=n;让左坐标和右坐标卡范围,当然,右坐标要取从n到size()-1慢慢递增,因为有留出足够的位数,不然能读满m位。
然后 让 n=s.size()-n;
这就代表要剩下几个。
每次在范围里找最小的数字,然后存到a数组里去。

最后还要处理前导零。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值