牛客巅峰赛S2第三场初级场题解
A-牛牛打怪 https://ac.nowcoder.com/acm/contest/9246/A
先对数组从小到大排序,再遍历数组,当天数ans>=DEF[i]时,++i;每循环一次天数都要+1;
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param n int整型
* @param DEF int整型vector
* @return int整型
*/
int Minimumdays(int n, vector<int>& DEF) {
// write code here
sort(DEF.begin(), DEF.end());
int ans = 0;
int i = 0;
while (i < n)
{
if (ans >= DEF[i])
{
++i;
}
ans++;
}
return ans - 1;
}
}; B-简单的公式 https://ac.nowcoder.com/acm/contest/9246/B
快速幂,网上有很多板子和教程
class Solution {
public:
/**
* 返回c[n]%1000000007的值
* @param n long长整型 即题目中的n
* @return int整型
*/
int Answerforcn(long long n) {
// write code here
const int mod = 1000000007;
long long ans = 14;
long long x = 15;
n = n - 1;
while (n > 0)
{
if ((n & 1) == 1)
{
ans *= x;
ans %= mod;
}
x = (x * x) % mod;
n = (n >> 1);
}
return (int)ans;
}
}; #牛客编程巅峰赛##C/C++##题解#
查看9道真题和解析