牛客周赛 Round 86
——form ljzzzzz
Problem A
直接写就好了,但是当时写的时候取余(%)都忘记了
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0);
int main()
{
IOS;
int x,y,temp;
double ans;
cin>>x>>y;
ans=y/x;
temp=(int)ans;
if(y==x*temp)
{
cout<<ans;
}else
{
cout<<temp+1;
}
return 0;
}
Problem B
诈骗题,就看正数就好了
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0);
int main()
{
IOS;
int t,n,k,i;
long long temp;
cin>>t;
while(t--)
{
cin>>n>>k;
long long ans=0;
for(i=0;i<n;i++)
{
cin>>temp;
if(temp>0)
{
ans+=temp;
}
}
cout<<ans<<"\n";
}
return 0;
}
Problem C
整体思路是先消除一轮,剩下的只能改一个消除一次,不存在改一次会消除多个的情况,所以消除一次后len//2就是答案
1.一开始用python的切片来写的……对一部分,运行超时
def remove(len):
global str
i=0
while i<len-1:
if i<len-1:
if str[i] == str[i + 1]:
str = str[:i] + str[i + 2:]
if i!=0:
i-=1
len -= 2
else:
i+=1
return len;
t=int(input())
for _ in range(t):
n=int(input())
str=input()
print(remove(n)//2)
2.改变算法,用stack来实现
def remove_duplicates(s):
stack = []
for char in s:
if stack and stack[-1] == char:
stack.pop() # 移除相邻的重复字符
else:
stack.append(char) # 添加新字符
return len(stack) # 返回最终字符串的长度
t = int(input())
for _ in range(t):
n = int(input())
s = input().strip()
result_length = remove_duplicates(s)
print(result_length // 2) # 输出最终长度的一半(向下取整)
Problem D
解题思路如下:
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0);
int check(int x,int y)
{
int XOR=x^y,AND=x&y,GCD=__gcd(x,y);
if((XOR & x)==0 || (XOR & y)==0) return 2; //and和xor交换位置等价
if((GCD & x)==0 || (GCD & y)==0) return 2;
if((GCD ^ x)==0 || (GCD ^ y)==0) return 2;
return 3;
}
int main()
{
IOS;
int x,y,t;
cin>>t;
while(t--)
{
cin>>x>>y;
if((x&y)==0 || (x^y)==0)
{
cout<<1<<"\n";
}else cout<<check(x,y)<<"\n";
}
return 0;
}