腾讯笔试题-巧克力豆
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int N, begin = -1, end = -1;
cin >> N;
vector<int> v;
int tmp;
while (N--){
cin >> tmp;
v.push_back(tmp);
}
for (int i = 0; i<v.size(); i++){
if (v[i] == 1){
begin = i;
break;
}
}
for (int i = v.size() - 1; i >= 0; i--){
if (v[i] == 1){
end = i;
break;
}
}
int res = 1;
if (end > begin){
int m = 0;
for (int i = begin; i<= end; i++){
if (v[i] == 0){
m++;
}
else{
res *= (m + 1);
m = 0;
}
}
}
cout << res << endl;
return 0;
}
#腾讯#
#include <string>
#include <vector>
using namespace std;
int main(){
int N, begin = -1, end = -1;
cin >> N;
vector<int> v;
int tmp;
while (N--){
cin >> tmp;
v.push_back(tmp);
}
for (int i = 0; i<v.size(); i++){
if (v[i] == 1){
begin = i;
break;
}
}
for (int i = v.size() - 1; i >= 0; i--){
if (v[i] == 1){
end = i;
break;
}
}
int res = 1;
if (end > begin){
int m = 0;
for (int i = begin; i<= end; i++){
if (v[i] == 0){
m++;
}
else{
res *= (m + 1);
m = 0;
}
}
}
cout << res << endl;
return 0;
}
#腾讯#