我已经定义了以下最大宏
#define max(a,b)(a>b?a:b);
Run Code Online (Sandbox Code Playgroud)
在main()里面,我正在做以下事情
int t,a,b,c,d;
t=max(a,b)+max(c,d);
Run Code Online (Sandbox Code Playgroud)
但是输出并不像预期的那样.t只显示a和b中的最大值.可能是什么问题呢?
我正在编码,以下代码没有提供所需的输出.当pos被除以2时,pos&1应该返回余数.当我用pos%2替换pos&1时,一切正常.可能是什么问题呢?
#include <iostream>
using namespace std;
int main(){
int y;
unsigned long long int pos;
cin>>y;
cin>>pos;
int f=0;
while(y>0){
y--;
if(pos&1==0){
f=1-f;
}
pos=pos/2;
}
if(f==1){
cout<<"blue\n";
}
else
cout<<"red\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在写一段代码,我必须执行2的除法.以下代码行给出了正确的输出
ans = ans + ((long long)cnt * (cnt-1))/2;
Run Code Online (Sandbox Code Playgroud)
但是,当我改变它
ans = ans + ((long long)cnt * (cnt-1)) >> 1;
Run Code Online (Sandbox Code Playgroud)
上面的代码有什么问题
在我的设置中,值永远不会消极
这是代码
#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
int s[1000000];
int main(){_
int t;
cin>>t;
while(t--){
int n,i,z,sum=0,p,cnt=0;
unsigned long long int ans=0;
cin>>n;
for(i=0;i<n;i++){
cin>>z;
sum+=z;
s[i]=sum;
}
sort(s,s+n);
i=0;
while(i<n){
p=s[i];
cnt=0;
while(s[i]==p){
cnt++;
i++;
}
ans=ans+((unsigned long long)cnt*(cnt-1))>>1;
}
cnt=0;
for(int i=0;i<n && s[i]<=0;i++){
if(s[i]==0){
cnt++;
}
}
ans+=cnt;
cout<<ans<<"\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输入1 …