C/C++ Programming Learning-Week 9 ⑨ Mercenary

Topic link

Title description

The maximum physical strength of a mercenary is M, the initial physical strength is 0, the combat power is N, and it has X energy elements.

When the mercenary's physical strength is exactly M, it can participate in an M-day combat period, and the physical strength value will be 0 at the end of the combat period. In the same combat period, the mercenary's combat power will increase by 1 point for n days of continuous combat, where n is the combat power at the beginning of the current combat period.

After a combat period is over, the mercenaries need to use several energy elements to restore their physical strength to the maximum M, so as to participate in the next combat period. The physical strength value recovered by each energy element does not exceed the current combat strength. Each energy element can only be used once.

Excuse me: What is the maximum combat effectiveness of mercenaries?

Note: Energy elements can be used only after the battle period is over, multiple energy elements can be used.

Input format
One line includes three integers M, N, X, separated by a single space between two adjacent integers. M, N, and X are all positive integers not exceeding 10,000.

Output format
Output an integer, which is the maximum combat effectiveness of the mercenary.

Sample Input

5 2 10

Sample Output

6

Ideas

Greedy thinking simulates the process of replenishing physical strength and fighting. When the stamina supplemented by each element is at its maximum, the combat effectiveness at the end of all battles is at its maximum.

C++ code 1:

#include<iostream>
using namespace std;
int m, n, x, hp, cnt;//hp记录当前的体力值
int main()
{
    
    
	cin >> m >> n >> x;
	while(x > 0 && hp < m)//初始化体力为0,因此需要补充体力
	{
    
    
		x--;
		hp += n;
		if(hp > m) hp = m;
	}
	while(x >= 0)//模拟能够进行的所有战斗的过程
	{
    
    
		if(hp == m)//模拟m场战斗,同时更新战斗力
		{
    
    
			hp = 0;
			n += m / n;
		}
		else break;
		while(x > 0 && hp < m)//模拟一场战斗结束后,用元素进行补充体力的过程
		{
    
    
			x--;
			hp += n;
			if(hp > m) hp = m;
		}
	}
	cout << n << '\n';//输出最后的战斗结束后的最大战斗力
	return 0;
}

C++ code 2:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int M, N, X;
	while(cin >> M >> N >> X)
	{
    
    
		int m = 0, n = N;
		while(X > 0)
		{
    
    
			X--;
			m += n;
			if(m >= M)
			{
    
    
				n += M / n;
				m = 0;
			}
		}
		cout << n << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113095852