A—猜拳游戏
A-猜拳游戏_牛客小白月赛76 (nowcoder.com)
思路:随便代一个。例如长途说拳头,他就会以为你出布来赢,所以长途为了赢他就会出剪刀,这时你出拳头就能赢
代码:
#include <iostream>
using namespace std;
int main()
{
string s;
cin >> s;
cout << s << endl;
return 0;
}
B—Kevin喜欢一
B-Kevin喜欢一_牛客小白月赛76 (nowcoder.com)
思路:由于是可以随意选定长度复制的,我们每次就求可以复制的最大值,至于小于每次复制最大值的就不用管。
代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int nums = 0;
int x = 1;
while(x < n){
x += pow(2, nums);
nums++;
}
cout << nums << endl;
}
return 0;
}
C—A加B,A模B
C-A加B,A模B_牛客小白月赛76 (nowcoder.com)
思路:当我们使用b = n - a然后遍历a,判断a % b == m是否成立时,题目会TLE,故O(n)的时间复杂度是会超时的。这时就要放弃这个方法。观察n,m的关系我们可以看出当m * 2 >= n时是不存在解的(如果不理解可以随意代一组数);而当m * 2 < n时就可以直接让a = m, b = n - m。
代码:
#include <iostream>
using namespace std;
int main()
{
cin.tie();
int t;
cin >> t;
while(t--)
{
int n, m;
cin >> n >> m;
if(m < (n + 1) / 2){
cout << m << ' ' << n - m << endl;
}
else{
cout << -1 << endl;
}
}
return 0;
}
D—MoonLight的运算问题
D-MoonLight的运算问题_牛客小白月赛76 (nowcoder.com)
思路:如果直接全程用x = max(x * a, x + a)的话,那么就算x是long long 的类型也会有些例子过不了。所以我们就得思考怎么在边计算的情况下一边%mod(让x的值不至于过大)。我这里是用当x的值大于等于2时,并且a > 1时就使用x = x * a(条件为2的原因是当x >= 2时,只要a > 1那么一直有 x * a >= x + a.至于a > 1,x * 1 < x + 1);其他的情况就用x = x + 1;当分成两组的时候就可以边计算边%mod。
代码:
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
ll mod = 998244353;
int main()
{
int t;
cin >> t;
while(t--)
{
ll n, a;
cin >> n;
bool f = false;
ll x = 0;
for(int i = 1; i <= n; i++) {
cin >> a;
if(f && a > 1){
x = (x * a) % mod;
}
else{
x = (x + a);
if(x >= 2) f = true;
x %= mod;
}
}
cout << x % mod << endl;
}
return 0;
}
E—括号序列操作专家
E-括号序列操作专家_牛客小白月赛76 (nowcoder.com)
思路:要得到最少的移动次数,那么最佳的移动选择就是让左括号去向前匹配还未匹配的右括号。如果单纯的去匹配最近的右括号时,则会导致后面还是会要右括号来与之匹配,这时右括号要跨过的长度(交换的次数)就会更多。
这里用sum去记录遍历过程中的左右括号的数量,当前面有右括号时,就去寻找后面最近的左括号。
代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
string s;
cin >> n >> s;
int t = count(s.begin(), s.end(), '(');
if(t * 2 != n){
cout << -1 << endl;
continue;
}
s = " " + s;
long long res = 0;
int sum = 0;
for(int i = 1, j = 1; i <= n; i++){
if(s[i] == '(') sum++;
else sum--;
if(sum < 0){
//sum = 1是因为当交换完s[j],s[i]后,则后面会出现一个')'
//所以我们需要让sum = 1
sum = 1;
//不能利用前面的'('
if(j <= i) j = i;
while(s[j] != '(') j++;
res += j - i;
swap(s[j], s[i]);
}
}
cout << res << endl;
}
return 0;
}