From Hero to Zero 题解

题目地址

分析

考试时的一道水题但是还是没拿满,按题目意思模拟即可

#include <cstdio>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;

LL t, n, k, step;

int main() {
//	freopen("game.in", "r", stdin);
//	freopen("game.out", "w", stdout);
	scanf ("%lld", &t);
	while (t--) {
		scanf ("%lld %lld", &n, &k);
		step = 0;
		while (n) {
			if (n % k == 0) {
				n /= k;
				step ++;
			} else {
				step += n % k;
				n = n - n % k;
                                // step ++;考试时这么写,结果超时。。。
                                // n --;
			}
		}
		printf ("%lld
", step);
	}
	return 0;
} 
原文地址:https://www.cnblogs.com/cqbz-ChenJiage/p/13519423.html