题解 | 有趣的区间
有趣的区间
https://www.nowcoder.com/practice/40929e3b2f2e4ca2a481f242f8ffca71
由于一堆数或起来是奇数只需要其中一个数是奇数就可以,因为奇数的二进制最后一位是1。
只要求出以每一个数结尾的满足要求的区间数量有几个即可。
#include <iostream>
using namespace std;
int main(){
int x;
cin>>x;
int lst[x+5];
int lst2[x+5];
for(int i=1; i<=x; i++)cin>>lst[i];
for(int i=1; i<=x; i++){
if(lst[i]%2 == 0)lst2[i] = 0;
else lst2[i] = 1;
}
int lst3[x+5];
for(int i=1; i<=x; i++){
lst2[i]==1?lst3[i]=i:lst3[i]=0;
}
for(int i=1; i<=x; i++){
if(lst3[i+1] == 0)lst3[i+1] = lst3[i];
}
long long sum=0;
for(int i=1; i<=x; i++)sum += lst3[i];
cout<<sum;
}
查看17道真题和解析