Codeforces Round #617 (Div. 3), problem: (B) Food Buying

Aine ·
更新时间:2024-09-21
· 698 次阅读

状态AC

放在B类仍然是比较水的,标签“math”,解题思路就是每次剩余个位数的钱不花,这样就能保证每次都会找回来>=1的钱款,用于下一次购物。可以使用迭代的方法,每次迭代需要更新已花钱总数和剩余钱款总数,最后剩余的钱小于10后跳出循环。也可以用递归的方法,每一层递归求出当前花的钱,返回这个数字与下一次递归的和。

我提交的是递归解:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
class Operater {
public:
	static unsigned int call(unsigned int k);
};
unsigned int Operater::call(unsigned int k) {
	if (!(k / 10))
		return k;
	unsigned int tmp = k / 10;
	return (10*tmp + call(tmp+k-10*tmp));
}
int main() {
	int test_loop = 0;
	cin >> test_loop;
	while (test_loop) {
		test_loop--;
		unsigned int S = 0;
		cin >> S;
		cout << (Operater::call(S)) << endl;
	}
	return 0;
}

作者:YGLeeeon



problem CodeForces round div

需要 登录 后方可回复, 如果你还没有账号请 注册新账号