一开始写的代码只过了小部分样例, 如果数很大的话就WA了。
下面是一开始的代码:
C++ 代码
#include<iostream>
#include<vector>
using namespace std;
vector<int> a;
int main(){
int n, x;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d", &x);
a.push_back(x);
}
int maxSum = -1e9, sum, ansDep = 1;
for(int i = 0, j = 1, dep = 1; i < a.size(); i++, dep++){
sum = 0;
for(int k = i, m = 0; k < a.size() && m < j; m++,k++) sum += a[k];
if(maxSum < sum){
maxSum = sum;
ansDep = dep;
}
j *= 2;
i = j - 2;
}
cout << ansDep << endl;
return 0;
}
下面是改后的代码将maxSum
和 sum
都定义为long long
类型的了。
C++ 代码
#include<iostream>
#include<vector>
using namespace std;
vector<int> a;
int main(){
int n, x;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d", &x);
a.push_back(x);
}
long long maxSum = a[0], sum;
int ansDep = 1;
for(int i = 0, j = 1, dep = 1; i < a.size(); dep++){
sum = 0;
for(int k = i, m = 0; k < a.size() && m < j; m++,k++) sum += a[k];
if(maxSum < sum){
maxSum = sum;
ansDep = dep;
}
j *= 2;
i = j - 1;
}
cout << ansDep << endl;
return 0;
}
##define int long long
开玩笑的,这种写法不可取哦
哈哈哈哈哈哈哈哈哈