网易测试岗笔试第二题
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<vector>
using namespace std;
int sum_max(vector<int>&vecn,vector<int>&vecflag,int start,int k)
{
int sum = 0;
for (int i = start; i < start+k; i++)
{
if (vecflag[i]==0)
{
sum+=vecn[i];
}
else
continue;
}
return sum;
}
int main()
{
vector<int>vecn;
vector<int>vecflag;
int n,k;
cin>>n>>k;
vecn.reserve(n);
vecflag.reserve(n);
for (int i = 0; i < n; i++)
{
cin>>vecn[i];
}
for (int i = 0; i < n; i++)
{
cin>>vecflag[i];
}
int sum = 0;
if (n<=k)
{
for (int i = 0; i < n; i++)
{
sum += vecn[i];
}
cout<<sum;
system("pause");
return 0;
}
for (int i = 0; i < n-k; i++)
{
int result = sum_max(vecn,vecflag,i,k);
if (sum < result)
{
sum = result;
}
}
for (int i = 0; i < n; i++)
{
if (vecflag[i] == 1)
{
sum += vecn[i];
}
}
cout<<sum;
system("pause");
return 0;
}
#网易##测试#
#include<string>
#include<sstream>
#include<algorithm>
#include<vector>
using namespace std;
int sum_max(vector<int>&vecn,vector<int>&vecflag,int start,int k)
{
int sum = 0;
for (int i = start; i < start+k; i++)
{
if (vecflag[i]==0)
{
sum+=vecn[i];
}
else
continue;
}
return sum;
}
int main()
{
vector<int>vecn;
vector<int>vecflag;
int n,k;
cin>>n>>k;
vecn.reserve(n);
vecflag.reserve(n);
for (int i = 0; i < n; i++)
{
cin>>vecn[i];
}
for (int i = 0; i < n; i++)
{
cin>>vecflag[i];
}
int sum = 0;
if (n<=k)
{
for (int i = 0; i < n; i++)
{
sum += vecn[i];
}
cout<<sum;
system("pause");
return 0;
}
for (int i = 0; i < n-k; i++)
{
int result = sum_max(vecn,vecflag,i,k);
if (sum < result)
{
sum = result;
}
}
for (int i = 0; i < n; i++)
{
if (vecflag[i] == 1)
{
sum += vecn[i];
}
}
cout<<sum;
system("pause");
return 0;
}
#网易##测试#