Codeforces 758D Ability To Convert 【贪心】

题目链接:http://codeforces.com/contest/758/problem/D


题意:

有一个 n 进制下的数 k,问这个数在10进制下最小是几。

题解:

虽然 k 很大,但是 k 的位数很小,所以我们可以每次贪心的去从末尾取尽可能多的数,注意特判 0 的情况


代码:

#include <cstdio>
#include <string>
#include <iostream>

using namespace std;

// Version 3. By DenyTianly
// Time: 2017-02-02
// Verdict: Accepted 

typedef long long LL;
LL n, base = 1LL, ans;
string k;

inline void quick_IO() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); }

int main() {
	quick_IO();
	cin >> n >> k;
	int now = k.size()-1;

	while( now >= 0 ) {
		for ( int i = 0; i <= now; i ++ ) {
			if(k[i] == '0' && i != now ) continue;
			LL tot = 0LL;
			for ( int j = i; j <= now; j ++ ) {
				tot = tot*10+k[j]-'0';
				if( tot >= n ) break;
			}

			if( tot < n ) {
				ans += tot*base;
				base *= n;
				now = i-1;
			}
		}
	}
	cout << ans << endl;

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值